From 603bb6c8786137e877d2f2eb2e52c4d019f9d4c6 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Thu, 5 Sep 2019 16:54:59 +0300 Subject: [PATCH] Make it possible to save multiple variations of the Flex Object --- system/src/Grav/Framework/Flex/FlexObject.php | 20 ++++++++++--- .../Framework/Flex/Storage/FolderStorage.php | 30 +++++++++++++++---- .../Framework/Flex/Storage/SimpleStorage.php | 16 +++++++++- 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/system/src/Grav/Framework/Flex/FlexObject.php b/system/src/Grav/Framework/Flex/FlexObject.php index 412f70f3e..826769beb 100644 --- a/system/src/Grav/Framework/Flex/FlexObject.php +++ b/system/src/Grav/Framework/Flex/FlexObject.php @@ -618,12 +618,24 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface $this->triggerEvent('onBeforeSave'); $storage = $this->getFlexDirectory()->getStorage(); - - $key = $this->getStorageKey() ?: '@@' . spl_object_hash($this); $meta = $this->getMetaData(); /** @var string|null $origKey */ $origKey = $meta['storage_key'] ?? null; + $storageKey = $this->getStorageKey() ?: '@@' . spl_object_hash($this); + + if (method_exists($storage, 'parseKey')) { + if (null !== $origKey) { + $origParts =$storage->parseKey($origKey); + $origKey = $origParts['key']; + + } + $keyParts = $storage->parseKey($storageKey); + $key = $keyParts['key']; + } else { + $key = $storageKey; + } + if (null !== $origKey && $key !== $origKey) { if (!empty($meta['copy'])) { $storage->copyRow($origKey, $key); @@ -632,7 +644,7 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface } } - $result = $storage->replaceRows([$key => $this->prepareStorage()]); + $result = $storage->replaceRows([$storageKey => $this->prepareStorage()]); $value = reset($result); $meta = $value['__META'] ?? null; @@ -652,7 +664,7 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface // Make sure that the object exists before continuing (just in case). if (!$this->exists()) { - throw new \RuntimeException('Saving failed: Object does not exist!'); + throw new \RuntimeException('Save failed: Object does not exist!'); } if (method_exists($this, 'saveUpdatedMedia')) { diff --git a/system/src/Grav/Framework/Flex/Storage/FolderStorage.php b/system/src/Grav/Framework/Flex/Storage/FolderStorage.php index c5642f091..07338053b 100644 --- a/system/src/Grav/Framework/Flex/Storage/FolderStorage.php +++ b/system/src/Grav/Framework/Flex/Storage/FolderStorage.php @@ -254,10 +254,11 @@ class FolderStorage extends AbstractFilesystemStorage if (null === $key || $key === '') { $path = $this->dataFolder; } else { + $parts = $this->parseKey($key, false); $options = [ $this->dataFolder, // {FOLDER} - $key, // {KEY} - \mb_substr($key, 0, 2), // {KEY:2} + $parts['key'], // {KEY} + $parts['key:2'], // {KEY:2} '***', // {FILE} '***' // {EXT} ]; @@ -285,17 +286,36 @@ class FolderStorage extends AbstractFilesystemStorage */ public function getPathFromKey(string $key): string { + $parts = $this->parseKey($key); $options = [ $this->dataFolder, // {FOLDER} - $key, // {KEY} - \mb_substr($key, 0, 2), // {KEY:2} - $this->dataFile, // {FILE} + $parts['key'], // {KEY} + $parts['key:2'], // {KEY:2} + $parts['file'], // {FILE} $this->dataExt // {EXT} ]; return sprintf($this->dataPattern, ...$options); } + /** + * @param string $key + * @param bool $variations + * @return array + */ + public function parseKey(string $key, bool $variations = true): array + { + $keys = [ + 'key' => $key, + 'key:2' => \mb_substr($key, 0, 2), + ]; + if ($variations) { + $keys['file'] = $this->dataFile; + } + + return $keys; + } + /** * Get key from the filesystem path. * diff --git a/system/src/Grav/Framework/Flex/Storage/SimpleStorage.php b/system/src/Grav/Framework/Flex/Storage/SimpleStorage.php index d7c716a68..3f0b34b2a 100644 --- a/system/src/Grav/Framework/Flex/Storage/SimpleStorage.php +++ b/system/src/Grav/Framework/Flex/Storage/SimpleStorage.php @@ -316,7 +316,21 @@ class SimpleStorage extends AbstractFilesystemStorage */ public function getMediaPath(string $key = null): string { - return sprintf('%s/%s/%s', $this->dataFolder, basename($this->dataPattern, $this->dataFormatter->getDefaultFileExtension()), $key); + $parts = $this->parseKey($key); + + return sprintf('%s/%s/%s', $this->dataFolder, basename($this->dataPattern, $this->dataFormatter->getDefaultFileExtension()), $parts['key']); + } + + /** + * @param string $key + * @param bool $variations + * @return array + */ + public function parseKey(string $key, bool $variations = true): array + { + return [ + 'key' => $key, + ]; } protected function save(): void