diff --git a/CHANGELOG.md b/CHANGELOG.md index b10764a84..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 @@ -101,6 +107,7 @@ 1. [](#improved) * Propogate error code between 400 and 600 for production sites [#2181](https://github.com/getgrav/grav/pull/2181) 1. [](#bugfix) + * Remove hardcoded `302` when redirecting trailing slash [#2155](https://github.com/getgrav/grav/pull/2155) # v1.5.4 ## 11/05/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..76432cdae 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(); } /** @@ -422,7 +422,7 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface */ public function save() { - $this->getFlexDirectory()->getStorage()->replaceRows([$this->getStorageKey() => $this->prepareStorage()]); + $result = $this->getFlexDirectory()->getStorage()->replaceRows([$this->getStorageKey() => $this->prepareStorage()]); try { $this->getFlexDirectory()->clearCache(); @@ -437,6 +437,12 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface // Caching failed, but we can ignore that for now. } + $value = reset($result); + $storageKey = key($result); + if ($value && $storageKey) { + $this->setStorageKey($storageKey); + } + return $this; } 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/FileStorage.php b/system/src/Grav/Framework/Flex/Storage/FileStorage.php index caac2ae9a..01390e9e2 100644 --- a/system/src/Grav/Framework/Flex/Storage/FileStorage.php +++ b/system/src/Grav/Framework/Flex/Storage/FileStorage.php @@ -22,7 +22,7 @@ class FileStorage extends FolderStorage */ public function __construct(array $options) { - $this->dataPattern = '%1s/%2s'; + $this->dataPattern = '{FOLDER}/{KEY}'; if (!isset($options['formatter']) && isset($options['pattern'])) { $options['formatter'] = $this->detectDataFormatter($options['pattern']); diff --git a/system/src/Grav/Framework/Flex/Storage/FolderStorage.php b/system/src/Grav/Framework/Flex/Storage/FolderStorage.php index 82d63ff72..908c62365 100644 --- a/system/src/Grav/Framework/Flex/Storage/FolderStorage.php +++ b/system/src/Grav/Framework/Flex/Storage/FolderStorage.php @@ -26,7 +26,9 @@ class FolderStorage extends AbstractFilesystemStorage /** @var string */ protected $dataFolder; /** @var string */ - protected $dataPattern = '%1s/%2s/item'; + protected $dataPattern = '{FOLDER}/{KEY}/item'; + /** @var bool */ + protected $indexed; /** * {@inheritdoc} @@ -64,7 +66,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 +95,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 +121,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 +140,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 +165,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); @@ -183,7 +200,7 @@ class FolderStorage extends AbstractFilesystemStorage if (null === $key) { $path = $this->dataFolder; } else { - $path = sprintf($this->dataPattern, $this->dataFolder, $key); + $path = sprintf($this->dataPattern, $this->dataFolder, $key, substr($key, 0, 2)); } return $path; @@ -205,7 +222,7 @@ class FolderStorage extends AbstractFilesystemStorage */ public function getPathFromKey(string $key) : string { - return sprintf($this->dataPattern, $this->dataFolder, $key); + return sprintf($this->dataPattern, $this->dataFolder, $key, substr($key, 0, 2)); } /** @@ -364,8 +381,10 @@ class FolderStorage extends AbstractFilesystemStorage { $extension = $this->dataFormatter->getDefaultFileExtension(); $pattern = !empty($options['pattern']) ? $options['pattern'] : $this->dataPattern; + $pattern = preg_replace(['/{FOLDER}/', '/{KEY}/', '/{KEY:2}/'], ['%1$s', '%2$s', '%3$s'], $pattern); $this->dataPattern = \dirname($pattern) . '/' . basename($pattern, $extension) . $extension; $this->dataFolder = $options['folder']; + $this->indexed = (bool)($options['indexed'] ?? false); } } 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); } /**