Improve new flex object saving and storage

This commit is contained in:
Matias Griese
2018-11-13 20:13:32 +02:00
parent ff5aa8a0ac
commit 44db0245a7
3 changed files with 15 additions and 5 deletions

View File

@@ -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;
}

View File

@@ -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']);

View File

@@ -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}
@@ -198,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;
@@ -220,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));
}
/**
@@ -379,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);
}
}