mirror of
https://github.com/getgrav/grav.git
synced 2026-03-05 12:01:37 +01:00
Implement Object\FilesystemStorage class
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
namespace Grav\Common\Object;
|
||||
|
||||
interface ObjectInterface extends \ArrayAccess
|
||||
interface ObjectInterface extends \ArrayAccess, \JsonSerializable
|
||||
{
|
||||
/**
|
||||
* Returns the global instance to the object.
|
||||
|
||||
@@ -1,48 +1,95 @@
|
||||
<?php
|
||||
namespace Grav\Common\Object\Storage;
|
||||
|
||||
use Grav\Common\Object\AbstractObject;
|
||||
use Grav\Common\Grav;
|
||||
use RocketTheme\Toolbox\File\FileInterface;
|
||||
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
|
||||
|
||||
class FilesystemStorage implements StorageInterface
|
||||
{
|
||||
/**
|
||||
* @param array $keys
|
||||
* @var string
|
||||
*/
|
||||
protected $path;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $type;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $extension;
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param string $extension
|
||||
* @param string $type
|
||||
*/
|
||||
public function __construct($path, $type = 'Grav\\Common\\File\\CompiledJsonFile', $extension = '.json')
|
||||
{
|
||||
$this->path = $path;
|
||||
$this->type = $type;
|
||||
$this->extension = $extension;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return array
|
||||
*/
|
||||
public function load(array $keys)
|
||||
public function load($key)
|
||||
{
|
||||
// TODO
|
||||
return [];
|
||||
if ($key === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$file = $this->getFile($key);
|
||||
$content = (array)$file->content();
|
||||
$file->free();
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AbstractObject $object
|
||||
* @return string|int Id
|
||||
* @param string $key
|
||||
* @param array $data
|
||||
* @return string
|
||||
*/
|
||||
public function save(AbstractObject $object)
|
||||
public function save($key, array $data)
|
||||
{
|
||||
// TODO
|
||||
return 'xxx';
|
||||
$file = $this->getFile($key);
|
||||
$file->save($data);
|
||||
$file->free();
|
||||
|
||||
return $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AbstractObject $object
|
||||
* @param string $key
|
||||
* @return bool
|
||||
*/
|
||||
public function delete(AbstractObject $object)
|
||||
public function delete($key)
|
||||
{
|
||||
// TODO
|
||||
return false;
|
||||
$file = $this->getFile($key);
|
||||
$result = $file->delete();
|
||||
$file->free();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|int[]|string[] $list
|
||||
* @param string[] $list
|
||||
* @return array
|
||||
*/
|
||||
public function loadList(array $list)
|
||||
{
|
||||
// TODO
|
||||
return [];
|
||||
$results = [];
|
||||
foreach ($list as $id) {
|
||||
$results[$id] = $this->load(['id' => $id]);
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,11 +104,32 @@ class FilesystemStorage implements StorageInterface
|
||||
|
||||
/**
|
||||
* @param array $query
|
||||
* @return array|int[]|string[]
|
||||
* @return string[]
|
||||
*/
|
||||
public function find(array $query)
|
||||
public function find(array $query, $start = 0, $limit = 0)
|
||||
{
|
||||
// TODO
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return FileInterface
|
||||
*/
|
||||
protected function getFile($key)
|
||||
{
|
||||
if ($key === null) {
|
||||
throw new \RuntimeException('Id not defined');
|
||||
}
|
||||
|
||||
$filename = "{$this->path}/{$key}{$this->extension}";
|
||||
|
||||
/** @var UniformResourceLocator $locator */
|
||||
$locator = Grav::instance()['locator'];
|
||||
|
||||
/** @var FileInterface $type */
|
||||
$type = $this->type;
|
||||
|
||||
return $type::instance($locator->findResource($filename, true) ?: $locator->findResource($filename, true, true));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,30 +1,29 @@
|
||||
<?php
|
||||
namespace Grav\Common\Object\Storage;
|
||||
|
||||
use Grav\Common\Object\AbstractObject;
|
||||
|
||||
interface StorageInterface
|
||||
{
|
||||
/**
|
||||
* @param array $keys
|
||||
* @param string $key
|
||||
* @return array
|
||||
*/
|
||||
public function load(array $keys);
|
||||
public function load($key);
|
||||
|
||||
/**
|
||||
* @param AbstractObject $object
|
||||
* @return string Id
|
||||
* @param string $key
|
||||
* @param array $data
|
||||
* @return mixed
|
||||
*/
|
||||
public function save(AbstractObject $object);
|
||||
public function save($key, array $data);
|
||||
|
||||
/**
|
||||
* @param AbstractObject $object
|
||||
* @param string $key
|
||||
* @return bool
|
||||
*/
|
||||
public function delete(AbstractObject $object);
|
||||
public function delete($key);
|
||||
|
||||
/**
|
||||
* @param array|string[] $list
|
||||
* @param string[] $list
|
||||
* @return array
|
||||
*/
|
||||
public function loadList(array $list);
|
||||
@@ -39,7 +38,7 @@ interface StorageInterface
|
||||
* @param array $query
|
||||
* @param int $start
|
||||
* @param int $limit
|
||||
* @return array|string[]
|
||||
* @return string[]
|
||||
*/
|
||||
public function find(array $query, $start, $limit);
|
||||
public function find(array $query, $start = 0, $limit = 0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user