Improve object classes

This commit is contained in:
Matias Griese
2017-05-05 13:38:29 +03:00
parent 7217635c6f
commit 718608cd55
6 changed files with 73 additions and 52 deletions

View File

@@ -1,5 +1,5 @@
# Feature/Objects Branch
## xx/xx/2017
## mm/dd/2017
1. [](#new)
* Added `Pages::baseUrl()`, `Pages::homeUrl()` and `Pages::url()` functions

View File

@@ -17,7 +17,7 @@ use RocketTheme\Toolbox\ArrayTraits\Export;
* @property string $id
* @package Grav\Framework\Object
*/
abstract class AbstractObject implements ObjectInterface
abstract class AbstractObject implements StoredObjectInterface
{
use ObjectStorageTrait {
check as traitcheck;

View File

@@ -20,7 +20,7 @@ interface ObjectInterface extends \ArrayAccess, \JsonSerializable
* Note that using array of fields will always make a query, but it's very useful feature if you want to search one
* item by using arbitrary set of matching fields. If there are more than one matching object, first one gets returned.
*
* @param int|array $keys An optional primary key value to load the object by, or an array of fields to match.
* @param null|int|string|array $keys An optional primary key value to load the object by, or an array of fields to match.
* @param boolean $reload Force object to reload.
*
* @return Object
@@ -37,7 +37,7 @@ interface ObjectInterface extends \ArrayAccess, \JsonSerializable
/**
* Removes all or selected instances from the object cache.
*
* @param null|int|array $ids An optional primary key or list of keys.
* @param null|int|string|array $ids An optional primary key or list of keys.
*/
static public function freeInstances($ids = null);
@@ -50,46 +50,6 @@ interface ObjectInterface extends \ArrayAccess, \JsonSerializable
*/
public function initialize();
/**
* Convert instance to a read only object.
*
* @return $this
*/
public function readonly();
/**
* Returns true if the object exists in the storage.
*
* @return boolean True if object exists.
*/
public function isSaved();
/**
* Method to load object from the storage.
*
* @param mixed $keys An optional primary key value to load the object by, or an array of fields to match. If not
* set the instance key value is used.
*
* @return boolean True on success, false if the object doesn't exist.
*/
public function load($keys = null);
/**
* Method to save the object to the storage.
*
* Before saving the object, this method checks if object can be safely saved.
*
* @return boolean True on success.
*/
public function save();
/**
* Method to delete the object from the database.
*
* @return boolean True on success.
*/
public function delete();
/**
* Method to perform sanity checks on the instance properties to ensure they are safe to store in the storage.
*

View File

@@ -26,23 +26,27 @@ class FilesystemStorage implements StorageInterface
/**
* @var string
*/
protected $type;
protected $type = 'Grav\\Common\\File\\CompiledJsonFile';
/**
* @var string
*/
protected $extension;
protected $extension = '.json';
/**
* @param string $path
* @param string $extension
* @param string $type
*/
public function __construct($path, $type = 'Grav\\Common\\File\\CompiledJsonFile', $extension = '.json')
public function __construct($path, $type = null, $extension = null)
{
$this->path = $path;
$this->type = $type;
$this->extension = $extension;
if ($type) {
$this->type = $type;
}
if ($extension) {
$this->extension = $extension;
}
}
/**
@@ -112,7 +116,7 @@ class FilesystemStorage implements StorageInterface
{
$results = [];
foreach ($list as $id) {
$results[$id] = $this->load(['id' => $id]);
$results[$id] = $this->load($id);
}
return $results;
@@ -147,7 +151,7 @@ class FilesystemStorage implements StorageInterface
protected function getFile($key)
{
if ($key === null) {
throw new \RuntimeException('Id not defined');
throw new \RuntimeException('Storage key not defined');
}
$filename = "{$this->path}/{$key}{$this->extension}";

View File

@@ -29,7 +29,7 @@ interface StorageInterface
/**
* @param string $key
* @param array $data
* @return mixed
* @return string
*/
public function save($key, array $data);

View File

@@ -0,0 +1,57 @@
<?php
/**
* @package Grav\Framework\Object
*
* @copyright Copyright (C) 2014 - 2017 RocketTheme, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/
namespace Grav\Framework\Object;
/**
* Stored Object Interface
* @package Grav\Framework\Object
*/
interface StoredObjectInterface extends ObjectInterface
{
/**
* Convert instance to a read only object.
*
* @return $this
*/
public function readonly();
/**
* Returns true if the object exists in the storage.
*
* @return boolean True if object exists.
*/
public function isSaved();
/**
* Method to load object from the storage.
*
* @param null|int|string|array $keys An optional primary key value to load the object by, or an array of fields
* to match. If not set the instance key value is used.
*
* @return boolean True on success, false if the object doesn't exist.
*/
public function load($keys = null);
/**
* Method to save the object to the storage.
*
* Before saving the object, this method checks if object can be safely saved.
*
* @return boolean True on success.
*/
public function save();
/**
* Method to delete the object from the database.
*
* @return boolean True on success.
*/
public function delete();
}