diff --git a/src/flextype/Endpoints/entries.php b/src/flextype/Endpoints/entries.php index 52659cdd..575cc6e7 100644 --- a/src/flextype/Endpoints/entries.php +++ b/src/flextype/Endpoints/entries.php @@ -193,7 +193,7 @@ flextype()->post('/api/entries', function (Request $request, Response $response) $create_entry = flextype('entries')->create($id, $data); if ($create_entry) { - $response_data['data'] = flextype('entries')->fetchSingle($id); + $response_data['data'] = flextype('entries')->fetch($id); } else { $response_data['data'] = []; } @@ -303,7 +303,7 @@ flextype()->patch('/api/entries', function (Request $request, Response $response $update_entry = flextype('entries')->update($id, $data); if ($update_entry) { - $response_data['data'] = flextype('entries')->fetchSingle($id); + $response_data['data'] = flextype('entries')->fetch($id); } else { $response_data['data'] = []; } @@ -414,7 +414,7 @@ flextype()->put('/api/entries', function (Request $request, Response $response) // Get entry data if ($move_entry) { - $response_data['data'] = flextype('entries')->fetchSingle($new_id); + $response_data['data'] = flextype('entries')->fetch($new_id); } else { $response_data['data'] = []; } @@ -525,7 +525,7 @@ flextype()->put('/api/entries/copy', function (Request $request, Response $respo // Get entry data if ($copy_entry === null) { - $response_data['data'] = flextype('entries')->fetchSingle($new_id); + $response_data['data'] = flextype('entries')->fetch($new_id); } else { $response_data['data'] = []; } diff --git a/src/flextype/Foundation/Entries/Entries.php b/src/flextype/Foundation/Entries/Entries.php index ac2e2f79..b8b3f69f 100755 --- a/src/flextype/Foundation/Entries/Entries.php +++ b/src/flextype/Foundation/Entries/Entries.php @@ -214,151 +214,6 @@ class Entries return $single($this->storage['fetch']['id'], $this->storage['fetch']['options']); } - - } - - /** - * Fetch single entry. - * - * @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 fetchSingle(string $id, array $options = []): Arrays - { - // Store data - $this->setStorage('fetch.id', $id); - $this->setStorage('fetch.options', $options); - $this->setStorage('fetch.data', []); - - // Run event: onEntriesFetchSingle - flextype('emitter')->emit('onEntriesFetchSingle'); - - // Get Cache ID for current requested entry - $entryCacheID = $this->getCacheID($this->getStorage('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', [])); - - // Run event: onEntriesFetchSingleCacheHasResult - flextype('emitter')->emit('onEntriesFetchSingleCacheHasResult'); - - // Return entry from cache - return arrays($this->getStorage('fetch.data')); - } - - // 2. Try to get current requested entry from filesystem - if ($this->has($this->getStorage('fetch.id'))) { - // Get entry file location - $entryFile = $this->getFileLocation($this->getStorage('fetch.id')); - - // Try to get requested entry from the filesystem - $entryFileContent = filesystem()->file($entryFile)->get(); - - if ($entryFileContent === false) { - // Run event: onEntriesFetchSingleNoResult - flextype('emitter')->emit('onEntriesFetchSingleNoResult'); - return arrays($this->getStorage('fetch.data')); - } - - // Decode entry file content - $this->setStorage('fetch.data', flextype('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', [])); - - // Set cache state - $cache = $this->getStorage('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')); - } - - // Return entry data - return arrays($this->getStorage('fetch.data')); - } - - // Run event: onEntriesFetchSingleNoResult - flextype('emitter')->emit('onEntriesFetchSingleNoResult'); - - // Return empty array if entry is not founded - return arrays($this->getStorage('fetch.data')); - } - - /** - * Fetch entries collection. - * - * @param string $id Unique identifier of the entries collecton. - * @param array $options Options array. - * - * @access public - * - * @return self Returns instance of The Arrays class. - */ - public function fetchCollection(string $id, array $options = []): Arrays - { - // Run event: onEntriesFetchCollection - flextype('emitter')->emit('onEntriesFetchCollection'); - - if (! $this->getDirectoryLocation($id)) { - // Run event: onEntriesFetchCollectionNoResult - flextype('emitter')->emit('onEntriesFetchCollectionNoResult'); - - // Return entries array - return arrays($this->getStorage('fetch.data')); - } - - // Find entries in the filesystem - $entries = find($this->getDirectoryLocation($id), - isset($options['find']) ? - $options['find'] : - []); - - // 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')) { - continue; - } - - $currentEntryID = strings($currenEntry->getPath()) - ->replace('\\', '/') - ->replace(PATH['project'] . '/entries/', '') - ->trim('/') - ->toString(); - - $data[$currentEntryID] = $this->fetchSingle($currentEntryID)->toArray(); - } - - $this->setStorage('fetch.data', $data); - - // Run event: onEntriesFetchCollectionHasResult - flextype('emitter')->emit('onEntriesFetchCollectionHasResult'); - - // Apply filter for fetch data - $this->setStorage('fetch.data', filter($this->getStorage('fetch.data'), - isset($options['filter']) ? - $options['filter'] : - [])); - } - - // Run event: onEntriesFetchCollectionNoResult - flextype('emitter')->emit('onEntriesFetchCollectionNoResult'); - - // Return entries array - return arrays($this->getStorage('fetch.data')); } /** 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/EntriesTest.php b/tests/Foundation/Entries/EntriesTest.php index 1418827f..3c6e79e4 100644 --- a/tests/Foundation/Entries/EntriesTest.php +++ b/tests/Foundation/Entries/EntriesTest.php @@ -27,28 +27,28 @@ test('test update() method', function () { $this->assertFalse(flextype('entries')->update('bar', ['title' => 'Test'])); }); -test('test fetchSingle() and fetchCollection() entry', function () { +test('test fetch() entry', function () { flextype('entries')->create('foo', ['title' => 'Foo']); flextype('entries')->create('foo/bar', ['title' => 'Bar']); flextype('entries')->create('foo/baz', ['title' => 'Baz']); flextype('entries')->create('foo/zed', ['title' => 'Zed']); - $this->assertEquals(12, flextype('entries')->fetchSingle('foo')->count()); - $this->assertEquals('foo', flextype('entries')->fetchSingle('foo')['id']); - $this->assertEquals(12, flextype('entries')->fetchSingle('foo', [])->count()); - $this->assertEquals('foo', flextype('entries')->fetchSingle('foo')['id']); - $this->assertEquals(3, flextype('entries')->fetchCollection('foo')->count()); + $this->assertEquals(12, flextype('entries')->fetch('foo')->count()); + $this->assertEquals('foo', flextype('entries')->fetch('foo')['id']); + $this->assertEquals(12, flextype('entries')->fetch('foo', [])->count()); + $this->assertEquals('foo', flextype('entries')->fetch('foo')['id']); + $this->assertEquals(3, flextype('entries')->fetch('foo', ['collection' => true])->count()); - $this->assertEquals('Bar', flextype('entries')->fetchSingle('foo/bar')['title']); - $this->assertEquals('Baz', flextype('entries')->fetchSingle('foo/baz')['title']); - $this->assertEquals('Zed', flextype('entries')->fetchSingle('foo/zed')['title']); + $this->assertEquals('Bar', flextype('entries')->fetch('foo/bar')['title']); + $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'); - $this->assertEquals(0, flextype('entries')->fetchSingle('wrong-entry')->count()); + $this->assertEquals(0, flextype('entries')->fetch('wrong-entry')->count()); flextype('entries')->setStorage('fetch.id', 'wrong-entry'); - $this->assertEquals(0, flextype('entries')->fetchSingle('wrong-entry')->count()); + $this->assertEquals(0, flextype('entries')->fetch('wrong-entry')->count()); - $this->assertTrue(count(flextype('entries')->fetchCollection('foo')) > 0); + $this->assertTrue(count(flextype('entries')->fetch('foo', ['collection' => true])) > 0); /* flextype('emitter')->addListener('onEntriesFetchCollectionHasResult', static function (): void { @@ -137,7 +137,7 @@ test('test macro() entry', function () { flextype('entries')::macro('fetchRecentPosts', function($limit = 1) { return flextype('entries') - ->fetchCollection('foo') + ->fetch('foo') ->sortBy('published_at') ->limit($limit); }); 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/EntriesFieldTest.php b/tests/Foundation/Entries/Fields/EntriesFieldTest.php index efdef6fc..c1588e4f 100644 --- a/tests/Foundation/Entries/Fields/EntriesFieldTest.php +++ b/tests/Foundation/Entries/Fields/EntriesFieldTest.php @@ -15,7 +15,7 @@ test('test entries field for blog', function () { flextype('entries')->create('blog/post-1', flextype('frontmatter')->decode(filesystem()->file(ROOT_DIR . '/tests/Foundation/Entries/Fields/fixtures/entries/blog/post-1/entry.md')->get())); flextype('entries')->create('blog/post-2', flextype('frontmatter')->decode(filesystem()->file(ROOT_DIR . '/tests/Foundation/Entries/Fields/fixtures/entries/blog/post-2/entry.md')->get())); - $blog = flextype('entries')->fetchSingle('blog'); + $blog = flextype('entries')->fetch('blog'); $this->assertEquals(14, $blog->count()); }); @@ -58,7 +58,7 @@ test('test entries field for catalog', function () { $catalogLongCollecion = flextype('entries')->fetch('catalog', ['collection' => true, 'find' => ['depth' => ['>0', '<4']]]); $this->assertEquals(5, $catalogLongCollecion->count()); - $banner = flextype('entries')->fetchSingle('banner'); + $banner = flextype('entries')->fetch('banner'); $this->assertEquals('Banner', $banner['title']); $this->assertEquals('banner', $banner['id']); }); 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('