diff --git a/CHANGELOG.md b/CHANGELOG.md index 0947d542a..5effbc549 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ * Exception will output a badly formatted line in `CsvFormatter::decode()` 1. [](#bugfix) * Fixed error when activating Flex Accounts in GRAV system configuration (PHP 7.1) + * Fixed Grav parameter handling in `RouteFactory::createFromString()` # v1.7.0-beta.8 ## 09/19/2019 diff --git a/system/src/Grav/Framework/Flex/FlexObject.php b/system/src/Grav/Framework/Flex/FlexObject.php index e6e9943a4..8f66615e5 100644 --- a/system/src/Grav/Framework/Flex/FlexObject.php +++ b/system/src/Grav/Framework/Flex/FlexObject.php @@ -618,32 +618,9 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface $this->triggerEvent('onBeforeSave'); $storage = $this->getFlexDirectory()->getStorage(); - $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); - } else { - $storage->renameRow($origKey, $key); - } - } - $result = $storage->replaceRows([$storageKey => $this->prepareStorage()]); $value = reset($result); @@ -652,9 +629,9 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface $this->_meta = $meta; } - $storageKey = $meta['storage_key'] ?? (string)key($result); if ($value) { - if ($storageKey) { + $storageKey = $meta['storage_key'] ?? (string)key($result); + if ($storageKey !== '') { $this->setStorageKey($storageKey); } diff --git a/system/src/Grav/Framework/Flex/Storage/AbstractFilesystemStorage.php b/system/src/Grav/Framework/Flex/Storage/AbstractFilesystemStorage.php index e49284b93..079851691 100644 --- a/system/src/Grav/Framework/Flex/Storage/AbstractFilesystemStorage.php +++ b/system/src/Grav/Framework/Flex/Storage/AbstractFilesystemStorage.php @@ -38,7 +38,7 @@ abstract class AbstractFilesystemStorage implements FlexStorageInterface /** * {@inheritdoc} - * @see FlexStorageInterface::hasKey() + * @see FlexStorageInterface::hasKeys() */ public function hasKeys(array $keys): array { @@ -58,6 +58,50 @@ abstract class AbstractFilesystemStorage implements FlexStorageInterface return $this->keyField; } + /** + * @param array $keys + * @param bool $includeParams + * @return string + */ + public function buildStorageKey(array $keys, bool $includeParams = true): string + { + $key = $keys['key'] ?? ''; + $params = $includeParams ? $this->buildStorageKeyParams($keys) : ''; + + return $params ? "{$key}|{$params}" : $key; + } + + /** + * @param array $keys + * @return string + */ + public function buildStorageKeyParams(array $keys): string + { + return ''; + } + + /** + * @param array $row + * @return array + */ + public function extractKeysFromRow(array $row): array + { + return [ + 'key' => $row[$this->keyField] ?? '' + ]; + } + + /** + * @param string $key + * @return array + */ + public function extractKeysFromStorageKey(string $key): array + { + return [ + 'key' => $key + ]; + } + protected function initDataFormatter($formatter): void { // Initialize formatter. diff --git a/system/src/Grav/Framework/Flex/Storage/FolderStorage.php b/system/src/Grav/Framework/Flex/Storage/FolderStorage.php index e42352b9b..3b4b4a9a3 100644 --- a/system/src/Grav/Framework/Flex/Storage/FolderStorage.php +++ b/system/src/Grav/Framework/Flex/Storage/FolderStorage.php @@ -323,20 +323,10 @@ class FolderStorage extends AbstractFilesystemStorage * Prepares the row for saving and returns the storage key for the record. * * @param array $row - * @param string $key - * @return string */ - protected function prepareRow(array &$row, string $key = '@@'): string + protected function prepareRow(array &$row): void { - if (isset($row['storage_key'])) { - $key = $row['storage_key']; - unset($row['storage_key']); - } - if (strpos($key, '@@') !== false) { - $key = $this->getNewKey(); - } - - return $key; + unset($row[$this->keyField]); } /** @@ -369,10 +359,17 @@ class FolderStorage extends AbstractFilesystemStorage protected function saveRow(string $key, array $row): array { try { - $newKey = $this->prepareRow($row, $key); + if (isset($row[$this->keyField])) { + $key = $row[$this->keyField]; + } + if (strpos($key, '@@') !== false) { + $key = $this->getNewKey(); + } + + $this->prepareRow($row); unset($row['__META'], $row['__ERROR']); - $path = $this->getPathFromKey($newKey); + $path = $this->getPathFromKey($key); $file = $this->getFile($path); $file->save($row); @@ -386,7 +383,7 @@ class FolderStorage extends AbstractFilesystemStorage throw new \RuntimeException(sprintf('Flex saveFile(%s): %s', $file->filename(), $e->getMessage())); } - $row['__META'] = $this->getObjectMeta($newKey, true); + $row['__META'] = $this->getObjectMeta($key, true); return $row; } diff --git a/system/src/Grav/Framework/Flex/Storage/SimpleStorage.php b/system/src/Grav/Framework/Flex/Storage/SimpleStorage.php index c28e7e423..0b2815347 100644 --- a/system/src/Grav/Framework/Flex/Storage/SimpleStorage.php +++ b/system/src/Grav/Framework/Flex/Storage/SimpleStorage.php @@ -47,6 +47,7 @@ class SimpleStorage extends AbstractFilesystemStorage $this->dataPattern = basename($pattern, $extension) . $extension; $this->dataFolder = \dirname($options['folder']); + $this->keyField = $options['key'] ?? 'storage_key'; // Make sure that the data folder exists. if (!file_exists($this->dataFolder)) { @@ -70,7 +71,7 @@ class SimpleStorage extends AbstractFilesystemStorage $list = []; foreach ($keys as $key) { - $list[$key] = $this->getObjectMeta($key); + $list[$key] = $this->getObjectMeta((string)$key); } return $list; @@ -316,7 +317,7 @@ class SimpleStorage extends AbstractFilesystemStorage */ public function getMediaPath(string $key = null): ?string { - $parts = $this->parseKey($key); + $parts = $this->extractKeysFromStorageKey($key); return sprintf('%s/%s/%s', $this->dataFolder, basename($this->dataPattern, $this->dataFormatter->getDefaultFileExtension()), $parts['key']); } diff --git a/system/src/Grav/Framework/Route/RouteFactory.php b/system/src/Grav/Framework/Route/RouteFactory.php index ea1cd93f9..50a9b720d 100644 --- a/system/src/Grav/Framework/Route/RouteFactory.php +++ b/system/src/Grav/Framework/Route/RouteFactory.php @@ -43,8 +43,8 @@ class RouteFactory 'grav' => [ 'root' => self::$root, 'language' => self::$language, - 'route' => $path, - 'params' => '' + 'route' => static::trimParams($path), + 'params' => static::getParams($path) ], ]; return new Route($parts); @@ -132,6 +132,26 @@ class RouteFactory return $params !== '' ? static::parseParams($params) : []; } + public static function trimParams($str) + { + if ($str === '') { + return $str; + } + + $delimiter = self::$delimiter; + + /** @var array $params */ + $params = explode('/', $str); + $list = []; + foreach ($params as $param) { + if (mb_strpos($param, $delimiter) === false) { + $list[] = $param; + } + } + + return implode('/', $list); + } + /** * @param string $str * @return array @@ -146,16 +166,17 @@ class RouteFactory /** @var array $params */ $params = explode('/', $str); + $list = []; foreach ($params as &$param) { /** @var array $parts */ $parts = explode($delimiter, $param, 2); if (isset($parts[1])) { $var = rawurldecode($parts[0]); $val = rawurldecode($parts[1]); - $param = [$var => $val]; + $list[$var] = $val; } } - return $params; + return $list; } }