1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-08 22:26:46 +02:00

feat(entries): Entries API fetch improvements. #491

This commit is contained in:
Awilum
2020-12-11 13:10:56 +03:00
parent fe7eb314ac
commit 4abcc46426
10 changed files with 42 additions and 27 deletions

View File

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

View File

@@ -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 {

View File

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

View File

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

View File

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

View File

@@ -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 {

View File

@@ -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 {

View File

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

View File

@@ -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 {

View File

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