diff --git a/CHANGELOG.md b/CHANGELOG.md index 66cf7e863..9fb44a31b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/system/src/Grav/Framework/Flex/FlexDirectory.php b/system/src/Grav/Framework/Flex/FlexDirectory.php index d26003c8c..3c1b8e411 100644 --- a/system/src/Grav/Framework/Flex/FlexDirectory.php +++ b/system/src/Grav/Framework/Flex/FlexDirectory.php @@ -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; } diff --git a/system/src/Grav/Framework/Flex/FlexObject.php b/system/src/Grav/Framework/Flex/FlexObject.php index 682ddf4e4..da501a288 100644 --- a/system/src/Grav/Framework/Flex/FlexObject.php +++ b/system/src/Grav/Framework/Flex/FlexObject.php @@ -218,7 +218,7 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface */ public function getStorageKey() { - return $this->_storageKey; + return $this->_storageKey ?? $this->__toString(); } /** diff --git a/system/src/Grav/Framework/Flex/Storage/AbstractFilesystemStorage.php b/system/src/Grav/Framework/Flex/Storage/AbstractFilesystemStorage.php index 168384508..76dae57b9 100644 --- a/system/src/Grav/Framework/Flex/Storage/AbstractFilesystemStorage.php +++ b/system/src/Grav/Framework/Flex/Storage/AbstractFilesystemStorage.php @@ -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); } /** diff --git a/system/src/Grav/Framework/Flex/Storage/FolderStorage.php b/system/src/Grav/Framework/Flex/Storage/FolderStorage.php index 82d63ff72..b1d6fedb5 100644 --- a/system/src/Grav/Framework/Flex/Storage/FolderStorage.php +++ b/system/src/Grav/Framework/Flex/Storage/FolderStorage.php @@ -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); diff --git a/system/src/Grav/Framework/Flex/Storage/SimpleStorage.php b/system/src/Grav/Framework/Flex/Storage/SimpleStorage.php index cb651da00..03c395f40 100644 --- a/system/src/Grav/Framework/Flex/Storage/SimpleStorage.php +++ b/system/src/Grav/Framework/Flex/Storage/SimpleStorage.php @@ -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]); } /** diff --git a/system/src/Grav/Framework/Object/Base/ObjectTrait.php b/system/src/Grav/Framework/Object/Base/ObjectTrait.php index c4dfaa293..a6cacc901 100644 --- a/system/src/Grav/Framework/Object/Base/ObjectTrait.php +++ b/system/src/Grav/Framework/Object/Base/ObjectTrait.php @@ -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); } /**