diff --git a/src/flextype/Foundation/Entries/Entries.php b/src/flextype/Foundation/Entries/Entries.php index 2a602127..4cd54339 100755 --- a/src/flextype/Foundation/Entries/Entries.php +++ b/src/flextype/Foundation/Entries/Entries.php @@ -40,6 +40,8 @@ class Entries * * @param string|int|null $key Key. * @param mixed $default Default value. + * + * @access public */ public function getStorage($key, $default = null) { @@ -51,12 +53,41 @@ class Entries * * @param string|null $key Key. * @param mixed $value Value. + * + * @access public */ public function setStorage(?string $key, $value): void { $this->storage = arrays($this->storage)->set($key, $value)->toArray(); } + /** + * Fetch. + * + * @param string $id Unique identifier of the entry. + * @param array $options Options array. + * + * @access public + * + * @return self Returns instance of The Arrays class. + */ + public function fetch(string $id, array $options = []): Arrays + { + // Store data + $this->storage['fetch']['id'] = $id; + $this->storage['fetch']['options'] = $options; + $this->storage['fetch']['data'] = []; + + // Run event: onEntriesFetch + flextype('emitter')->emit('onEntriesFetch'); + + if (isset($options['collection']) && strings($options['collection'])->isTrue()) { + return $this->fetchCollection($id, $options); + } + + return $this->fetchSingle($id, $options); + } + /** * Fetch single entry. * @@ -64,15 +95,18 @@ class Entries * @param array $options Options array. * * @access public + * + * @return self Returns instance of The Arrays class. */ public function fetchSingle(string $id, array $options = []): Arrays { // Store data - $this->storage['fetch']['id'] = $id; - $this->storage['fetch']['data'] = []; + $this->storage['fetch']['id'] = $id; + $this->storage['fetch']['options'] = $options; + $this->storage['fetch']['data'] = []; - // Run event: onEntryInitialized - flextype('emitter')->emit('onEntryInitialized'); + // Run event: onEntriesFetchSingle + flextype('emitter')->emit('onEntriesFetchSingle'); // Get Cache ID for current requested entry $entryCacheID = $this->getCacheID($this->storage['fetch']['id']); @@ -82,8 +116,8 @@ class Entries // Fetch entry from cache $this->storage['fetch']['data'] = flextype('cache')->get($entryCacheID); - // Run event: onEntryAfterCacheInitialized - flextype('emitter')->emit('onEntryAfterCacheInitialized'); + // Run event: onEntriesFetchSingleAfterCacheInitialized + flextype('emitter')->emit('onEntriesFetchSingleAfterCacheInitialized'); // Apply filter for fetch data $this->storage['fetch']['data'] = filter($this->storage['fetch']['data'], $options); @@ -106,8 +140,8 @@ class Entries // Decode entry file content $this->storage['fetch']['data'] = flextype('frontmatter')->decode($entryFileContent); - // Run event: onEntryAfterInitialized - flextype('emitter')->emit('onEntryAfterInitialized'); + // Run event: onEntriesFetchSingleAfterInitialized + flextype('emitter')->emit('onEntriesFetchSingleAfterInitialized'); // Set cache state $cache = flextype('entries')->storage['fetch']['data']['cache']['enabled'] ?? @@ -136,16 +170,18 @@ class Entries * @param array $options Options array. * * @access public + * + * @return self Returns instance of The Arrays class. */ public function fetchCollection(string $id, array $options = []): Arrays { // Store data $this->storage['fetch']['id'] = $id; - $this->storage['fetch']['data'] = []; $this->storage['fetch']['options'] = $options; + $this->storage['fetch']['data'] = []; - // Run event: onEntriesInitialized - flextype('emitter')->emit('onEntriesInitialized'); + // Run event: onEntriesFetchCollection + flextype('emitter')->emit('onEntriesFetchCollection'); // Find entries $entries = find($this->getDirectoryLocation($this->storage['fetch']['id']), $options); @@ -171,8 +207,8 @@ class Entries // Apply filter for fetch data $this->storage['fetch']['data'] = filter($data, $options); - // Run event: onEntriesAfterInitialized - flextype('emitter')->emit('onEntriesAfterInitialized'); + // Run event: onEntriesFetchCollectionAfterInitialized + flextype('emitter')->emit('onEntriesFetchCollectionAfterInitialized'); } // Return entries array @@ -195,8 +231,8 @@ class Entries $this->storage['move']['id'] = $id; $this->storage['move']['new_id'] = $newID; - // Run event: onEntryMove - flextype('emitter')->emit('onEntryMove'); + // Run event: onEntriesMove + flextype('emitter')->emit('onEntriesMove'); if (! $this->has($this->storage['move']['new_id'])) { return filesystem() @@ -223,8 +259,8 @@ class Entries $this->storage['update']['id'] = $id; $this->storage['update']['data'] = $data; - // Run event: onEntryUpdate - flextype('emitter')->emit('onEntryUpdate'); + // Run event: onEntriesUpdate + flextype('emitter')->emit('onEntriesUpdate'); $entryFile = $this->getFileLocation($this->storage['update']['id']); @@ -254,8 +290,8 @@ class Entries $this->storage['create']['id'] = $id; $this->storage['create']['data'] = $data; - // Run event: onEntryCreate - flextype('emitter')->emit('onEntryCreate'); + // Run event: onEntriesCreate + flextype('emitter')->emit('onEntriesCreate'); // Create entry directory first if it is not exists $entryDir = $this->getDirectoryLocation($this->storage['create']['id']); @@ -290,8 +326,8 @@ class Entries // Store data $this->storage['delete']['id'] = $id; - // Run event: onEntryDelete - flextype('emitter')->emit('onEntryDelete'); + // Run event: onEntriesDelete + flextype('emitter')->emit('onEntriesDelete'); return filesystem() ->directory($this->getDirectoryLocation($this->storage['delete']['id'])) @@ -314,8 +350,8 @@ class Entries $this->storage['copy']['id'] = $id; $this->storage['copy']['new_id'] = $newID; - // Run event: onEntryCopy - flextype('emitter')->emit('onEntryCopy'); + // Run event: onEntriesCopy + flextype('emitter')->emit('onEntriesCopy'); return filesystem() ->directory($this->getDirectoryLocation($this->storage['copy']['id'])) @@ -336,8 +372,8 @@ class Entries // Store data $this->storage['has']['id'] = $id; - // Run event: onEntryHas - flextype('emitter')->emit('onEntryHas'); + // Run event: onEntriesHas + flextype('emitter')->emit('onEntriesHas'); return filesystem()->file($this->getFileLocation($this->storage['has']['id']))->exists(); } diff --git a/src/flextype/Foundation/Entries/Fields/CreatedAtField.php b/src/flextype/Foundation/Entries/Fields/CreatedAtField.php index b8aa0e3e..cffe8b7b 100644 --- a/src/flextype/Foundation/Entries/Fields/CreatedAtField.php +++ b/src/flextype/Foundation/Entries/Fields/CreatedAtField.php @@ -9,7 +9,7 @@ declare(strict_types=1); if (flextype('registry')->get('flextype.settings.entries.fields.created_at.enabled')) { - flextype('emitter')->addListener('onEntryAfterInitialized', static function (): void { + flextype('emitter')->addListener('onEntriesFetchSingleAfterInitialized', 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()); } else { @@ -17,7 +17,7 @@ if (flextype('registry')->get('flextype.settings.entries.fields.created_at.enabl } }); - flextype('emitter')->addListener('onEntryCreate', static function (): void { + flextype('emitter')->addListener('onEntriesCreate', static function (): void { if (flextype('entries')->getStorage('create.data.created_at') !== null) { return; } diff --git a/src/flextype/Foundation/Entries/Fields/CreatedByField.php b/src/flextype/Foundation/Entries/Fields/CreatedByField.php index 6f7ed575..7090425c 100644 --- a/src/flextype/Foundation/Entries/Fields/CreatedByField.php +++ b/src/flextype/Foundation/Entries/Fields/CreatedByField.php @@ -8,7 +8,7 @@ declare(strict_types=1); */ if (flextype('registry')->get('flextype.settings.entries.fields.created_by.enabled')) { - flextype('emitter')->addListener('onEntryCreate', static function (): void { + flextype('emitter')->addListener('onEntriesCreate', static function (): void { if (flextype('entries')->getStorage('create.data.created_by') !== null) { return; } diff --git a/src/flextype/Foundation/Entries/Fields/IdField.php b/src/flextype/Foundation/Entries/Fields/IdField.php index 284d76e0..e6b95937 100644 --- a/src/flextype/Foundation/Entries/Fields/IdField.php +++ b/src/flextype/Foundation/Entries/Fields/IdField.php @@ -9,7 +9,7 @@ declare(strict_types=1); if (flextype('registry')->get('flextype.settings.entries.fields.id.enabled')) { - flextype('emitter')->addListener('onEntryAfterInitialized', static function (): void { + flextype('emitter')->addListener('onEntriesFetchSingleAfterInitialized', static function (): void { if (flextype('entries')->getStorage('fetch.data.id') !== null) { return; } diff --git a/src/flextype/Foundation/Entries/Fields/ModifiedAtField.php b/src/flextype/Foundation/Entries/Fields/ModifiedAtField.php index e34fb8cf..255f0c4f 100644 --- a/src/flextype/Foundation/Entries/Fields/ModifiedAtField.php +++ b/src/flextype/Foundation/Entries/Fields/ModifiedAtField.php @@ -8,7 +8,7 @@ declare(strict_types=1); */ if (flextype('registry')->get('flextype.settings.entries.fields.modified_at.enabled')) { - flextype('emitter')->addListener('onEntryAfterInitialized', static function (): void { + flextype('emitter')->addListener('onEntriesFetchSingleAfterInitialized', static function (): void { if (flextype('entries')->getStorage('fetch.data.modified_at') !== null) { return; } diff --git a/src/flextype/Foundation/Entries/Fields/ParsersField.php b/src/flextype/Foundation/Entries/Fields/ParsersField.php index 48af99b3..69cea88b 100644 --- a/src/flextype/Foundation/Entries/Fields/ParsersField.php +++ b/src/flextype/Foundation/Entries/Fields/ParsersField.php @@ -8,7 +8,7 @@ declare(strict_types=1); */ if (flextype('registry')->get('flextype.settings.entries.fields.parsers.enabled')) { - flextype('emitter')->addListener('onEntryAfterInitialized', static function (): void { + flextype('emitter')->addListener('onEntriesFetchSingleAfterInitialized', static function (): void { processParsersField(); }); } diff --git a/src/flextype/Foundation/Entries/Fields/PublishedAtField.php b/src/flextype/Foundation/Entries/Fields/PublishedAtField.php index 47335ebe..a5d048a0 100644 --- a/src/flextype/Foundation/Entries/Fields/PublishedAtField.php +++ b/src/flextype/Foundation/Entries/Fields/PublishedAtField.php @@ -8,7 +8,7 @@ declare(strict_types=1); */ if (flextype('registry')->get('flextype.settings.entries.fields.published_at.enabled')) { - flextype('emitter')->addListener('onEntryAfterInitialized', static function (): void { + flextype('emitter')->addListener('onEntriesFetchSingleAfterInitialized', 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()); } else { @@ -16,7 +16,7 @@ if (flextype('registry')->get('flextype.settings.entries.fields.published_at.ena } }); - flextype('emitter')->addListener('onEntryCreate', static function (): void { + flextype('emitter')->addListener('onEntriesCreate', static function (): void { if (flextype('entries')->getStorage('create.data.published_at') !== null) { return; } diff --git a/src/flextype/Foundation/Entries/Fields/PublishedByField.php b/src/flextype/Foundation/Entries/Fields/PublishedByField.php index ff88e295..b742ca56 100644 --- a/src/flextype/Foundation/Entries/Fields/PublishedByField.php +++ b/src/flextype/Foundation/Entries/Fields/PublishedByField.php @@ -8,7 +8,7 @@ declare(strict_types=1); */ if (flextype('registry')->get('flextype.settings.entries.fields.published_by.enabled')) { - flextype('emitter')->addListener('onEntryCreate', static function (): void { + flextype('emitter')->addListener('onEntriesCreate', static function (): void { if (flextype('entries')->getStorage('create.data.published_by') !== null) { return; } diff --git a/src/flextype/Foundation/Entries/Fields/RoutableField.php b/src/flextype/Foundation/Entries/Fields/RoutableField.php index f9cb25d3..d04d9a5d 100644 --- a/src/flextype/Foundation/Entries/Fields/RoutableField.php +++ b/src/flextype/Foundation/Entries/Fields/RoutableField.php @@ -9,7 +9,7 @@ declare(strict_types=1); if (flextype('registry')->get('flextype.settings.entries.fields.routable.enabled')) { - flextype('emitter')->addListener('onEntryAfterInitialized', static function (): void { + flextype('emitter')->addListener('onEntriesFetchSingleAfterInitialized', static function (): void { if (flextype('entries')->getStorage('fetch.data.routable') === null) { flextype('entries')->setStorage('fetch.data.routable', true); } else { @@ -17,7 +17,7 @@ if (flextype('registry')->get('flextype.settings.entries.fields.routable.enabled } }); - flextype('emitter')->addListener('onEntryCreate', static function (): void { + flextype('emitter')->addListener('onEntriesCreate', static function (): void { if (flextype('entries')->getStorage('create.data.routable') === null) { flextype('entries')->setStorage('create.data.routable', true); } else { diff --git a/src/flextype/Foundation/Entries/Fields/SlugField.php b/src/flextype/Foundation/Entries/Fields/SlugField.php index 2025e535..7b94c15e 100644 --- a/src/flextype/Foundation/Entries/Fields/SlugField.php +++ b/src/flextype/Foundation/Entries/Fields/SlugField.php @@ -9,7 +9,7 @@ declare(strict_types=1); if (flextype('registry')->get('flextype.settings.entries.fields.slug.enabled')) { - flextype('emitter')->addListener('onEntryAfterInitialized', static function (): void { + flextype('emitter')->addListener('onEntriesFetchSingleAfterInitialized', static function (): void { if (flextype('entries')->getStorage('fetch.data.slug') !== null) { return; } diff --git a/src/flextype/Foundation/Entries/Fields/UuidField.php b/src/flextype/Foundation/Entries/Fields/UuidField.php index 0fb14453..25bfeabb 100644 --- a/src/flextype/Foundation/Entries/Fields/UuidField.php +++ b/src/flextype/Foundation/Entries/Fields/UuidField.php @@ -10,7 +10,7 @@ declare(strict_types=1); use Ramsey\Uuid\Uuid; if (flextype('registry')->get('flextype.settings.entries.fields.uuid.enabled')) { - flextype('emitter')->addListener('onEntryCreate', static function (): void { + flextype('emitter')->addListener('onEntriesCreate', static function (): void { if (flextype('entries')->getStorage('create.data.uuid') !== null) { return; } diff --git a/src/flextype/Foundation/Entries/Fields/VisibilityField.php b/src/flextype/Foundation/Entries/Fields/VisibilityField.php index 98009646..98b08934 100644 --- a/src/flextype/Foundation/Entries/Fields/VisibilityField.php +++ b/src/flextype/Foundation/Entries/Fields/VisibilityField.php @@ -14,7 +14,7 @@ if (flextype('registry')->get('flextype.settings.entries.fields.visibility.enabl 'visible' => 'visible', ]; - flextype('emitter')->addListener('onEntryAfterInitialized', static function () use ($visibility): void { + flextype('emitter')->addListener('onEntriesFetchSingleAfterInitialized', 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')]); } else { @@ -22,7 +22,7 @@ if (flextype('registry')->get('flextype.settings.entries.fields.visibility.enabl } }); - flextype('emitter')->addListener('onEntryCreate', static function () use ($visibility): void { + 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')]); } else {