1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-08 06:06:45 +02:00

feat(entries): add fetch() method with ability to execute different methods with fetch prefix. #495

This commit is contained in:
Awilum
2020-12-17 12:43:30 +03:00
parent 1c6eafd5b0
commit ca8f338ccf
18 changed files with 45 additions and 188 deletions

View File

@@ -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'] = [];
}

View File

@@ -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'));
}
/**

View File

@@ -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'));
});
}

View File

@@ -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);
});

View File

@@ -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));
});

View File

@@ -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);
});

View File

@@ -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']);
});

View File

@@ -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);
});

View File

@@ -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));

View File

@@ -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('<h1>Foo</h1>', flextype('entries')->fetchSingle('foo')['content']);
$this->assertEquals('<h1>Foo</h1>', flextype('entries')->fetch('foo')['content']);
flextype('entries')->create('bar', ['content' => '[registry_get name="Bar" default="Zed"]', 'parsers' => ['shortcode' => ['enabled' => true, 'fields' => ['content']]]]);
$this->assertEquals('Zed', flextype('entries')->fetchSingle('bar')['content']);
$this->assertEquals('Zed', flextype('entries')->fetch('bar')['content']);
});

View File

@@ -13,7 +13,7 @@ afterEach(function (): void {
test('test PublishedAtField', function () {
flextype('entries')->create('foo', []);
$published_at = flextype('entries')->fetchSingle('foo')['published_at'];
$published_at = flextype('entries')->fetch('foo')['published_at'];
$this->assertTrue(strlen($published_at) > 0);
$this->assertTrue((ctype_digit($published_at) && strtotime(date('Y-m-d H:i:s', $published_at)) === (int)$published_at));

View File

@@ -12,10 +12,10 @@ afterEach(function (): void {
test('test PublishedByField', function () {
flextype('entries')->create('foo', []);
$published_by = flextype('entries')->fetchSingle('foo')['published_by'];
$published_by = flextype('entries')->fetch('foo')['published_by'];
$this->assertEquals('', $published_by);
flextype('entries')->create('bar', ['published_by' => 'Zed']);
$published_by = flextype('entries')->fetchSingle('bar')['published_by'];
$published_by = flextype('entries')->fetch('bar')['published_by'];
$this->assertEquals('Zed', $published_by);
});

View File

@@ -15,7 +15,7 @@ test('test registry field', function () {
flextype('entries')->create('registry-root/level-1', flextype('frontmatter')->decode(filesystem()->file(ROOT_DIR . '/tests/Foundation/Entries/Fields/fixtures/entries/registry-root/level-1/entry.md')->get()));
flextype('entries')->create('registry-root/level-1/level-2', flextype('frontmatter')->decode(filesystem()->file(ROOT_DIR . '/tests/Foundation/Entries/Fields/fixtures/entries/registry-root/level-1/level-2/entry.md')->get()));
$data = flextype('entries')->fetchSingle('registry-root');
$data = flextype('entries')->fetch('registry-root');
$this->assertEquals('Flextype', $data['flextype']);
$this->assertEquals('Sergey Romanenko', $data['author']['name']);

View File

@@ -14,14 +14,14 @@ test('test RoutableField', function () {
flextype('registry')->set('flextype.settings.cache.enabled', false);
flextype('entries')->create('foo', ['routable' => true]);
$routable = flextype('entries')->fetchSingle('foo')['routable'];
$routable = flextype('entries')->fetch('foo')['routable'];
$this->assertTrue($routable);
flextype('entries')->create('bar', []);
$routable = flextype('entries')->fetchSingle('bar')['routable'];
$routable = flextype('entries')->fetch('bar')['routable'];
$this->assertTrue($routable);
flextype('entries')->create('zed', ['routable' => false]);
$routable = flextype('entries')->fetchSingle('zed')['routable'];
$routable = flextype('entries')->fetch('zed')['routable'];
$this->assertFalse($routable);
});

View File

@@ -12,10 +12,10 @@ afterEach(function (): void {
test('test SlugField', function () {
flextype('entries')->create('foo', []);
$slug = flextype('entries')->fetchSingle('foo')['slug'];
$slug = flextype('entries')->fetch('foo')['slug'];
$this->assertEquals('foo', $slug);
flextype('entries')->create('bar', []);
$slug = flextype('entries')->fetchSingle('bar')['slug'];
$slug = flextype('entries')->fetch('bar')['slug'];
$this->assertEquals('bar', $slug);
});

View File

@@ -14,6 +14,6 @@ afterEach(function (): void {
test('test UuidField', function () {
flextype('entries')->create('foo', []);
$uuid = flextype('entries')->fetchSingle('foo')['uuid'];
$uuid = flextype('entries')->fetch('foo')['uuid'];
$this->assertTrue(v::uuid()->validate($uuid));
});

View File

@@ -12,14 +12,14 @@ afterEach(function (): void {
test('test VisibilityField', function () {
flextype('entries')->create('foo', []);
$visibility = flextype('entries')->fetchSingle('foo')['visibility'];
$visibility = flextype('entries')->fetch('foo')['visibility'];
$this->assertEquals('visible', $visibility);
flextype('entries')->create('bar', ['visibility' => 'draft']);
$visibility = flextype('entries')->fetchSingle('bar')['visibility'];
$visibility = flextype('entries')->fetch('bar')['visibility'];
$this->assertEquals('draft', $visibility);
flextype('entries')->create('zed', ['visibility' => 'foobar']);
$visibility = flextype('entries')->fetchSingle('zed')['visibility'];
$visibility = flextype('entries')->fetch('zed')['visibility'];
$this->assertEquals('visible', $visibility);
});

View File

@@ -1,17 +1,19 @@
---
title: Album 1
entries:
fetchSingle:
fetch:
banner-single:
id: banners
banner-single-2:
id: banners/2
fetchCollection:
banners-collection:
id: banners
options:
collection: true
banners-collection-2:
id: banners
options:
collection: true
filter:
limit: 1
find: