Make it possible to save multiple variations of the Flex Object

This commit is contained in:
Matias Griese
2019-09-05 16:54:59 +03:00
parent dac3e57fd4
commit 603bb6c878
3 changed files with 56 additions and 10 deletions

View File

@@ -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')) {

View File

@@ -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.
*

View File

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