Improve Flex storage

This commit is contained in:
Matias Griese
2018-11-13 18:00:18 +02:00
parent 8c0dd6a8d1
commit ff5aa8a0ac
7 changed files with 39 additions and 26 deletions

View File

@@ -1,3 +1,9 @@
# v1.6.0-beta.7
## mm/dd/2018
1. [](#improved)
* Improve Flex storage
# v1.6.0-beta.6
## 11/12/2018

View File

@@ -222,7 +222,6 @@ class FlexDirectory implements FlexAuthorizeInterface
}
$object->save();
//$rows = $storage->updateRows([$newKey => $object->triggerEvent('onSave')->prepareStorage()]);
}
try {
@@ -235,13 +234,6 @@ class FlexDirectory implements FlexAuthorizeInterface
// Caching failed, but we can ignore that for now.
}
/** @var FlexObject $class */
//$class = $this->getObjectClass();
//
//$row = $object;
//$index = $class::createIndex([key($rows) => time()]);
//$object = $this->createObject($row, key($index), false);
return $object;
}

View File

@@ -218,7 +218,7 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
*/
public function getStorageKey()
{
return $this->_storageKey;
return $this->_storageKey ?? $this->__toString();
}
/**

View File

@@ -115,7 +115,7 @@ abstract class AbstractFilesystemStorage implements FlexStorageInterface
*/
protected function generateKey() : string
{
return Base32::encode(Utils::generateRandomString(10));
return substr(hash('sha256', random_bytes(32)), 0, 32);
}
/**

View File

@@ -64,7 +64,7 @@ class FolderStorage extends AbstractFilesystemStorage
*/
public function hasKey(string $key) : bool
{
return $key && file_exists($this->getPathFromKey($key));
return $key && !strpos($key, '@@') && file_exists($this->getPathFromKey($key));
}
/**
@@ -93,9 +93,13 @@ class FolderStorage extends AbstractFilesystemStorage
foreach ($rows as $key => $row) {
if (null === $row || (!\is_object($row) && !\is_array($row))) {
// Only load rows which haven't been loaded before.
$path = $this->getPathFromKey($key);
$file = $this->getFile($path);
$list[$key] = $this->hasKey($key) ? $this->loadFile($file) : null;
if (!$this->hasKey($key)) {
$list[$key] = null;
} else {
$path = $this->getPathFromKey($key);
$file = $this->getFile($path);
$list[$key] = $this->loadFile($file);
}
if (null !== $fetched) {
$fetched[$key] = $list[$key];
}
@@ -115,9 +119,13 @@ class FolderStorage extends AbstractFilesystemStorage
{
$list = [];
foreach ($rows as $key => $row) {
$path = $this->getPathFromKey($key);
$file = $this->getFile($path);
$list[$key] = $this->hasKey($key) ? $this->saveFile($file, $row) : null;
if (!$this->hasKey($key)) {
$list[$key] = null;
} else {
$path = $this->getPathFromKey($key);
$file = $this->getFile($path);
$list[$key] = $this->saveFile($file, $row);
}
}
return $list;
@@ -130,15 +138,19 @@ class FolderStorage extends AbstractFilesystemStorage
{
$list = [];
foreach ($rows as $key => $row) {
$path = $this->getPathFromKey($key);
$file = $this->getFile($path);
$list[$key] = $this->hasKey($key) ? $this->deleteFile($file) : null;
if (!$this->hasKey($key)) {
$list[$key] = null;
} else {
$path = $this->getPathFromKey($key);
$file = $this->getFile($path);
$list[$key] = $this->deleteFile($file);
$storage = $this->getStoragePath($key);
$media = $this->getMediaPath($key);
$storage = $this->getStoragePath($key);
$media = $this->getMediaPath($key);
$this->deleteFolder($storage, true);
$media && $this->deleteFolder($media, true);
$this->deleteFolder($storage, true);
$media && $this->deleteFolder($media, true);
}
}
return $list;
@@ -151,6 +163,9 @@ class FolderStorage extends AbstractFilesystemStorage
{
$list = [];
foreach ($rows as $key => $row) {
if (strpos($key, '@@')) {
$key = $this->getNewKey();
}
$path = $this->getPathFromKey($key);
$file = $this->getFile($path);
$list[$key] = $this->saveFile($file, $row);

View File

@@ -68,7 +68,7 @@ class SimpleStorage extends AbstractFilesystemStorage
*/
public function hasKey(string $key) : bool
{
return isset($this->data[$key]);
return $key && !strpos($key, '@@') && isset($this->data[$key]);
}
/**

View File

@@ -53,7 +53,7 @@ trait ObjectTrait
*/
public function getKey()
{
return $this->_key ?: $this->getType() . '@' . spl_object_hash($this);
return $this->_key ?: $this->getType() . '@@' . spl_object_hash($this);
}
/**