Flex objects no longer return temporary key if they do not have one; empty key is returned instead

This commit is contained in:
Matias Griese
2019-07-16 17:39:58 +03:00
parent ee53e1be6e
commit 3d767a4d25
7 changed files with 47 additions and 23 deletions

View File

@@ -9,6 +9,7 @@
* Better support for Symfony local server `symfony server:start`
* Make `Route` objects immutable
* `FlexDirectory::getObject()` can now be called without any parameters to create a new object
* Flex objects no longer return temporary key if they do not have one; empty key is returned instead
1. [](#bugfix)
* Fixed `Form` not to use deleted flash object until the end of the request fixing issues with reset
* Fixed `FlexForm` to allow multiple form instances with non-existing objects

View File

@@ -301,16 +301,19 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface
$debugger = $grav['debugger'];
$debugger->startTimer('flex-collection-' . ($debugKey = uniqid($type, false)), 'Render Collection ' . $type . ' (' . $layout . ')');
$cache = $key = null;
$key = null;
foreach ($context as $value) {
if (!\is_scalar($value)) {
$key = false;
break;
}
}
if ($key !== false) {
$key = md5($this->getCacheKey() . '.' . $layout . json_encode($context));
$cache = $this->getCache('render');
} else {
$cache = null;
}
try {
@@ -331,9 +334,9 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface
}
if (!$block) {
$block = HtmlBlock::create($key);
$block = HtmlBlock::create($key ?: null);
$block->setChecksum($checksum);
if ($key === false) {
if (!$key) {
$block->disableCache();
}

View File

@@ -37,7 +37,7 @@ class FlexFormFlash extends FormFlash
if ($object) {
$serialized['object'] = [
'type' => $object->getFlexType(),
'key' => $object->hasKey() ? $object->getKey() : null,
'key' => $object->getKey() ?: null,
'storage_key' => $object->exists() ? $object->getStorageKey() : null,
'timestamp' => $object->getTimestamp(),
'serialized' => $object->jsonSerialize()

View File

@@ -171,7 +171,7 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
*/
public function getCacheKey(): string
{
return $this->getTypePrefix() . $this->getFlexType() . '.' . $this->getStorageKey();
return $this->hasKey() ? $this->getTypePrefix() . $this->getFlexType() . '.' . $this->getKey() : '';
}
/**
@@ -213,7 +213,7 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
*/
public function getKey()
{
return $this->_key ?: $this->getFlexType() . '@@' . spl_object_hash($this);
return (string)$this->_key;
}
/**
@@ -222,7 +222,13 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
*/
public function getFlexKey(): string
{
return $this->_storage['flex_key'] ?? $this->_flexDirectory->getFlexType() . '.obj:' . $this->getStorageKey();
$key = $this->_storage['flex_key'] ?? null;
if (!$key && $key = $this->getStorageKey()) {
$key = $this->_flexDirectory->getFlexType() . '.obj:' . $key;
}
return (string)$key;
}
/**
@@ -231,7 +237,7 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
*/
public function getStorageKey(): string
{
return $this->_storage['storage_key'] ?? $this->getTypePrefix() . $this->getFlexType() . '@@' . spl_object_hash($this);
return (string)($this->_storage['storage_key'] ?? null);
}
/**
@@ -373,7 +379,7 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
*/
public function setStorageKey($key = null)
{
$this->_storage['storage_key'] = $key;
$this->_storage['storage_key'] = $key ?? '';
return $this;
}
@@ -407,20 +413,28 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
$debugger = $grav['debugger'];
$debugger->startTimer('flex-object-' . ($debugKey = uniqid($type, false)), 'Render Object ' . $type . ' (' . $layout . ')');
$cache = $key = null;
foreach ($context as $value) {
if (!\is_scalar($value)) {
$key = false;
$key = $this->getCacheKey();
// Disable caching if context isn't all scalars.
if ($key) {
foreach ($context as $value) {
if (!\is_scalar($value)) {
$key = '';
break;
}
}
}
if ($key !== false) {
$key = md5($this->getCacheKey() . '.' . $layout . json_encode($context));
if ($key) {
// Create a new key which includes layout and context.
$key = md5($key . '.' . $layout . json_encode($context));
$cache = $this->getCache('render');
} else {
$cache = null;
}
try {
$data = $cache && $key ? $cache->get($key) : null;
$data = $cache ? $cache->get($key) : null;
$block = $data ? HtmlBlock::fromArray($data) : null;
} catch (InvalidArgumentException $e) {
@@ -441,7 +455,7 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
if (!$block) {
$block = HtmlBlock::create($key ?: null);
$block->setChecksum($checksum);
if ($key === false) {
if (!$cache) {
$block->disableCache();
}
@@ -463,7 +477,7 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
$block->setContent($output);
try {
$cache && $key && $block->isCached() && $cache->set($key, $block->toArray());
$cache && $block->isCached() && $cache->set($key, $block->toArray());
} catch (InvalidArgumentException $e) {
$debugger->addException($e);
}
@@ -567,7 +581,9 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
{
$this->triggerEvent('onBeforeSave');
$result = $this->getFlexDirectory()->getStorage()->replaceRows([$this->getStorageKey() => $this->prepareStorage()]);
$result = $this->getFlexDirectory()->getStorage()->replaceRows(
[$this->getStorageKey() ?: '@@' . spl_object_hash($this) => $this->prepareStorage()]
);
$value = reset($result);
$storageKey = (string)key($result);
@@ -615,6 +631,10 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
*/
public function delete()
{
if (!$this->exists()) {
return $this;
}
$this->triggerEvent('onBeforeDelete');
$this->getFlexDirectory()->getStorage()->deleteRows([$this->getStorageKey() => $this->prepareStorage()]);

View File

@@ -149,6 +149,6 @@ abstract class AbstractFilesystemStorage implements FlexStorageInterface
*/
protected function validateKey(string $key): bool
{
return (bool) preg_match('/^[^\\/\\?\\*:;{}\\\\\\n]+$/u', $key);
return $key && (bool) preg_match('/^[^\\/\\?\\*:;{}\\\\\\n]+$/u', $key);
}
}

View File

@@ -179,7 +179,7 @@ class FolderStorage extends AbstractFilesystemStorage
$list = [];
foreach ($rows as $key => $row) {
$key = (string)$key;
if (strpos($key, '@@')) {
if (strpos($key, '@@') !== false) {
$key = $this->getNewKey();
}
$path = $this->getPathFromKey($key);

View File

@@ -192,7 +192,7 @@ class SimpleStorage extends AbstractFilesystemStorage
$list = [];
foreach ($rows as $key => $row) {
if (strpos($key, '@@')) {
if (strpos($key, '@@') !== false) {
$key = $this->getNewKey();
}
$this->data[$key] = $list[$key] = $row;
@@ -255,7 +255,7 @@ class SimpleStorage extends AbstractFilesystemStorage
return sprintf('%s/%s/%s', $this->dataFolder, basename($this->dataPattern, $this->dataFormatter->getDefaultFileExtension()), $key);
}
protected function save() : void
protected function save(): void
{
if (null === $this->data) {
$this->buildIndex();