Make flex storage classes to return storage metadata along with the data

This commit is contained in:
Matias Griese
2019-07-30 13:51:15 +03:00
parent a6032af594
commit e8529e7d0b
4 changed files with 55 additions and 7 deletions

View File

@@ -100,6 +100,11 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
{
$this->_flexDirectory = $directory;
if (isset($elements['__META'])) {
$this->setStorage($elements['__META']);
unset($elements['__META']);
}
if ($validate) {
$blueprint = $this->getFlexDirectory()->getBlueprint();
@@ -813,6 +818,7 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
{
$this->_flexDirectory = $directory;
}
/**
* @param array $storage
*/

View File

@@ -25,7 +25,7 @@ class FileStorage extends FolderStorage
*/
public function __construct(array $options)
{
$this->dataPattern = '{FOLDER}/{KEY}';
$this->dataPattern = '{FOLDER}/{KEY}{EXT}';
if (!isset($options['formatter']) && isset($options['pattern'])) {
$options['formatter'] = $this->detectDataFormatter($options['pattern']);

View File

@@ -95,6 +95,7 @@ class FolderStorage extends AbstractFilesystemStorage
$path = $this->getPathFromKey($key);
$file = $this->getFile($path);
$list[$key] = $this->saveFile($file, $row);
$list[$key]['__META'] = $this->getObjectMeta($key, true);
}
return $list;
@@ -117,6 +118,7 @@ class FolderStorage extends AbstractFilesystemStorage
$path = $this->getPathFromKey($key);
$file = $this->getFile($path);
$list[$key] = $this->loadFile($file);
$list[$key]['__META'] = $this->getObjectMeta($key);
}
if (null !== $fetched) {
$fetched[$key] = $list[$key];
@@ -145,6 +147,7 @@ class FolderStorage extends AbstractFilesystemStorage
$path = $this->getPathFromKey($key);
$file = $this->getFile($path);
$list[$key] = $this->saveFile($file, $row);
$list[$key]['__META'] = $this->getObjectMeta($key, true);
}
}
@@ -198,6 +201,7 @@ class FolderStorage extends AbstractFilesystemStorage
$path = $this->getPathFromKey($key);
$file = $this->getFile($path);
$list[$key] = $this->saveFile($file, $row);
$list[$key]['__META'] = $this->getObjectMeta($key, true);
}
return $list;
@@ -331,6 +335,7 @@ class FolderStorage extends AbstractFilesystemStorage
protected function saveFile(File $file, array $data): array
{
try {
unset($data['__META'], $data['__error']);
$file->save($data);
/** @var UniformResourceLocator $locator */
@@ -458,9 +463,10 @@ class FolderStorage extends AbstractFilesystemStorage
/**
* @param string $key
* @param bool $reload
* @return array
*/
protected function getObjectMeta(string $key): array
protected function getObjectMeta(string $key, bool $reload = false): array
{
$filename = $this->getPathFromKey($key);
$modified = is_file($filename) ? filemtime($filename) : 0;

View File

@@ -111,7 +111,10 @@ class SimpleStorage extends AbstractFilesystemStorage
$list = [];
foreach ($rows as $key => $row) {
$key = $this->getNewKey();
$this->data[$key] = $list[$key] = $row;
$this->data[$key] = $row;
$list[$key] = $row;
$list[$key]['__META'] = $this->getObjectMeta($key, true);
}
if ($list) {
@@ -136,7 +139,12 @@ class SimpleStorage extends AbstractFilesystemStorage
if (null === $row || (!\is_object($row) && !\is_array($row))) {
// Only load rows which haven't been loaded before.
$key = (string)$key;
$list[$key] = $this->hasKey($key) ? $this->data[$key] : null;
if (!$this->hasKey($key)) {
$list[$key] = null;
} else {
$list[$key] = $this->data[$key];
$list[$key]['__META'] = $this->getObjectMeta($key);
}
if (null !== $fetched) {
$fetched[$key] = $list[$key];
}
@@ -163,7 +171,10 @@ class SimpleStorage extends AbstractFilesystemStorage
foreach ($rows as $key => $row) {
$key = (string)$key;
if ($this->hasKey($key)) {
$this->data[$key] = $list[$key] = $row;
$this->data[$key] = $row;
$list[$key] = $row;
$list[$key]['__META'] = $this->getObjectMeta($key, true);
}
}
@@ -189,6 +200,7 @@ class SimpleStorage extends AbstractFilesystemStorage
$key = (string)$key;
if ($this->hasKey($key)) {
unset($this->data[$key]);
$list[$key] = $row;
}
}
@@ -215,7 +227,10 @@ class SimpleStorage extends AbstractFilesystemStorage
if (strpos($key, '@@') !== false) {
$key = $this->getNewKey();
}
$this->data[$key] = $list[$key] = $row;
$this->data[$key] = $row;$list[$key]['__META'] = $this->getObjectMeta($key, true);
$list[$key] = $row;
$list[$key]['__META'] = $this->getObjectMeta($key, true);
}
if ($list) {
@@ -225,6 +240,26 @@ class SimpleStorage extends AbstractFilesystemStorage
return $list;
}
/**
* @param string $src
* @param string $dst
* @return bool
*/
public function copyRow(string $src, string $dst): bool
{
if ($this->hasKey($dst)) {
throw new \RuntimeException("Cannot copy object: key '{$dst}' is already taken");
}
if (!$this->hasKey($src)) {
return false;
}
$this->data[$dst] = $this->data[$src];
return true;
}
/**
* {@inheritdoc}
* @see FlexStorageInterface::renameRow()
@@ -322,9 +357,10 @@ class SimpleStorage extends AbstractFilesystemStorage
/**
* @param string $key
* @param bool $reload
* @return array
*/
protected function getObjectMeta(string $key): array
protected function getObjectMeta(string $key, bool $reload = false): array
{
$modified = isset($this->data[$key]) ? $this->modified : 0;