diff --git a/src/flextype/Foundation/Entries/Entries.php b/src/flextype/Foundation/Entries/Entries.php index a8811f92..b23c8b06 100755 --- a/src/flextype/Foundation/Entries/Entries.php +++ b/src/flextype/Foundation/Entries/Entries.php @@ -69,28 +69,24 @@ class Entries * * @access public * - * @return self Returns instance of The Arrays class. + * @return mixed */ - public function fetch(string $id, array $options = []): Arrays + public function fetch(string $id, $from = 'single', array $options = []) { - // Store data + // Store data in EMS. $this->storage['fetch']['id'] = $id; + $this->storage['fetch']['from'] = $from; $this->storage['fetch']['options'] = $options; - $this->storage['fetch']['data'] = []; // Run event: onEntriesFetch flextype('emitter')->emit('onEntriesFetch'); - // 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']); - } - - // Fetch single - return $this->fetchSingle($this->storage['fetch']['id'], - $this->storage['fetch']['options']); + // Return fetch result + return $this->{strings($this->storage['fetch']['from']) + ->studly() + ->prepend('fetch') + ->toString()}($this->storage['fetch']['id'], + $this->storage['fetch']['options']); } /** @@ -106,34 +102,36 @@ class Entries public function fetchSingle(string $id, array $options = []): Arrays { // Store data - $this->storage['fetch']['id'] = $id; - $this->storage['fetch']['options'] = $options; - $this->storage['fetch']['data'] = []; + $this->storage['fetch_single']['id'] = $id; + $this->storage['fetch_single']['options'] = $options; + $this->storage['fetch_single']['data'] = []; // Run event: onEntriesFetchSingle flextype('emitter')->emit('onEntriesFetchSingle'); // Get Cache ID for current requested entry - $entryCacheID = $this->getCacheID($this->storage['fetch']['id']); + $entryCacheID = $this->getCacheID($this->storage['fetch_single']['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->storage['fetch']['options']); + $this->storage['fetch_single']['data'] = filter(flextype('cache')->get($entryCacheID), + isset($this->storage['fetch_single']['options']['filter']) ? + $this->storage['fetch_single']['options']['filter'] : + []); // Run event: onEntriesFetchSingleCacheHasResult flextype('emitter')->emit('onEntriesFetchSingleCacheHasResult'); // Return entry from cache - return arrays($this->storage['fetch']['data']); + return arrays($this->storage['fetch_single']['data']); } // 2. Try to get current requested entry from filesystem - if ($this->has($this->storage['fetch']['id'])) { + if ($this->has($this->storage['fetch_single']['id'])) { // Get entry file location - $entryFile = $this->getFileLocation($this->storage['fetch']['id']); + $entryFile = $this->getFileLocation($this->storage['fetch_single']['id']); // Try to get requested entry from the filesystem $entryFileContent = filesystem()->file($entryFile)->get(); @@ -141,34 +139,39 @@ class Entries if ($entryFileContent === false) { // Run event: onEntriesFetchSingleNoResult flextype('emitter')->emit('onEntriesFetchSingleNoResult'); - return arrays($this->storage['fetch']['data']); + return arrays($this->storage['fetch_single']['data']); } - // Decode entry file content and Apply filter for fetch data - $this->storage['fetch']['data'] = filter(flextype('frontmatter')->decode($entryFileContent), - $this->storage['fetch']['options']); + // Decode entry file content + $this->storage['fetch_single']['data'] = flextype('frontmatter')->decode($entryFileContent); // Run event: onEntriesFetchSingleHasResult flextype('emitter')->emit('onEntriesFetchSingleHasResult'); + // Apply filter for fetch data + $this->storage['fetch_single']['data'] = filter($this->storage['fetch_single']['data'], + isset($this->storage['fetch_single']['options']['filter']) ? + $this->storage['fetch_single']['options']['filter'] : + []); + // Set cache state - $cache = flextype('entries')->storage['fetch']['data']['cache']['enabled'] ?? + $cache = flextype('entries')->storage['fetch_single']['data']['cache']['enabled'] ?? flextype('registry')->get('flextype.settings.cache.enabled'); // Save entry data to cache if ($cache) { - flextype('cache')->set($entryCacheID, $this->storage['fetch']['data']); + flextype('cache')->set($entryCacheID, $this->storage['fetch_single']['data']); } // Return entry data - return arrays($this->storage['fetch']['data']); + return arrays($this->storage['fetch_single']['data']); } // Run event: onEntriesFetchSingleNoResult flextype('emitter')->emit('onEntriesFetchSingleNoResult'); // Return empty array if entry is not founded - return arrays($this->storage['fetch']['data']); + return arrays($this->storage['fetch_single']['data']); } /** @@ -184,16 +187,18 @@ class Entries public function fetchCollection(string $id, array $options = []): Arrays { // Store data - $this->storage['fetch']['id'] = $id; - $this->storage['fetch']['options'] = $options; - $this->storage['fetch']['data'] = []; + $this->storage['fetch_collection']['id'] = $id; + $this->storage['fetch_collection']['options'] = $options; + $this->storage['fetch_collection']['data'] = []; // Run event: onEntriesFetchCollection flextype('emitter')->emit('onEntriesFetchCollection'); // Find entries in the filesystem - $entries = find($this->getDirectoryLocation($this->storage['fetch']['id']), - $this->storage['fetch']['options']); + $entries = find($this->getDirectoryLocation($this->storage['fetch_collection']['id']), + isset($this->storage['fetch_collection']['options']['find']) ? + $this->storage['fetch_collection']['options']['find'] : + []); // Walk through entries results if ($entries->hasResults()) { @@ -211,21 +216,24 @@ class Entries $data[$currentEntryID] = $this->fetchSingle($currentEntryID)->toArray(); } - // Restore fetch id - $this->storage['fetch']['id'] = $id; - - // Apply filter for fetch data - $this->storage['fetch']['data'] = filter($data, $this->storage['fetch']['options']); + $this->storage['fetch_collection']['data'] = $data; // Run event: onEntriesFetchCollectionHasResult flextype('emitter')->emit('onEntriesFetchCollectionHasResult'); + + // Apply filter for fetch data + $this->storage['fetch_collection']['data'] = filter($this->storage['fetch_collection']['data'], + isset($this->storage['fetch_collection']['options']['filter']) ? + $this->storage['fetch_collection']['options']['filter'] : + []); + } // Run event: onEntriesFetchCollectionNoResult flextype('emitter')->emit('onEntriesFetchCollectionNoResult'); // Return entries array - return arrays($this->storage['fetch']['data']); + return arrays($this->storage['fetch_collection']['data']); } /** diff --git a/src/flextype/Foundation/Entries/Fields/CreatedAtField.php b/src/flextype/Foundation/Entries/Fields/CreatedAtField.php index fa4a6dbd..984c79fd 100644 --- a/src/flextype/Foundation/Entries/Fields/CreatedAtField.php +++ b/src/flextype/Foundation/Entries/Fields/CreatedAtField.php @@ -10,10 +10,10 @@ 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')->getStorage('fetch_single.data.created_at') === null) { + flextype('entries')->setStorage('fetch_single.data.created_at', (int) filesystem()->file(flextype('entries')->getFileLocation(flextype('entries')->getStorage('fetch_single.id')))->lastModified()); } else { - flextype('entries')->setStorage('fetch.data.created_at', (int) strtotime((string) flextype('entries')->getStorage('fetch.data.created_at'))); + flextype('entries')->setStorage('fetch_single.data.created_at', (int) strtotime((string) flextype('entries')->getStorage('fetch_single.data.created_at'))); } }); diff --git a/src/flextype/Foundation/Entries/Fields/IdField.php b/src/flextype/Foundation/Entries/Fields/IdField.php index 45e3dc20..5c6ef1b5 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')->getStorage('fetch_single.data.id') !== null) { return; } - flextype('entries')->setStorage('fetch.data.id', (string) strings(flextype('entries')->getStorage('fetch.id'))->trimSlashes()); + flextype('entries')->setStorage('fetch_single.data.id', (string) strings(flextype('entries')->getStorage('fetch_single.id'))->trimSlashes()); }); } diff --git a/src/flextype/Foundation/Entries/Fields/ModifiedAtField.php b/src/flextype/Foundation/Entries/Fields/ModifiedAtField.php index b2a42bbc..382cfaa1 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')->getStorage('fetch_single.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')->setStorage('fetch_single.data.modified_at', (int) filesystem()->file(flextype('entries')->getFileLocation(flextype('entries')->getStorage('fetch_single.id')))->lastModified()); }); } diff --git a/src/flextype/Foundation/Entries/Fields/ParsersField.php b/src/flextype/Foundation/Entries/Fields/ParsersField.php index 4beb0385..79fe0048 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')->getStorage('fetch_single.data.cache.enabled') == null) { $cache = false; } else { - $cache = (bool) flextype('entries')->getStorage('fetch.data.cache.enabled'); + $cache = (bool) flextype('entries')->getStorage('fetch_single.data.cache.enabled'); } - if (flextype('entries')->getStorage('fetch.data.parsers') != null) { - foreach (flextype('entries')->getStorage('fetch.data.parsers') as $parserName => $parserData) { + if (flextype('entries')->getStorage('fetch_single.data.parsers') != null) { + foreach (flextype('entries')->getStorage('fetch_single.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')->getStorage('fetch_single.data.parsers.'.$parserName.'.enabled') === true) { + if (flextype('entries')->getStorage('fetch_single.data.parsers.'.$parserName.'.fields') != null) { + if (is_array(flextype('entries')->getStorage('fetch_single.data.parsers.'.$parserName.'.fields'))) { + foreach (flextype('entries')->getStorage('fetch_single.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('markdown')->parse(flextype('entries')->getStorage('fetch.data.'.$field), $cache)); + if (arrays(flextype('entries')->getStorage('fetch_single.data'))->has($field)) { + flextype('entries')->setStorage('fetch_single.data.'.$field, + flextype('markdown')->parse(flextype('entries')->getStorage('fetch_single.data.'.$field), $cache)); } } if ($parserName == 'shortcode') { - if (arrays(flextype('entries')->getStorage('fetch.data'))->has($field)) { - flextype('entries')->setStorage('fetch.data.'.$field, - flextype('shortcode')->process(flextype('entries')->getStorage('fetch.data.'.$field), $cache)); + if (arrays(flextype('entries')->getStorage('fetch_single.data'))->has($field)) { + flextype('entries')->setStorage('fetch_single.data.'.$field, + flextype('shortcode')->process(flextype('entries')->getStorage('fetch_single.data.'.$field), $cache)); } } } diff --git a/src/flextype/Foundation/Entries/Fields/PublishedAtField.php b/src/flextype/Foundation/Entries/Fields/PublishedAtField.php index 0caa118d..d8399d45 100644 --- a/src/flextype/Foundation/Entries/Fields/PublishedAtField.php +++ b/src/flextype/Foundation/Entries/Fields/PublishedAtField.php @@ -9,10 +9,10 @@ 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')->getStorage('fetch_single.data.published_at') === null) { + flextype('entries')->setStorage('fetch_single.data.published_at', (int) filesystem()->file(flextype('entries')->getFileLocation(flextype('entries')->getStorage('fetch_single.id')))->lastModified()); } else { - flextype('entries')->setStorage('fetch.data.published_at', (int) strtotime((string) flextype('entries')->getStorage('fetch.data.published_at'))); + flextype('entries')->setStorage('fetch_single.data.published_at', (int) strtotime((string) flextype('entries')->getStorage('fetch_single.data.published_at'))); } }); diff --git a/src/flextype/Foundation/Entries/Fields/RoutableField.php b/src/flextype/Foundation/Entries/Fields/RoutableField.php index 7812a0b4..e86c3625 100644 --- a/src/flextype/Foundation/Entries/Fields/RoutableField.php +++ b/src/flextype/Foundation/Entries/Fields/RoutableField.php @@ -10,10 +10,10 @@ 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')->getStorage('fetch_single.data.routable') === null) { + flextype('entries')->setStorage('fetch_single.data.routable', true); } else { - flextype('entries')->setStorage('fetch.data.routable', (bool) flextype('entries')->getStorage('fetch.data.routable')); + flextype('entries')->setStorage('fetch_single.data.routable', (bool) flextype('entries')->getStorage('fetch_single.data.routable')); } }); diff --git a/src/flextype/Foundation/Entries/Fields/SlugField.php b/src/flextype/Foundation/Entries/Fields/SlugField.php index bc38ae1c..578f173f 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')->getStorage('fetch_single.data.slug') !== null) { return; } - $parts = strings(flextype('entries')->getStorage('fetch.id'))->trimSlashes()->segments(); - flextype('entries')->setStorage('fetch.data.slug', (string) end($parts)); + $parts = strings(flextype('entries')->getStorage('fetch_single.id'))->trimSlashes()->segments(); + flextype('entries')->setStorage('fetch_single.data.slug', (string) end($parts)); }); } diff --git a/src/flextype/Foundation/Entries/Fields/VisibilityField.php b/src/flextype/Foundation/Entries/Fields/VisibilityField.php index 16845362..fe8485d3 100644 --- a/src/flextype/Foundation/Entries/Fields/VisibilityField.php +++ b/src/flextype/Foundation/Entries/Fields/VisibilityField.php @@ -15,10 +15,10 @@ 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')->getStorage('fetch_single.data.visibility') !== null && in_array(flextype('entries')->getStorage('fetch_single.data.visibility'), $visibility)) { + flextype('entries')->setStorage('fetch_single.data.visibility', (string) $visibility[flextype('entries')->getStorage('fetch_single.data.visibility')]); } else { - flextype('entries')->setStorage('fetch.data.visibility', (string) $visibility['visible']); + flextype('entries')->setStorage('fetch_single.data.visibility', (string) $visibility['visible']); } }); diff --git a/src/flextype/Support/Parsers/Shortcodes/EntriesShortcode.php b/src/flextype/Support/Parsers/Shortcodes/EntriesShortcode.php index c8ad6c4f..8fd886b0 100644 --- a/src/flextype/Support/Parsers/Shortcodes/EntriesShortcode.php +++ b/src/flextype/Support/Parsers/Shortcodes/EntriesShortcode.php @@ -12,6 +12,6 @@ use Thunder\Shortcode\Shortcode\ShortcodeInterface; // Shortcode: [entries_fetch id="entry-id" field="field-name" default="default-value"] if (flextype('registry')->get('flextype.settings.shortcode.shortcodes.entries.enabled')) { flextype('shortcode')->addHandler('entries_fetch', static function (ShortcodeInterface $s) { - return arrays(flextype('entries')->fetchSingle($s->getParameter('id')))->get($s->getParameter('field'), $s->getParameter('default')); + return arrays(flextype('entries')->fetch($s->getParameter('id')))->get($s->getParameter('field'), $s->getParameter('default')); }); } diff --git a/tests/Foundation/Entries/Fields/CreatedAtFieldTest.php b/tests/Foundation/Entries/Fields/CreatedAtFieldTest.php index 4d95f539..b9fc101e 100644 --- a/tests/Foundation/Entries/Fields/CreatedAtFieldTest.php +++ b/tests/Foundation/Entries/Fields/CreatedAtFieldTest.php @@ -13,7 +13,7 @@ afterEach(function (): void { test('test CreatedAtField', function () { // 1 flextype('entries')->create('foo', []); - $created_at = flextype('entries')->fetchSingle('foo')['created_at']; + $created_at = flextype('entries')->fetch('foo')['created_at']; $this->assertTrue(strlen($created_at) > 0); $this->assertTrue((ctype_digit($created_at) && strtotime(date('Y-m-d H:i:s', $created_at)) === (int)$created_at)); }); diff --git a/tests/Foundation/Entries/Fields/CreatedByFieldTest.php b/tests/Foundation/Entries/Fields/CreatedByFieldTest.php index 22864492..c97aa8c9 100644 --- a/tests/Foundation/Entries/Fields/CreatedByFieldTest.php +++ b/tests/Foundation/Entries/Fields/CreatedByFieldTest.php @@ -12,10 +12,10 @@ afterEach(function (): void { test('test CreatedByField', function () { flextype('entries')->create('foo', []); - $created_by = flextype('entries')->fetchSingle('foo')['created_by']; + $created_by = flextype('entries')->fetch('foo')['created_by']; $this->assertEquals('', $created_by); flextype('entries')->create('bar', ['created_by' => 'Zed']); - $created_by = flextype('entries')->fetchSingle('bar')['created_by']; + $created_by = flextype('entries')->fetch('bar')['created_by']; $this->assertEquals('Zed', $created_by); }); diff --git a/tests/Foundation/Entries/Fields/IdFieldTest.php b/tests/Foundation/Entries/Fields/IdFieldTest.php index f4816037..129ff73c 100644 --- a/tests/Foundation/Entries/Fields/IdFieldTest.php +++ b/tests/Foundation/Entries/Fields/IdFieldTest.php @@ -12,10 +12,10 @@ afterEach(function (): void { test('test IdField', function () { flextype('entries')->create('foo', []); - $id = flextype('entries')->fetchSingle('foo')['id']; + $id = flextype('entries')->fetch('foo')['id']; $this->assertEquals('foo', $id); flextype('entries')->create('foo/bar', []); - $id = flextype('entries')->fetchSingle('foo/bar')['id']; + $id = flextype('entries')->fetch('foo/bar')['id']; $this->assertEquals('foo/bar', $id); }); diff --git a/tests/Foundation/Entries/Fields/ModifiedAtFieldTest.php b/tests/Foundation/Entries/Fields/ModifiedAtFieldTest.php index 6e7a9802..549b60af 100644 --- a/tests/Foundation/Entries/Fields/ModifiedAtFieldTest.php +++ b/tests/Foundation/Entries/Fields/ModifiedAtFieldTest.php @@ -13,7 +13,7 @@ afterEach(function (): void { test('test ModifiedAtField', function () { flextype('entries')->create('foo', []); - $modified_at = flextype('entries')->fetchSingle('foo')['modified_at']; + $modified_at = flextype('entries')->fetch('foo')['modified_at']; $this->assertTrue(strlen($modified_at) > 0); $this->assertTrue((ctype_digit($modified_at) && strtotime(date('Y-m-d H:i:s', $modified_at)) === (int)$modified_at)); diff --git a/tests/Foundation/Entries/Fields/ParsersFieldTest.php b/tests/Foundation/Entries/Fields/ParsersFieldTest.php index 61cf9136..b7d92fcf 100644 --- a/tests/Foundation/Entries/Fields/ParsersFieldTest.php +++ b/tests/Foundation/Entries/Fields/ParsersFieldTest.php @@ -12,8 +12,8 @@ afterEach(function (): void { test('test ParsersField', function () { flextype('entries')->create('foo', ['content' => '#Foo', 'parsers' => ['markdown' => ['enabled' => true, 'fields' => ['content']]]]); - $this->assertEquals('