From 718608cd550e37e152d9353bd37a00259ffb08d8 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Fri, 5 May 2017 13:38:29 +0300 Subject: [PATCH] Improve object classes --- CHANGELOG.md | 2 +- .../Grav/Framework/Object/AbstractObject.php | 2 +- .../Grav/Framework/Object/ObjectInterface.php | 44 +------------- .../Object/Storage/FilesystemStorage.php | 18 +++--- .../Object/Storage/StorageInterface.php | 2 +- .../Object/StoredObjectInterface.php | 57 +++++++++++++++++++ 6 files changed, 73 insertions(+), 52 deletions(-) create mode 100644 system/src/Grav/Framework/Object/StoredObjectInterface.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 9bd44a21b..0286b81a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ # Feature/Objects Branch -## xx/xx/2017 +## mm/dd/2017 1. [](#new) * Added `Pages::baseUrl()`, `Pages::homeUrl()` and `Pages::url()` functions diff --git a/system/src/Grav/Framework/Object/AbstractObject.php b/system/src/Grav/Framework/Object/AbstractObject.php index 8d3b3d180..097edccd9 100644 --- a/system/src/Grav/Framework/Object/AbstractObject.php +++ b/system/src/Grav/Framework/Object/AbstractObject.php @@ -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; diff --git a/system/src/Grav/Framework/Object/ObjectInterface.php b/system/src/Grav/Framework/Object/ObjectInterface.php index ac5e29d39..b4ed09ef3 100644 --- a/system/src/Grav/Framework/Object/ObjectInterface.php +++ b/system/src/Grav/Framework/Object/ObjectInterface.php @@ -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. * diff --git a/system/src/Grav/Framework/Object/Storage/FilesystemStorage.php b/system/src/Grav/Framework/Object/Storage/FilesystemStorage.php index 196be8f7c..895c975f2 100644 --- a/system/src/Grav/Framework/Object/Storage/FilesystemStorage.php +++ b/system/src/Grav/Framework/Object/Storage/FilesystemStorage.php @@ -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}"; diff --git a/system/src/Grav/Framework/Object/Storage/StorageInterface.php b/system/src/Grav/Framework/Object/Storage/StorageInterface.php index a05e38740..e6d781eba 100644 --- a/system/src/Grav/Framework/Object/Storage/StorageInterface.php +++ b/system/src/Grav/Framework/Object/Storage/StorageInterface.php @@ -29,7 +29,7 @@ interface StorageInterface /** * @param string $key * @param array $data - * @return mixed + * @return string */ public function save($key, array $data); diff --git a/system/src/Grav/Framework/Object/StoredObjectInterface.php b/system/src/Grav/Framework/Object/StoredObjectInterface.php new file mode 100644 index 000000000..ee436205c --- /dev/null +++ b/system/src/Grav/Framework/Object/StoredObjectInterface.php @@ -0,0 +1,57 @@ +