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

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

BREAKING CHANGES

events changes:

onEntriesFetch
onEntriesFetchSingle instead of onEntryInitialized
onEntriesFetchSingleAfterCacheInitialized instead of onEntryAfterCacheInitialized
onEntriesFetchSingleAfterInitialized instead of onEntryAfterInitialized
onEntriesFetchCollection instead of onEntriesInitialized
onEntriesFetchCollectionAfterInitialized instead of onEntriesAfterInitialized
onEntriesMove instead of onEntryMove
onEntriesUpdate instead of onEntryUpdate
onEntriesCreate instead of onEntryCreate
onEntriesDelete instead of onEntryDelete
onEntriesCopy instead of onEntryCopy
onEntriesHas instead of onEntryHas
This commit is contained in:
Awilum
2020-12-10 20:14:03 +03:00
parent c1e6a3f274
commit 4a68523c4a
12 changed files with 76 additions and 40 deletions

View File

@@ -40,6 +40,8 @@ class Entries
*
* @param string|int|null $key Key.
* @param mixed $default Default value.
*
* @access public
*/
public function getStorage($key, $default = null)
{
@@ -51,12 +53,41 @@ class Entries
*
* @param string|null $key Key.
* @param mixed $value Value.
*
* @access public
*/
public function setStorage(?string $key, $value): void
{
$this->storage = arrays($this->storage)->set($key, $value)->toArray();
}
/**
* Fetch.
*
* @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 fetch(string $id, array $options = []): Arrays
{
// Store data
$this->storage['fetch']['id'] = $id;
$this->storage['fetch']['options'] = $options;
$this->storage['fetch']['data'] = [];
// Run event: onEntriesFetch
flextype('emitter')->emit('onEntriesFetch');
if (isset($options['collection']) && strings($options['collection'])->isTrue()) {
return $this->fetchCollection($id, $options);
}
return $this->fetchSingle($id, $options);
}
/**
* Fetch single entry.
*
@@ -64,15 +95,18 @@ class Entries
* @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->storage['fetch']['id'] = $id;
$this->storage['fetch']['data'] = [];
$this->storage['fetch']['id'] = $id;
$this->storage['fetch']['options'] = $options;
$this->storage['fetch']['data'] = [];
// Run event: onEntryInitialized
flextype('emitter')->emit('onEntryInitialized');
// Run event: onEntriesFetchSingle
flextype('emitter')->emit('onEntriesFetchSingle');
// Get Cache ID for current requested entry
$entryCacheID = $this->getCacheID($this->storage['fetch']['id']);
@@ -82,8 +116,8 @@ class Entries
// Fetch entry from cache
$this->storage['fetch']['data'] = flextype('cache')->get($entryCacheID);
// Run event: onEntryAfterCacheInitialized
flextype('emitter')->emit('onEntryAfterCacheInitialized');
// Run event: onEntriesFetchSingleAfterCacheInitialized
flextype('emitter')->emit('onEntriesFetchSingleAfterCacheInitialized');
// Apply filter for fetch data
$this->storage['fetch']['data'] = filter($this->storage['fetch']['data'], $options);
@@ -106,8 +140,8 @@ class Entries
// Decode entry file content
$this->storage['fetch']['data'] = flextype('frontmatter')->decode($entryFileContent);
// Run event: onEntryAfterInitialized
flextype('emitter')->emit('onEntryAfterInitialized');
// Run event: onEntriesFetchSingleAfterInitialized
flextype('emitter')->emit('onEntriesFetchSingleAfterInitialized');
// Set cache state
$cache = flextype('entries')->storage['fetch']['data']['cache']['enabled'] ??
@@ -136,16 +170,18 @@ class Entries
* @param array $options Options array.
*
* @access public
*
* @return self Returns instance of The Arrays class.
*/
public function fetchCollection(string $id, array $options = []): Arrays
{
// Store data
$this->storage['fetch']['id'] = $id;
$this->storage['fetch']['data'] = [];
$this->storage['fetch']['options'] = $options;
$this->storage['fetch']['data'] = [];
// Run event: onEntriesInitialized
flextype('emitter')->emit('onEntriesInitialized');
// Run event: onEntriesFetchCollection
flextype('emitter')->emit('onEntriesFetchCollection');
// Find entries
$entries = find($this->getDirectoryLocation($this->storage['fetch']['id']), $options);
@@ -171,8 +207,8 @@ class Entries
// Apply filter for fetch data
$this->storage['fetch']['data'] = filter($data, $options);
// Run event: onEntriesAfterInitialized
flextype('emitter')->emit('onEntriesAfterInitialized');
// Run event: onEntriesFetchCollectionAfterInitialized
flextype('emitter')->emit('onEntriesFetchCollectionAfterInitialized');
}
// Return entries array
@@ -195,8 +231,8 @@ class Entries
$this->storage['move']['id'] = $id;
$this->storage['move']['new_id'] = $newID;
// Run event: onEntryMove
flextype('emitter')->emit('onEntryMove');
// Run event: onEntriesMove
flextype('emitter')->emit('onEntriesMove');
if (! $this->has($this->storage['move']['new_id'])) {
return filesystem()
@@ -223,8 +259,8 @@ class Entries
$this->storage['update']['id'] = $id;
$this->storage['update']['data'] = $data;
// Run event: onEntryUpdate
flextype('emitter')->emit('onEntryUpdate');
// Run event: onEntriesUpdate
flextype('emitter')->emit('onEntriesUpdate');
$entryFile = $this->getFileLocation($this->storage['update']['id']);
@@ -254,8 +290,8 @@ class Entries
$this->storage['create']['id'] = $id;
$this->storage['create']['data'] = $data;
// Run event: onEntryCreate
flextype('emitter')->emit('onEntryCreate');
// Run event: onEntriesCreate
flextype('emitter')->emit('onEntriesCreate');
// Create entry directory first if it is not exists
$entryDir = $this->getDirectoryLocation($this->storage['create']['id']);
@@ -290,8 +326,8 @@ class Entries
// Store data
$this->storage['delete']['id'] = $id;
// Run event: onEntryDelete
flextype('emitter')->emit('onEntryDelete');
// Run event: onEntriesDelete
flextype('emitter')->emit('onEntriesDelete');
return filesystem()
->directory($this->getDirectoryLocation($this->storage['delete']['id']))
@@ -314,8 +350,8 @@ class Entries
$this->storage['copy']['id'] = $id;
$this->storage['copy']['new_id'] = $newID;
// Run event: onEntryCopy
flextype('emitter')->emit('onEntryCopy');
// Run event: onEntriesCopy
flextype('emitter')->emit('onEntriesCopy');
return filesystem()
->directory($this->getDirectoryLocation($this->storage['copy']['id']))
@@ -336,8 +372,8 @@ class Entries
// Store data
$this->storage['has']['id'] = $id;
// Run event: onEntryHas
flextype('emitter')->emit('onEntryHas');
// Run event: onEntriesHas
flextype('emitter')->emit('onEntriesHas');
return filesystem()->file($this->getFileLocation($this->storage['has']['id']))->exists();
}

View File

@@ -9,7 +9,7 @@ declare(strict_types=1);
if (flextype('registry')->get('flextype.settings.entries.fields.created_at.enabled')) {
flextype('emitter')->addListener('onEntryAfterInitialized', static function (): void {
flextype('emitter')->addListener('onEntriesFetchSingleAfterInitialized', 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 {
@@ -17,7 +17,7 @@ if (flextype('registry')->get('flextype.settings.entries.fields.created_at.enabl
}
});
flextype('emitter')->addListener('onEntryCreate', static function (): void {
flextype('emitter')->addListener('onEntriesCreate', static function (): void {
if (flextype('entries')->getStorage('create.data.created_at') !== null) {
return;
}

View File

@@ -8,7 +8,7 @@ declare(strict_types=1);
*/
if (flextype('registry')->get('flextype.settings.entries.fields.created_by.enabled')) {
flextype('emitter')->addListener('onEntryCreate', static function (): void {
flextype('emitter')->addListener('onEntriesCreate', static function (): void {
if (flextype('entries')->getStorage('create.data.created_by') !== null) {
return;
}

View File

@@ -9,7 +9,7 @@ declare(strict_types=1);
if (flextype('registry')->get('flextype.settings.entries.fields.id.enabled')) {
flextype('emitter')->addListener('onEntryAfterInitialized', static function (): void {
flextype('emitter')->addListener('onEntriesFetchSingleAfterInitialized', 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('onEntryAfterInitialized', static function (): void {
flextype('emitter')->addListener('onEntriesFetchSingleAfterInitialized', 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('onEntryAfterInitialized', static function (): void {
flextype('emitter')->addListener('onEntriesFetchSingleAfterInitialized', 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('onEntryAfterInitialized', static function (): void {
flextype('emitter')->addListener('onEntriesFetchSingleAfterInitialized', 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 {
@@ -16,7 +16,7 @@ if (flextype('registry')->get('flextype.settings.entries.fields.published_at.ena
}
});
flextype('emitter')->addListener('onEntryCreate', static function (): void {
flextype('emitter')->addListener('onEntriesCreate', static function (): void {
if (flextype('entries')->getStorage('create.data.published_at') !== null) {
return;
}

View File

@@ -8,7 +8,7 @@ declare(strict_types=1);
*/
if (flextype('registry')->get('flextype.settings.entries.fields.published_by.enabled')) {
flextype('emitter')->addListener('onEntryCreate', static function (): void {
flextype('emitter')->addListener('onEntriesCreate', static function (): void {
if (flextype('entries')->getStorage('create.data.published_by') !== null) {
return;
}

View File

@@ -9,7 +9,7 @@ declare(strict_types=1);
if (flextype('registry')->get('flextype.settings.entries.fields.routable.enabled')) {
flextype('emitter')->addListener('onEntryAfterInitialized', static function (): void {
flextype('emitter')->addListener('onEntriesFetchSingleAfterInitialized', static function (): void {
if (flextype('entries')->getStorage('fetch.data.routable') === null) {
flextype('entries')->setStorage('fetch.data.routable', true);
} else {
@@ -17,7 +17,7 @@ if (flextype('registry')->get('flextype.settings.entries.fields.routable.enabled
}
});
flextype('emitter')->addListener('onEntryCreate', static function (): void {
flextype('emitter')->addListener('onEntriesCreate', static function (): void {
if (flextype('entries')->getStorage('create.data.routable') === null) {
flextype('entries')->setStorage('create.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('onEntryAfterInitialized', static function (): void {
flextype('emitter')->addListener('onEntriesFetchSingleAfterInitialized', static function (): void {
if (flextype('entries')->getStorage('fetch.data.slug') !== null) {
return;
}

View File

@@ -10,7 +10,7 @@ declare(strict_types=1);
use Ramsey\Uuid\Uuid;
if (flextype('registry')->get('flextype.settings.entries.fields.uuid.enabled')) {
flextype('emitter')->addListener('onEntryCreate', static function (): void {
flextype('emitter')->addListener('onEntriesCreate', static function (): void {
if (flextype('entries')->getStorage('create.data.uuid') !== null) {
return;
}

View File

@@ -14,7 +14,7 @@ if (flextype('registry')->get('flextype.settings.entries.fields.visibility.enabl
'visible' => 'visible',
];
flextype('emitter')->addListener('onEntryAfterInitialized', static function () use ($visibility): void {
flextype('emitter')->addListener('onEntriesFetchSingleAfterInitialized', 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 {
@@ -22,7 +22,7 @@ if (flextype('registry')->get('flextype.settings.entries.fields.visibility.enabl
}
});
flextype('emitter')->addListener('onEntryCreate', static function () use ($visibility): void {
flextype('emitter')->addListener('onEntriesCreate', static function () use ($visibility): void {
if (flextype('entries')->getStorage('create.data.visibility') !== null && in_array(flextype('entries')->getStorage('create.data.visibility'), $visibility)) {
flextype('entries')->setStorage('create.data.visibility', (string) $visibility[flextype('entries')->getStorage('create.data.visibility')]);
} else {