diff --git a/src/flextype/Foundation/Entries/Entries.php b/src/flextype/Foundation/Entries/Entries.php index 4cd54339..94442315 100755 --- a/src/flextype/Foundation/Entries/Entries.php +++ b/src/flextype/Foundation/Entries/Entries.php @@ -81,11 +81,16 @@ class Entries // Run event: onEntriesFetch flextype('emitter')->emit('onEntriesFetch'); - if (isset($options['collection']) && strings($options['collection'])->isTrue()) { - return $this->fetchCollection($id, $options); + // Fetch collection + if (isset($this->storage['fetch']['options']['collection']) && + strings($this->storage['fetch']['options']['collection'])->isTrue()) { + return $this->fetchCollection($this->storage['fetch']['id'], + $this->storage['fetch']['options']); } - return $this->fetchSingle($id, $options); + // Fetch single + return $this->fetchSingle($this->storage['fetch']['id'], + $this->storage['fetch']['options']); } /** @@ -116,11 +121,12 @@ class Entries // Fetch entry from cache $this->storage['fetch']['data'] = flextype('cache')->get($entryCacheID); - // Run event: onEntriesFetchSingleAfterCacheInitialized - flextype('emitter')->emit('onEntriesFetchSingleAfterCacheInitialized'); - // Apply filter for fetch data - $this->storage['fetch']['data'] = filter($this->storage['fetch']['data'], $options); + $this->storage['fetch']['data'] = filter($this->storage['fetch']['data'], + $this->storage['fetch']['options']); + + // Run event: onEntriesFetchSingleCacheHasResult + flextype('emitter')->emit('onEntriesFetchSingleCacheHasResult'); // Return entry from cache return arrays($this->storage['fetch']['data']); @@ -140,9 +146,6 @@ class Entries // Decode entry file content $this->storage['fetch']['data'] = flextype('frontmatter')->decode($entryFileContent); - // Run event: onEntriesFetchSingleAfterInitialized - flextype('emitter')->emit('onEntriesFetchSingleAfterInitialized'); - // Set cache state $cache = flextype('entries')->storage['fetch']['data']['cache']['enabled'] ?? flextype('registry')->get('flextype.settings.cache.enabled'); @@ -153,14 +156,21 @@ class Entries } // Apply filter for fetch data - $this->storage['fetch']['data'] = filter($this->storage['fetch']['data'], $options); + $this->storage['fetch']['data'] = filter($this->storage['fetch']['data'], + $this->storage['fetch']['options']); + + // Run event: onEntriesFetchSingleHasResult + flextype('emitter')->emit('onEntriesFetchSingleHasResult'); // Return entry data return arrays($this->storage['fetch']['data']); } + // Run event: onEntriesFetchSingleNoResult + flextype('emitter')->emit('onEntriesFetchSingleNoResult'); + // Return empty array if entry is not founded - return arrays(); + return arrays($this->storage['fetch']['data']); } /** @@ -183,9 +193,11 @@ class Entries // Run event: onEntriesFetchCollection flextype('emitter')->emit('onEntriesFetchCollection'); - // Find entries - $entries = find($this->getDirectoryLocation($this->storage['fetch']['id']), $options); + // Find entries in the filesystem + $entries = find($this->getDirectoryLocation($this->storage['fetch']['id']), + $this->storage['fetch']['options']); + // Walk through entries results if ($entries->hasResults()) { foreach ($entries as $currenEntry) { if ($currenEntry->getType() !== 'file' || $currenEntry->getFilename() !== 'entry' . '.' . flextype('registry')->get('flextype.settings.entries.extension')) { @@ -205,12 +217,15 @@ class Entries $this->storage['fetch']['id'] = $id; // Apply filter for fetch data - $this->storage['fetch']['data'] = filter($data, $options); + $this->storage['fetch']['data'] = filter($data, $this->storage['fetch']['options']); - // Run event: onEntriesFetchCollectionAfterInitialized - flextype('emitter')->emit('onEntriesFetchCollectionAfterInitialized'); + // Run event: onEntriesFetchCollectionHasResult + flextype('emitter')->emit('onEntriesFetchCollectionHasResult'); } + // Run event: onEntriesFetchCollectionNoResult + flextype('emitter')->emit('onEntriesFetchCollectionNoResult'); + // Return entries array return arrays($this->storage['fetch']['data']); } diff --git a/src/flextype/Foundation/Entries/Fields/CreatedAtField.php b/src/flextype/Foundation/Entries/Fields/CreatedAtField.php index cffe8b7b..fa4a6dbd 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('onEntriesFetchSingleAfterInitialized', static function (): void { + 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()); } else { diff --git a/src/flextype/Foundation/Entries/Fields/IdField.php b/src/flextype/Foundation/Entries/Fields/IdField.php index e6b95937..45e3dc20 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('onEntriesFetchSingleAfterInitialized', static function (): void { + flextype('emitter')->addListener('onEntriesFetchSingleHasResult', 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 255f0c4f..b2a42bbc 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('onEntriesFetchSingleAfterInitialized', static function (): void { + flextype('emitter')->addListener('onEntriesFetchSingleHasResult', 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 69cea88b..4beb0385 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('onEntriesFetchSingleAfterInitialized', static function (): void { + flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void { processParsersField(); }); } diff --git a/src/flextype/Foundation/Entries/Fields/PublishedAtField.php b/src/flextype/Foundation/Entries/Fields/PublishedAtField.php index a5d048a0..0caa118d 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('onEntriesFetchSingleAfterInitialized', static function (): void { + 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()); } else { diff --git a/src/flextype/Foundation/Entries/Fields/RoutableField.php b/src/flextype/Foundation/Entries/Fields/RoutableField.php index d04d9a5d..7812a0b4 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('onEntriesFetchSingleAfterInitialized', static function (): void { + flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void { if (flextype('entries')->getStorage('fetch.data.routable') === null) { flextype('entries')->setStorage('fetch.data.routable', true); } else { diff --git a/src/flextype/Foundation/Entries/Fields/SlugField.php b/src/flextype/Foundation/Entries/Fields/SlugField.php index 7b94c15e..bc38ae1c 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('onEntriesFetchSingleAfterInitialized', static function (): void { + flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void { if (flextype('entries')->getStorage('fetch.data.slug') !== null) { return; } diff --git a/src/flextype/Foundation/Entries/Fields/VisibilityField.php b/src/flextype/Foundation/Entries/Fields/VisibilityField.php index 98b08934..16845362 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('onEntriesFetchSingleAfterInitialized', static function () use ($visibility): void { + 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')]); } else { diff --git a/tests/Foundation/Entries/EntriesTest.php b/tests/Foundation/Entries/EntriesTest.php index 167ab3ce..9ff1034a 100644 --- a/tests/Foundation/Entries/EntriesTest.php +++ b/tests/Foundation/Entries/EntriesTest.php @@ -41,11 +41,11 @@ test('test fetch() entry', function () { $this->assertEquals('Baz', flextype('entries')->fetch('foo/baz')['title']); $this->assertEquals('Zed', flextype('entries')->fetch('foo/zed')['title']); - flextype('emitter')->addListener('onEntriesFetchCollectionAfterInitialized', static function (): void { + flextype('emitter')->addListener('onEntriesFetchCollectionHasResult', static function (): void { flextype('entries')->setStorage('fetch.data.foo/zed.title', 'ZedFromCollection!'); }); - flextype('emitter')->addListener('onEntriesFetchCollectionAfterInitialized', static function (): void { + flextype('emitter')->addListener('onEntriesFetchCollectionHasResult', static function (): void { flextype('entries')->setStorage('fetch.data.foo/baz.title', 'BazFromCollection!'); });