From 55ef175b86e367ba0393b54ee216dd7c6d9965fa Mon Sep 17 00:00:00 2001 From: Awilum Date: Sat, 2 Jan 2021 15:06:08 +0300 Subject: [PATCH] feat(entries): simplify functionality to work with online entries storage. #536 --- src/flextype/Foundation/Entries/Entries.php | 202 ++++++------------ .../Entries/Fields/CreatedAtField.php | 10 +- .../Entries/Fields/CreatedByField.php | 4 +- .../Entries/Fields/EntriesField.php | 12 +- .../Foundation/Entries/Fields/IdField.php | 4 +- .../Foundation/Entries/Fields/MediaField.php | 24 +-- .../Entries/Fields/ModifiedAtField.php | 4 +- .../Entries/Fields/ParsersField.php | 28 +-- .../Entries/Fields/PublishedAtField.php | 10 +- .../Entries/Fields/PublishedByField.php | 4 +- .../Entries/Fields/RegistryField.php | 8 +- .../Entries/Fields/RoutableField.php | 12 +- .../Foundation/Entries/Fields/SlugField.php | 6 +- .../Foundation/Entries/Fields/UuidField.php | 4 +- .../Entries/Fields/VisibilityField.php | 12 +- tests/Foundation/Entries/EntriesTest.php | 25 ++- 16 files changed, 150 insertions(+), 219 deletions(-) diff --git a/src/flextype/Foundation/Entries/Entries.php b/src/flextype/Foundation/Entries/Entries.php index 53befecf..3d79ebd2 100755 --- a/src/flextype/Foundation/Entries/Entries.php +++ b/src/flextype/Foundation/Entries/Entries.php @@ -30,41 +30,28 @@ class Entries * Used for storing current requested entries data * and allow to change them on fly. * - * @var array + * @var Arrays * @access private */ - private array $storage = [ - 'fetch' => [ - 'id' => '', - 'data' => [], - 'options' => [ - 'find' => [], - 'filter' => [], - ], - ], - 'create' => [ - 'id' => '', - 'data' => [], - ], - 'update' => [ - 'id' => '', - 'data' => [], - ], - 'delete' => [ - 'id' => '', - ], - 'copy' => [ - 'id' => '', - 'newID' => '', - ], - 'move' => [ - 'id' => '', - 'newID' => '', - ], - 'has' => [ - 'id' => '', - ], - ]; + private Arrays $storage; + + /** + * __construct + */ + public function __construct() + { + $this->storage = arrays(); + } + + /** + * Get Entries Storage + * + * @return Arrays + */ + public function storage(): Arrays + { + return $this->storage; + } /** * Fetch. @@ -79,9 +66,9 @@ class Entries public function fetch(string $id, array $options = []): Arrays { // Store data - $this->storage['fetch']['id'] = $id; - $this->storage['fetch']['options'] = $options; - $this->storage['fetch']['data'] = []; + $this->storage()->set('fetch.id', $id); + $this->storage()->set('fetch.options', $options); + $this->storage()->set('fetch.data', []); // Run event: onEntriesFetch flextype('emitter')->emit('onEntriesFetch'); @@ -90,34 +77,34 @@ class Entries $single = function ($id, $options) { // Store data - $this->setStorage('fetch.id', $id); - $this->setStorage('fetch.options', $options); - $this->setStorage('fetch.data', []); + $this->storage()->set('fetch.id', $id); + $this->storage()->set('fetch.options', $options); + $this->storage()->set('fetch.data', []); // Run event: onEntriesFetchSingle flextype('emitter')->emit('onEntriesFetchSingle'); // Get Cache ID for current requested entry - $entryCacheID = $this->getCacheID($this->getStorage('fetch.id')); + $entryCacheID = $this->getCacheID($this->storage()->get('fetch.id')); // 1. Try to get current requested entry from cache if (flextype('cache')->has($entryCacheID)) { // Fetch entry from cache and Apply filter for fetch data - $this->storage['fetch']['data'] = filter(flextype('cache')->get($entryCacheID), - $this->getStorage('fetch.options.filter', [])); + $this->storage()->set('fetch.data', filter(flextype('cache')->get($entryCacheID), + $this->storage()->get('fetch.options.filter', []))); // Run event: onEntriesFetchSingleCacheHasResult flextype('emitter')->emit('onEntriesFetchSingleCacheHasResult'); // Return entry from cache - return arrays($this->getStorage('fetch.data')); + return arrays($this->storage()->get('fetch.data')); } // 2. Try to get current requested entry from filesystem - if ($this->has($this->getStorage('fetch.id'))) { + if ($this->has($this->storage()->get('fetch.id'))) { // Get entry file location - $entryFile = $this->getFileLocation($this->getStorage('fetch.id')); + $entryFile = $this->getFileLocation($this->storage()->get('fetch.id')); // Try to get requested entry from the filesystem $entryFileContent = filesystem()->file($entryFile)->get(); @@ -125,36 +112,36 @@ class Entries if ($entryFileContent === false) { // Run event: onEntriesFetchSingleNoResult flextype('emitter')->emit('onEntriesFetchSingleNoResult'); - return arrays($this->getStorage('fetch.data')); + return arrays($this->storage()->get('fetch.data')); } // Decode entry file content - $this->setStorage('fetch.data', flextype('serializers')->frontmatter()->decode($entryFileContent)); + $this->storage()->set('fetch.data', flextype('serializers')->frontmatter()->decode($entryFileContent)); // Run event: onEntriesFetchSingleHasResult flextype('emitter')->emit('onEntriesFetchSingleHasResult'); // Apply filter for fetch data - $this->storage['fetch']['data'] = filter($this->getStorage('fetch.data'), - $this->getStorage('fetch.options.filter', [])); + $this->storage()->set('fetch.data', filter($this->storage()->get('fetch.data'), + $this->storage()->get('fetch.options.filter', []))); // Set cache state - $cache = $this->getStorage('fetch.data.cache', flextype('registry')->get('flextype.settings.cache.enabled')); + $cache = $this->storage()->get('fetch.data.cache', flextype('registry')->get('flextype.settings.cache.enabled')); // Save entry data to cache if ($cache) { - flextype('cache')->set($entryCacheID, $this->getStorage('fetch.data')); + flextype('cache')->set($entryCacheID, $this->storage()->get('fetch.data')); } // Return entry data - return arrays($this->getStorage('fetch.data')); + return arrays($this->storage()->get('fetch.data')); } // Run event: onEntriesFetchSingleNoResult flextype('emitter')->emit('onEntriesFetchSingleNoResult'); // Return empty array if entry is not founded - return arrays($this->getStorage('fetch.data')); + return arrays($this->storage()->get('fetch.data')); }; if (isset($this->storage['fetch']['options']['collection']) && @@ -168,7 +155,7 @@ class Entries flextype('emitter')->emit('onEntriesFetchCollectionNoResult'); // Return entries array - return arrays($this->getStorage('fetch.data')); + return arrays($this->storage()->get('fetch.data')); } // Find entries in the filesystem @@ -196,13 +183,13 @@ class Entries $data[$currentEntryID] = $single($currentEntryID, [])->toArray(); } - $this->setStorage('fetch.data', $data); + $this->storage()->set('fetch.data', $data); // Run event: onEntriesFetchCollectionHasResult flextype('emitter')->emit('onEntriesFetchCollectionHasResult'); // Apply filter for fetch data - $this->setStorage('fetch.data', filter($this->getStorage('fetch.data'), + $this->storage()->set('fetch.data', filter($this->storage()->get('fetch.data'), isset($options['filter']) ? $options['filter'] : [])); @@ -212,7 +199,7 @@ class Entries flextype('emitter')->emit('onEntriesFetchCollectionNoResult'); // Return entries array - return arrays($this->getStorage('fetch.data')); + return arrays($this->storage()->get('fetch.data')); } else { return $single($this->storage['fetch']['id'], $this->storage['fetch']['options']); @@ -232,16 +219,16 @@ class Entries public function move(string $id, string $newID): bool { // Store data - $this->setStorage('move.id', $id); - $this->setStorage('move.newID', $newID); + $this->storage()->set('move.id', $id); + $this->storage()->set('move.newID', $newID); // Run event: onEntriesMove flextype('emitter')->emit('onEntriesMove'); - if (! $this->has($this->getStorage('move.newID'))) { + if (! $this->has($this->storage()->get('move.newID'))) { return filesystem() - ->directory($this->getDirectoryLocation($this->getStorage('move.id'))) - ->move($this->getDirectoryLocation($this->getStorage('move.newID'))); + ->directory($this->getDirectoryLocation($this->storage()->get('move.id'))) + ->move($this->getDirectoryLocation($this->storage()->get('move.newID'))); } return false; @@ -260,19 +247,19 @@ class Entries public function update(string $id, array $data): bool { // Store data - $this->setStorage('update.id', $id); - $this->setStorage('update.data', $data); + $this->storage()->set('update.id', $id); + $this->storage()->set('update.data', $data); // Run event: onEntriesUpdate flextype('emitter')->emit('onEntriesUpdate'); - $entryFile = $this->getFileLocation($this->getStorage('update.id')); + $entryFile = $this->getFileLocation($this->storage()->get('update.id')); if (filesystem()->file($entryFile)->exists()) { $body = filesystem()->file($entryFile)->get(); $entry = flextype('serializers')->frontmatter()->decode($body); - return (bool) filesystem()->file($entryFile)->put(flextype('serializers')->frontmatter()->encode(array_merge($entry, $this->getStorage('update.data')))); + return (bool) filesystem()->file($entryFile)->put(flextype('serializers')->frontmatter()->encode(array_merge($entry, $this->storage()->get('update.data')))); } return false; @@ -291,14 +278,14 @@ class Entries public function create(string $id, array $data = []): bool { // Store data - $this->setStorage('create.id', $id); - $this->setStorage('create.data', $data); + $this->storage()->set('create.id', $id); + $this->storage()->set('create.data', $data); // Run event: onEntriesCreate flextype('emitter')->emit('onEntriesCreate'); // Create entry directory first if it is not exists - $entryDir = $this->getDirectoryLocation($this->getStorage('create.id')); + $entryDir = $this->getDirectoryLocation($this->storage()->get('create.id')); if ( ! filesystem()->directory($entryDir)->exists() && @@ -310,7 +297,7 @@ class Entries // Create entry file $entryFile = $entryDir . '/entry' . '.' . flextype('registry')->get('flextype.settings.entries.extension'); if (! filesystem()->file($entryFile)->exists()) { - return (bool) filesystem()->file($entryFile)->put(flextype('serializers')->frontmatter()->encode($this->getStorage('create.data'))); + return (bool) filesystem()->file($entryFile)->put(flextype('serializers')->frontmatter()->encode($this->storage()->get('create.data'))); } return false; @@ -328,13 +315,13 @@ class Entries public function delete(string $id): bool { // Store data - $this->setStorage('delete.id', $id); + $this->storage()->set('delete.id', $id); // Run event: onEntriesDelete flextype('emitter')->emit('onEntriesDelete'); return filesystem() - ->directory($this->getDirectoryLocation($this->getStorage('delete.id'))) + ->directory($this->getDirectoryLocation($this->storage()->get('delete.id'))) ->delete(); } @@ -351,15 +338,15 @@ class Entries public function copy(string $id, string $newID): ?bool { // Store data - $this->setStorage('copy.id', $id); - $this->setStorage('copy.newID', $newID); + $this->storage()->set('copy.id', $id); + $this->storage()->set('copy.newID', $newID); // Run event: onEntriesCopy flextype('emitter')->emit('onEntriesCopy'); return filesystem() - ->directory($this->getDirectoryLocation($this->getStorage('copy.id'))) - ->copy($this->getDirectoryLocation($this->getStorage('copy.newID'))); + ->directory($this->getDirectoryLocation($this->storage()->get('copy.id'))) + ->copy($this->getDirectoryLocation($this->storage()->get('copy.newID'))); } /** @@ -374,71 +361,12 @@ class Entries public function has(string $id): bool { // Store data - $this->setStorage('has.id', $id); + $this->storage()->set('has.id', $id); // Run event: onEntriesHas flextype('emitter')->emit('onEntriesHas'); - return filesystem()->file($this->getFileLocation($this->getStorage('has.id')))->exists(); - } - - /** - * Get an item from an storage using "dot" notation. - * - * @param string|int|null $key Key. - * @param mixed $default Default value. - * - * @access public - * - * @return array Updated storage. - */ - public function getStorage($key, $default = null) - { - return arrays($this->storage)->get($key, $default); - } - - /** - * Checks if the given dot-notated key exists in the storage. - * - * @param string|array $keys Keys - * - * @return bool Return TRUE key exists in the array, FALSE otherwise. - */ - public function hasStorage($keys): bool - { - return arrays($this->storage)->has($keys); - } - - /** - * Set an storage item to a given value using "dot" notation. - * If no key is given to the method, the entire storage will be replaced. - * - * @param string|null $key Key. - * @param mixed $value Value. - * - * @access public - * - * @return self Returns instance of The Entries class. - */ - public function setStorage(?string $key, $value): self - { - $this->storage = arrays($this->storage)->set($key, $value)->toArray(); - - return $this; - } - - /** - * Deletes an storage value using "dot notation". - * - * @param array|string $keys Keys - * - * @return self Returns instance of The Entries class. - */ - public function deleteStorage($keys): self - { - $this->storage = arrays($this->storage)->delete($keys)->toArray(); - - return $this; + return filesystem()->file($this->getFileLocation($this->storage()->get('has.id')))->exists(); } /** diff --git a/src/flextype/Foundation/Entries/Fields/CreatedAtField.php b/src/flextype/Foundation/Entries/Fields/CreatedAtField.php index fa4a6dbd..70eed995 100644 --- a/src/flextype/Foundation/Entries/Fields/CreatedAtField.php +++ b/src/flextype/Foundation/Entries/Fields/CreatedAtField.php @@ -10,18 +10,18 @@ declare(strict_types=1); if (flextype('registry')->get('flextype.settings.entries.fields.created_at.enabled')) { flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void { - if (flextype('entries')->getStorage('fetch.data.created_at') === null) { - flextype('entries')->setStorage('fetch.data.created_at', (int) filesystem()->file(flextype('entries')->getFileLocation(flextype('entries')->getStorage('fetch.id')))->lastModified()); + if (flextype('entries')->storage()->get('fetch.data.created_at') === null) { + flextype('entries')->storage()->set('fetch.data.created_at', (int) filesystem()->file(flextype('entries')->getFileLocation(flextype('entries')->storage()->get('fetch.id')))->lastModified()); } else { - flextype('entries')->setStorage('fetch.data.created_at', (int) strtotime((string) flextype('entries')->getStorage('fetch.data.created_at'))); + flextype('entries')->storage()->set('fetch.data.created_at', (int) strtotime((string) flextype('entries')->storage()->get('fetch.data.created_at'))); } }); flextype('emitter')->addListener('onEntriesCreate', static function (): void { - if (flextype('entries')->getStorage('create.data.created_at') !== null) { + if (flextype('entries')->storage()->get('create.data.created_at') !== null) { return; } - flextype('entries')->setStorage('create.data.created_at', date(flextype('registry')->get('flextype.settings.date_format'), time())); + flextype('entries')->storage()->set('create.data.created_at', date(flextype('registry')->get('flextype.settings.date_format'), time())); }); } diff --git a/src/flextype/Foundation/Entries/Fields/CreatedByField.php b/src/flextype/Foundation/Entries/Fields/CreatedByField.php index 7090425c..f553584b 100644 --- a/src/flextype/Foundation/Entries/Fields/CreatedByField.php +++ b/src/flextype/Foundation/Entries/Fields/CreatedByField.php @@ -9,10 +9,10 @@ declare(strict_types=1); if (flextype('registry')->get('flextype.settings.entries.fields.created_by.enabled')) { flextype('emitter')->addListener('onEntriesCreate', static function (): void { - if (flextype('entries')->getStorage('create.data.created_by') !== null) { + if (flextype('entries')->storage()->get('create.data.created_by') !== null) { return; } - flextype('entries')->setStorage('create.data.created_by', ''); + flextype('entries')->storage()->set('create.data.created_by', ''); }); } diff --git a/src/flextype/Foundation/Entries/Fields/EntriesField.php b/src/flextype/Foundation/Entries/Fields/EntriesField.php index c3ee7208..00a89f1b 100644 --- a/src/flextype/Foundation/Entries/Fields/EntriesField.php +++ b/src/flextype/Foundation/Entries/Fields/EntriesField.php @@ -11,9 +11,9 @@ use Atomastic\Arrays\Arrays; if (flextype('registry')->get('flextype.settings.entries.fields.entries.fetch.enabled')) { flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void { - if (flextype('entries')->hasStorage('fetch.data.entries.fetch')) { + if (flextype('entries')->storage()->has('fetch.data.entries.fetch')) { // Get fetch. - $original = flextype('entries')->getStorage('fetch'); + $original = flextype('entries')->storage()->get('fetch'); $data = []; switch (flextype('registry')->get('flextype.settings.entries.fields.entries.fetch.result')) { @@ -28,7 +28,7 @@ if (flextype('registry')->get('flextype.settings.entries.fields.entries.fetch.en } // Modify fetch. - foreach (flextype('entries')->getStorage('fetch.data.entries.fetch') as $field => $body) { + foreach (flextype('entries')->storage()->get('fetch.data.entries.fetch') as $field => $body) { if (isset($body['options']['method']) && strpos($body['options']['method'], 'fetch') !== false && @@ -49,9 +49,9 @@ if (flextype('registry')->get('flextype.settings.entries.fields.entries.fetch.en } // Save fetch. - flextype('entries')->setStorage('fetch.id', $original['id']); - flextype('entries')->setStorage('fetch.options', $original['options']); - flextype('entries')->setStorage('fetch.data', arrays($original['data'])->merge($data)->toArray()); + flextype('entries')->storage()->set('fetch.id', $original['id']); + flextype('entries')->storage()->set('fetch.options', $original['options']); + flextype('entries')->storage()->set('fetch.data', arrays($original['data'])->merge($data)->toArray()); } }); } diff --git a/src/flextype/Foundation/Entries/Fields/IdField.php b/src/flextype/Foundation/Entries/Fields/IdField.php index 45e3dc20..d0fec2ad 100644 --- a/src/flextype/Foundation/Entries/Fields/IdField.php +++ b/src/flextype/Foundation/Entries/Fields/IdField.php @@ -10,10 +10,10 @@ declare(strict_types=1); if (flextype('registry')->get('flextype.settings.entries.fields.id.enabled')) { flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void { - if (flextype('entries')->getStorage('fetch.data.id') !== null) { + if (flextype('entries')->storage()->get('fetch.data.id') !== null) { return; } - flextype('entries')->setStorage('fetch.data.id', (string) strings(flextype('entries')->getStorage('fetch.id'))->trimSlashes()); + flextype('entries')->storage()->set('fetch.data.id', (string) strings(flextype('entries')->storage()->get('fetch.id'))->trimSlashes()); }); } diff --git a/src/flextype/Foundation/Entries/Fields/MediaField.php b/src/flextype/Foundation/Entries/Fields/MediaField.php index c0f31fb9..bd1d23ae 100644 --- a/src/flextype/Foundation/Entries/Fields/MediaField.php +++ b/src/flextype/Foundation/Entries/Fields/MediaField.php @@ -11,9 +11,9 @@ use Atomastic\Arrays\Arrays; if (flextype('registry')->get('flextype.settings.entries.fields.media.files.fetch.enabled')) { flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void { - if (flextype('entries')->hasStorage('fetch.data.media.files.fetch')) { + if (flextype('entries')->storage()->has('fetch.data.media.files.fetch')) { // Get fetch. - $original = flextype('entries')->getStorage('fetch'); + $original = flextype('entries')->storage()->get('fetch'); $data = []; switch (flextype('registry')->get('flextype.settings.entries.fields.media.files.fetch.result')) { @@ -28,7 +28,7 @@ if (flextype('registry')->get('flextype.settings.entries.fields.media.files.fetc } // Modify fetch. - foreach (flextype('entries')->getStorage('fetch.data.media.files.fetch') as $field => $body) { + foreach (flextype('entries')->storage()->get('fetch.data.media.files.fetch') as $field => $body) { if (isset($body['options']['method']) && strpos($body['options']['method'], 'fetch') !== false && @@ -50,9 +50,9 @@ if (flextype('registry')->get('flextype.settings.entries.fields.media.files.fetc } // Save fetch. - flextype('entries')->setStorage('fetch.id', $original['id']); - flextype('entries')->setStorage('fetch.options', $original['options']); - flextype('entries')->setStorage('fetch.data', arrays($original['data'])->merge($data)->toArray()); + flextype('entries')->storage()->set('fetch.id', $original['id']); + flextype('entries')->storage()->set('fetch.options', $original['options']); + flextype('entries')->storage()->set('fetch.data', arrays($original['data'])->merge($data)->toArray()); } }); } @@ -60,10 +60,10 @@ if (flextype('registry')->get('flextype.settings.entries.fields.media.files.fetc if (flextype('registry')->get('flextype.settings.entries.fields.media.folders.fetch.enabled')) { flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void { - if (flextype('entries')->hasStorage('fetch.data.media.folders.fetch')) { + if (flextype('entries')->storage()->has('fetch.data.media.folders.fetch')) { // Get fetch. - $original = flextype('entries')->getStorage('fetch'); + $original = flextype('entries')->storage()->get('fetch'); $data = []; switch (flextype('registry')->get('flextype.settings.entries.fields.media.folders.fetch.result')) { @@ -78,7 +78,7 @@ if (flextype('registry')->get('flextype.settings.entries.fields.media.folders.fe } // Modify fetch. - foreach (flextype('entries')->getStorage('fetch.data.media.folders.fetch') as $field => $body) { + foreach (flextype('entries')->storage()->get('fetch.data.media.folders.fetch') as $field => $body) { if (isset($body['options']['method']) && strpos($body['options']['method'], 'fetch') !== false && @@ -100,9 +100,9 @@ if (flextype('registry')->get('flextype.settings.entries.fields.media.folders.fe } // Save fetch. - flextype('entries')->setStorage('fetch.id', $original['id']); - flextype('entries')->setStorage('fetch.options', $original['options']); - flextype('entries')->setStorage('fetch.data', arrays($original['data'])->merge($data)->toArray()); + flextype('entries')->storage()->set('fetch.id', $original['id']); + flextype('entries')->storage()->set('fetch.options', $original['options']); + flextype('entries')->storage()->set('fetch.data', arrays($original['data'])->merge($data)->toArray()); } }); } diff --git a/src/flextype/Foundation/Entries/Fields/ModifiedAtField.php b/src/flextype/Foundation/Entries/Fields/ModifiedAtField.php index b2a42bbc..fd8df1b4 100644 --- a/src/flextype/Foundation/Entries/Fields/ModifiedAtField.php +++ b/src/flextype/Foundation/Entries/Fields/ModifiedAtField.php @@ -9,10 +9,10 @@ declare(strict_types=1); if (flextype('registry')->get('flextype.settings.entries.fields.modified_at.enabled')) { flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void { - if (flextype('entries')->getStorage('fetch.data.modified_at') !== null) { + if (flextype('entries')->storage()->get('fetch.data.modified_at') !== null) { return; } - flextype('entries')->setStorage('fetch.data.modified_at', (int) filesystem()->file(flextype('entries')->getFileLocation(flextype('entries')->getStorage('fetch.id')))->lastModified()); + flextype('entries')->storage()->set('fetch.data.modified_at', (int) filesystem()->file(flextype('entries')->getFileLocation(flextype('entries')->storage()->get('fetch.id')))->lastModified()); }); } diff --git a/src/flextype/Foundation/Entries/Fields/ParsersField.php b/src/flextype/Foundation/Entries/Fields/ParsersField.php index fecf4840..24bb5d19 100644 --- a/src/flextype/Foundation/Entries/Fields/ParsersField.php +++ b/src/flextype/Foundation/Entries/Fields/ParsersField.php @@ -15,30 +15,30 @@ if (flextype('registry')->get('flextype.settings.entries.fields.parsers.enabled' function processParsersField(): void { - if (flextype('entries')->getStorage('fetch.data.cache.enabled') == null) { + if (flextype('entries')->storage()->get('fetch.data.cache.enabled') == null) { $cache = false; } else { - $cache = (bool) flextype('entries')->getStorage('fetch.data.cache.enabled'); + $cache = (bool) flextype('entries')->storage()->get('fetch.data.cache.enabled'); } - if (flextype('entries')->getStorage('fetch.data.parsers') != null) { - foreach (flextype('entries')->getStorage('fetch.data.parsers') as $parserName => $parserData) { + if (flextype('entries')->storage()->get('fetch.data.parsers') != null) { + foreach (flextype('entries')->storage()->get('fetch.data.parsers') as $parserName => $parserData) { if (in_array($parserName, ['markdown', 'shortcode'])) { - if (flextype('entries')->getStorage('fetch.data.parsers.'.$parserName.'.enabled') === true) { - if (flextype('entries')->getStorage('fetch.data.parsers.'.$parserName.'.fields') != null) { - if (is_array(flextype('entries')->getStorage('fetch.data.parsers.'.$parserName.'.fields'))) { - foreach (flextype('entries')->getStorage('fetch.data.parsers.'.$parserName.'.fields') as $field) { + if (flextype('entries')->storage()->get('fetch.data.parsers.'.$parserName.'.enabled') === true) { + if (flextype('entries')->storage()->get('fetch.data.parsers.'.$parserName.'.fields') != null) { + if (is_array(flextype('entries')->storage()->get('fetch.data.parsers.'.$parserName.'.fields'))) { + foreach (flextype('entries')->storage()->get('fetch.data.parsers.'.$parserName.'.fields') as $field) { if (! in_array($field, flextype('registry')->get('flextype.settings.entries.fields'))) { if ($parserName == 'markdown') { - if (arrays(flextype('entries')->getStorage('fetch.data'))->has($field)) { - flextype('entries')->setStorage('fetch.data.'.$field, - flextype('parsers')->markdown()->parse(flextype('entries')->getStorage('fetch.data.'.$field), $cache)); + if (arrays(flextype('entries')->storage()->get('fetch.data'))->has($field)) { + flextype('entries')->storage()->set('fetch.data.'.$field, + flextype('parsers')->markdown()->parse(flextype('entries')->storage()->get('fetch.data.'.$field), $cache)); } } if ($parserName == 'shortcode') { - if (arrays(flextype('entries')->getStorage('fetch.data'))->has($field)) { - flextype('entries')->setStorage('fetch.data.'.$field, - flextype('parsers')->shortcode()->process(flextype('entries')->getStorage('fetch.data.'.$field), $cache)); + if (arrays(flextype('entries')->storage()->get('fetch.data'))->has($field)) { + flextype('entries')->storage()->set('fetch.data.'.$field, + flextype('parsers')->shortcode()->process(flextype('entries')->storage()->get('fetch.data.'.$field), $cache)); } } } diff --git a/src/flextype/Foundation/Entries/Fields/PublishedAtField.php b/src/flextype/Foundation/Entries/Fields/PublishedAtField.php index 0caa118d..0fda2c8e 100644 --- a/src/flextype/Foundation/Entries/Fields/PublishedAtField.php +++ b/src/flextype/Foundation/Entries/Fields/PublishedAtField.php @@ -9,18 +9,18 @@ declare(strict_types=1); if (flextype('registry')->get('flextype.settings.entries.fields.published_at.enabled')) { flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void { - if (flextype('entries')->getStorage('fetch.data.published_at') === null) { - flextype('entries')->setStorage('fetch.data.published_at', (int) filesystem()->file(flextype('entries')->getFileLocation(flextype('entries')->getStorage('fetch.id')))->lastModified()); + if (flextype('entries')->storage()->get('fetch.data.published_at') === null) { + flextype('entries')->storage()->set('fetch.data.published_at', (int) filesystem()->file(flextype('entries')->getFileLocation(flextype('entries')->storage()->get('fetch.id')))->lastModified()); } else { - flextype('entries')->setStorage('fetch.data.published_at', (int) strtotime((string) flextype('entries')->getStorage('fetch.data.published_at'))); + flextype('entries')->storage()->set('fetch.data.published_at', (int) strtotime((string) flextype('entries')->storage()->get('fetch.data.published_at'))); } }); flextype('emitter')->addListener('onEntriesCreate', static function (): void { - if (flextype('entries')->getStorage('create.data.published_at') !== null) { + if (flextype('entries')->storage()->get('create.data.published_at') !== null) { return; } - flextype('entries')->setStorage('create.data.published_at', date(flextype('registry')->get('flextype.settings.date_format'), time())); + flextype('entries')->storage()->set('create.data.published_at', date(flextype('registry')->get('flextype.settings.date_format'), time())); }); } diff --git a/src/flextype/Foundation/Entries/Fields/PublishedByField.php b/src/flextype/Foundation/Entries/Fields/PublishedByField.php index b742ca56..822f964f 100644 --- a/src/flextype/Foundation/Entries/Fields/PublishedByField.php +++ b/src/flextype/Foundation/Entries/Fields/PublishedByField.php @@ -9,10 +9,10 @@ declare(strict_types=1); if (flextype('registry')->get('flextype.settings.entries.fields.published_by.enabled')) { flextype('emitter')->addListener('onEntriesCreate', static function (): void { - if (flextype('entries')->getStorage('create.data.published_by') !== null) { + if (flextype('entries')->storage()->get('create.data.published_by') !== null) { return; } - flextype('entries')->setStorage('create.data.published_by', ''); + flextype('entries')->storage()->set('create.data.published_by', ''); }); } diff --git a/src/flextype/Foundation/Entries/Fields/RegistryField.php b/src/flextype/Foundation/Entries/Fields/RegistryField.php index 6a9ab01b..11eeb326 100644 --- a/src/flextype/Foundation/Entries/Fields/RegistryField.php +++ b/src/flextype/Foundation/Entries/Fields/RegistryField.php @@ -9,14 +9,14 @@ declare(strict_types=1); if (flextype('registry')->get('flextype.settings.entries.fields.registry.get.enabled')) { flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void { - if (flextype('entries')->hasStorage('fetch.data.registry.get')) { + if (flextype('entries')->storage()->has('fetch.data.registry.get')) { // Get fetch. - $original = flextype('entries')->getStorage('fetch'); + $original = flextype('entries')->storage()->get('fetch'); $data = []; // Modify fetch. - foreach (flextype('entries')->getStorage('fetch.data.registry.get') as $field => $body) { + foreach (flextype('entries')->storage()->get('fetch.data.registry.get') as $field => $body) { $data = arrays($data)->merge(arrays($data)->set($field, flextype('registry')->get($body['key'], isset($body['default']) ? $body['default'] : @@ -25,7 +25,7 @@ if (flextype('registry')->get('flextype.settings.entries.fields.registry.get.ena } // Save fetch. - flextype('entries')->setStorage('fetch.data', arrays($original['data'])->merge($data)->toArray()); + flextype('entries')->storage()->set('fetch.data', arrays($original['data'])->merge($data)->toArray()); } }); } diff --git a/src/flextype/Foundation/Entries/Fields/RoutableField.php b/src/flextype/Foundation/Entries/Fields/RoutableField.php index 7812a0b4..d60acb6d 100644 --- a/src/flextype/Foundation/Entries/Fields/RoutableField.php +++ b/src/flextype/Foundation/Entries/Fields/RoutableField.php @@ -10,18 +10,18 @@ declare(strict_types=1); if (flextype('registry')->get('flextype.settings.entries.fields.routable.enabled')) { flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void { - if (flextype('entries')->getStorage('fetch.data.routable') === null) { - flextype('entries')->setStorage('fetch.data.routable', true); + if (flextype('entries')->storage()->get('fetch.data.routable') === null) { + flextype('entries')->storage()->set('fetch.data.routable', true); } else { - flextype('entries')->setStorage('fetch.data.routable', (bool) flextype('entries')->getStorage('fetch.data.routable')); + flextype('entries')->storage()->set('fetch.data.routable', (bool) flextype('entries')->storage()->get('fetch.data.routable')); } }); flextype('emitter')->addListener('onEntriesCreate', static function (): void { - if (flextype('entries')->getStorage('create.data.routable') === null) { - flextype('entries')->setStorage('create.data.routable', true); + if (flextype('entries')->storage()->get('create.data.routable') === null) { + flextype('entries')->storage()->set('create.data.routable', true); } else { - flextype('entries')->setStorage('create.data.routable', (bool) flextype('entries')->getStorage('create.data.routable')); + flextype('entries')->storage()->set('create.data.routable', (bool) flextype('entries')->storage()->get('create.data.routable')); } }); } diff --git a/src/flextype/Foundation/Entries/Fields/SlugField.php b/src/flextype/Foundation/Entries/Fields/SlugField.php index 694606bf..132f9a50 100644 --- a/src/flextype/Foundation/Entries/Fields/SlugField.php +++ b/src/flextype/Foundation/Entries/Fields/SlugField.php @@ -10,11 +10,11 @@ declare(strict_types=1); if (flextype('registry')->get('flextype.settings.entries.fields.slug.enabled')) { flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void { - if (flextype('entries')->getStorage('fetch.data.slug') !== null) { + if (flextype('entries')->storage()->get('fetch.data.slug') !== null) { return; } - $parts = explode('/', ltrim(rtrim(flextype('entries')->getStorage('fetch.id'), '/'), '/')); - flextype('entries')->setStorage('fetch.data.slug', (string) end($parts)); + $parts = explode('/', ltrim(rtrim(flextype('entries')->storage()->get('fetch.id'), '/'), '/')); + flextype('entries')->storage()->set('fetch.data.slug', (string) end($parts)); }); } diff --git a/src/flextype/Foundation/Entries/Fields/UuidField.php b/src/flextype/Foundation/Entries/Fields/UuidField.php index 25bfeabb..1ce7b0ea 100644 --- a/src/flextype/Foundation/Entries/Fields/UuidField.php +++ b/src/flextype/Foundation/Entries/Fields/UuidField.php @@ -11,10 +11,10 @@ use Ramsey\Uuid\Uuid; if (flextype('registry')->get('flextype.settings.entries.fields.uuid.enabled')) { flextype('emitter')->addListener('onEntriesCreate', static function (): void { - if (flextype('entries')->getStorage('create.data.uuid') !== null) { + if (flextype('entries')->storage()->get('create.data.uuid') !== null) { return; } - flextype('entries')->setStorage('create.data.uuid', Uuid::uuid4()->toString()); + flextype('entries')->storage()->set('create.data.uuid', Uuid::uuid4()->toString()); }); } diff --git a/src/flextype/Foundation/Entries/Fields/VisibilityField.php b/src/flextype/Foundation/Entries/Fields/VisibilityField.php index 16845362..e48400a2 100644 --- a/src/flextype/Foundation/Entries/Fields/VisibilityField.php +++ b/src/flextype/Foundation/Entries/Fields/VisibilityField.php @@ -15,18 +15,18 @@ if (flextype('registry')->get('flextype.settings.entries.fields.visibility.enabl ]; flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function () use ($visibility): void { - if (flextype('entries')->getStorage('fetch.data.visibility') !== null && in_array(flextype('entries')->getStorage('fetch.data.visibility'), $visibility)) { - flextype('entries')->setStorage('fetch.data.visibility', (string) $visibility[flextype('entries')->getStorage('fetch.data.visibility')]); + if (flextype('entries')->storage()->get('fetch.data.visibility') !== null && in_array(flextype('entries')->storage()->get('fetch.data.visibility'), $visibility)) { + flextype('entries')->storage()->set('fetch.data.visibility', (string) $visibility[flextype('entries')->storage()->get('fetch.data.visibility')]); } else { - flextype('entries')->setStorage('fetch.data.visibility', (string) $visibility['visible']); + flextype('entries')->storage()->set('fetch.data.visibility', (string) $visibility['visible']); } }); flextype('emitter')->addListener('onEntriesCreate', static function () use ($visibility): void { - if (flextype('entries')->getStorage('create.data.visibility') !== null && in_array(flextype('entries')->getStorage('create.data.visibility'), $visibility)) { - flextype('entries')->setStorage('create.data.visibility', (string) $visibility[flextype('entries')->getStorage('create.data.visibility')]); + if (flextype('entries')->storage()->get('create.data.visibility') !== null && in_array(flextype('entries')->storage()->get('create.data.visibility'), $visibility)) { + flextype('entries')->storage()->set('create.data.visibility', (string) $visibility[flextype('entries')->storage()->get('create.data.visibility')]); } else { - flextype('entries')->setStorage('create.data.visibility', (string) $visibility['visible']); + flextype('entries')->storage()->set('create.data.visibility', (string) $visibility['visible']); } }); } diff --git a/tests/Foundation/Entries/EntriesTest.php b/tests/Foundation/Entries/EntriesTest.php index 3c6e79e4..bd0dc3b7 100644 --- a/tests/Foundation/Entries/EntriesTest.php +++ b/tests/Foundation/Entries/EntriesTest.php @@ -43,20 +43,20 @@ test('test fetch() entry', function () { $this->assertEquals('Baz', flextype('entries')->fetch('foo/baz')['title']); $this->assertEquals('Zed', flextype('entries')->fetch('foo/zed')['title']); - flextype('entries')->setStorage('fetch.id', 'wrong-entry'); + flextype('entries')->storage()->set('fetch.id', 'wrong-entry'); $this->assertEquals(0, flextype('entries')->fetch('wrong-entry')->count()); - flextype('entries')->setStorage('fetch.id', 'wrong-entry'); + flextype('entries')->storage()->set('fetch.id', 'wrong-entry'); $this->assertEquals(0, flextype('entries')->fetch('wrong-entry')->count()); $this->assertTrue(count(flextype('entries')->fetch('foo', ['collection' => true])) > 0); /* flextype('emitter')->addListener('onEntriesFetchCollectionHasResult', static function (): void { - flextype('entries')->setStorage('fetch_collection.data.foo/zed.title', 'ZedFromCollection!'); + flextype('entries')->storage()->set('fetch_collection.data.foo/zed.title', 'ZedFromCollection!'); }); flextype('emitter')->addListener('onEntriesFetchCollectionHasResult', static function (): void { - flextype('entries')->setStorage('fetch_collection.data.foo/baz.title', 'BazFromCollection!'); + flextype('entries')->storage()->set('fetch_collection.data.foo/baz.title', 'BazFromCollection!'); }); $this->assertEquals('ZedFromCollection!', flextype('entries')->fetch('foo', ['collection' => true])['foo/zed.title']); @@ -121,13 +121,16 @@ test('test getCacheID() entry', function () { flextype('registry')->set('flextype.settings.cache.enabled', false); }); -test('test setStorage() and getStorage() entry', function () { - flextype('entries')->setStorage('foo', ['title' => 'Foo']); - flextype('entries')->setStorage('bar', ['title' => 'Bar']); - $this->assertEquals('Foo', flextype('entries')->getStorage('foo')['title']); - $this->assertEquals('Bar', flextype('entries')->getStorage('bar')['title']); - $this->assertEquals('Foo', flextype('entries')->getStorage('foo.title')); - $this->assertEquals('Bar', flextype('entries')->getStorage('bar.title')); +test('test storage() entry', function () { + flextype('entries')->storage()->set('foo', ['title' => 'Foo']); + $this->assertEquals('Foo', flextype('entries')->storage()->get('foo')['title']); + flextype('entries')->storage()->set('bar', ['title' => 'Bar']); + $this->assertEquals(true, flextype('entries')->storage()->has('foo.title')); + $this->assertEquals(true, flextype('entries')->storage()->has('bar.title')); + flextype('entries')->storage()->delete('foo.title'); + flextype('entries')->storage()->delete('bar.title'); + $this->assertEquals(false, flextype('entries')->storage()->has('foo.title')); + $this->assertEquals(false, flextype('entries')->storage()->has('bar.title')); }); test('test macro() entry', function () {