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

feat(entries): Entries API return Arrays Object instead of plain array on fetch. #485

This commit is contained in:
Awilum
2020-12-01 23:35:09 +03:00
parent b4b0fde384
commit 14446e2adf
12 changed files with 149 additions and 129 deletions

View File

@@ -12,13 +12,12 @@ namespace Flextype\Foundation\Entries;
use Atomastic\Arrays\Arrays;
use function array_merge;
use function arrays;
use function filesystem;
use function filter;
use function count;
use function find_filter;
use function ltrim;
use function md5;
use function rtrim;
use function str_replace;
use function find;
use function flextype;
use function strings;
class Entries
{
@@ -36,8 +35,8 @@ class Entries
/**
* Get storage
*
* @param string|int|null $key Key
* @param mixed $default Default value
* @param string|int|null $key Key.
* @param mixed $default Default value.
*/
public function getStorage($key, $default = null)
{
@@ -47,10 +46,10 @@ class Entries
/**
* Set storage
*
* @param string|null $key Key
* @param mixed $value Value
* @param string|null $key Key.
* @param mixed $value Value.
*/
public function setStorage(?string $key, $value)
public function setStorage(?string $key, $value): void
{
$this->storage = arrays($this->storage)->set($key, $value)->toArray();
}
@@ -59,156 +58,155 @@ class Entries
* Fetch entry(entries)
*
* @param string $id Unique identifier of the entry(entries).
* @param bool $collection Set `true` if collection of entries need to be fetched.
* @param array $filter Select items in collection by given conditions.
*
* @return Arrays
* @param bool $collection Set TRUE if collection of entries need to be returned.
* @param array $params Params array.
*
* @access public
*/
public function fetch(string $id, bool $collection = false, array $filter = []): Arrays
public function fetch(string $id, bool $collection = false, array $params = []): Arrays
{
if ($collection) {
return $this->fetchCollection($id, $filter);
return $this->fetchCollection($id, $params);
}
return $this->fetchSingle($id);
}
/**
* Fetch single entry
* Fetch single entry.
*
* @param string $id Unique identifier of the entry(entries).
*
* @return Arrays
* @param string $id Unique identifier of the entry.
*
* @access public
*/
public function fetchSingle(string $id): Arrays
{
// Store data
$this->storage['fetch_single']['id'] = $id;
$this->storage['fetch']['id'] = $id;
$this->storage['fetch']['data'] = [];
// Run event: onEntryInitialized
flextype('emitter')->emit('onEntryInitialized');
// Get Cache ID for current requested entry
$entry_cache_id = $this->getCacheID($this->storage['fetch_single']['id']);
$entryCacheID = $this->getCacheID($this->storage['fetch']['id']);
// Try to get current requested entry from cache
if (flextype('cache')->has($entry_cache_id)) {
// 1. Try to get current requested entry from cache
if (flextype('cache')->has($entryCacheID)) {
// Fetch entry from cache
$this->storage['fetch_single']['data'] = flextype('cache')->get($entry_cache_id);
$this->storage['fetch']['data'] = flextype('cache')->get($entryCacheID);
// Run event: onEntryAfterCacheInitialized
flextype('emitter')->emit('onEntryAfterCacheInitialized');
// Return entry from cache
return arrays($this->storage['fetch_single']['data']);
return arrays($this->storage['fetch']['data']);
}
// Try to get current requested entry from filesystem
if ($this->has($this->storage['fetch_single']['id'])) {
// 2. Try to get current requested entry from filesystem
if ($this->has($this->storage['fetch']['id'])) {
// Get entry file location
$entry_file = $this->getFileLocation($this->storage['fetch_single']['id']);
$entryFile = $this->getFileLocation($this->storage['fetch']['id']);
// Try to get requested entry from the filesystem
$entry_file_content = filesystem()->file($entry_file)->get();
if ($entry_file_content === false) {
return arrays([]);
$entryFileContent = filesystem()->file($entryFile)->get();
if ($entryFileContent === false) {
return arrays();
}
// Decode entry file content
$this->storage['fetch_single']['data'] = flextype('frontmatter')->decode($entry_file_content);
$this->storage['fetch']['data'] = flextype('frontmatter')->decode($entryFileContent);
// Run event: onEntryAfterInitialized
flextype('emitter')->emit('onEntryAfterInitialized');
// Set cache state
$cache = flextype('entries')->storage['fetch_single']['data']['cache']['enabled'] ??
$cache = flextype('entries')->storage['fetch']['data']['cache']['enabled'] ??
flextype('registry')->get('flextype.settings.cache.enabled');
// Save entry data to cache
if ($cache) {
flextype('cache')->set($entry_cache_id, $this->storage['fetch_single']['data']);
flextype('cache')->set($entryCacheID, $this->storage['fetch']['data']);
}
// Return entry data
return arrays($this->storage['fetch_single']['data']);
return arrays($this->storage['fetch']['data']);
}
// Return empty array if entry is not founded
return arrays([]);
return arrays();
}
/**
* Fetch entries collection
* Fetch entries collection.
*
* @param string $id Unique identifier of the entry(entries).
* @param array $filter Select items in collection by given conditions.
*
* @return Arrays
* @param array $params Params array.
*
* @access public
*/
public function fetchCollection(string $id, array $filter = []): Arrays
public function fetchCollection(string $id, array $params = []): Arrays
{
// Store data
$this->storage['fetch_collection']['id'] = $this->getDirectoryLocation($id);
$this->storage['fetch_collection']['data'] = [];
$this->storage['fetch']['id'] = $id;
$this->storage['fetch']['data'] = [];
// Run event: onEntriesInitialized
flextype('emitter')->emit('onEntriesInitialized');
// Apply find_filter
$entries_list = find($this->storage['fetch_collection']['id'], $filter);
// Find entries
$entries = find($this->getDirectoryLocation($this->storage['fetch']['id']), $params);
// If entries founded in the entries folder
// We are checking... Whether the requested entry is an a true entry.
// Get entry $_id. Remove entries path and remove left and right slashes.
// Fetch single entry.
if (count($entries_list) > 0) {
foreach ($entries_list as $current_entry) {
if ($current_entry->getType() !== 'file' || $current_entry->getFilename() !== 'entry' . '.' . flextype('registry')->get('flextype.settings.entries.extension')) {
if ($entries->hasResults()) {
foreach ($entries as $currenEntry) {
if ($currenEntry->getType() !== 'file' || $currenEntry->getFilename() !== 'entry' . '.' . flextype('registry')->get('flextype.settings.entries.extension')) {
continue;
}
$_id = ltrim(rtrim(str_replace(PATH['project'] . '/entries/', '', str_replace('\\', '/', $current_entry->getPath())), '/'), '/');
$this->storage['fetch_collection']['data'][$_id] = $this->fetchSingle($_id);
$currentEntryID = strings($currenEntry->getPath())
->replace('\\', '/')
->replace(PATH['project'] . '/entries/', '')
->trim('/')
->toString();
$data[$currentEntryID] = $this->fetchSingle($currentEntryID);
}
// Apply filter
$this->storage['fetch_collection']['data'] = filter($this->storage['fetch_collection']['data'], $filter);
$this->storage['fetch']['id'] = $id;
$this->storage['fetch']['data'] = filter($data, $params);
// Run event: onEntriesAfterInitialized
flextype('emitter')->emit('onEntriesAfterInitialized');
}
// Return entries array
return arrays($this->storage['fetch_collection']['data']);
return arrays($this->storage['fetch']['data']);
}
/**
* Move entry
*
* @param string $id Unique identifier of the entry(entries).
* @param string $new_id New Unique identifier of the entry(entries).
* @param string $id Unique identifier of the entry(entries).
* @param string $newID New Unique identifier of the entry(entries).
*
* @return bool True on success, false on failure.
*
* @access public
*/
public function move(string $id, string $new_id): bool
public function move(string $id, string $newID): bool
{
// Store data
$this->storage['move']['id'] = $id;
$this->storage['move']['new_id'] = $new_id;
$this->storage['move']['new_id'] = $newID;
// Run event: onEntryMove
flextype('emitter')->emit('onEntryMove');
if (! $this->has($this->storage['move']['new_id'])) {
return filesystem()->directory($this->getDirectoryLocation($this->storage['move']['id']))->move($this->getDirectoryLocation($this->storage['move']['new_id']));
return filesystem()
->directory($this->getDirectoryLocation($this->storage['move']['id']))
->move($this->getDirectoryLocation($this->storage['move']['new_id']));
}
return false;
@@ -233,13 +231,13 @@ class Entries
// Run event: onEntryUpdate
flextype('emitter')->emit('onEntryUpdate');
$entry_file = $this->getFileLocation($this->storage['update']['id']);
$entryFile = $this->getFileLocation($this->storage['update']['id']);
if (filesystem()->file($entry_file)->exists()) {
$body = filesystem()->file($entry_file)->get();
if (filesystem()->file($entryFile)->exists()) {
$body = filesystem()->file($entryFile)->get();
$entry = flextype('frontmatter')->decode($body);
return (bool) filesystem()->file($entry_file)->put(flextype('frontmatter')->encode(array_merge($entry, $this->storage['update']['data'])));
return (bool) filesystem()->file($entryFile)->put(flextype('frontmatter')->encode(array_merge($entry, $this->storage['update']['data'])));
}
return false;
@@ -265,16 +263,19 @@ class Entries
flextype('emitter')->emit('onEntryCreate');
// Create entry directory first if it is not exists
$entry_dir = $this->getDirectoryLocation($this->storage['create']['id']);
if (! filesystem()->directory($entry_dir)->exists() &&
! filesystem()->directory($entry_dir)->create()) {
$entryDir = $this->getDirectoryLocation($this->storage['create']['id']);
if (
! filesystem()->directory($entryDir)->exists() &&
! filesystem()->directory($entryDir)->create()
) {
return false;
}
// Create entry file
$entry_file = $entry_dir . '/entry' . '.' . flextype('registry')->get('flextype.settings.entries.extension');
if (! filesystem()->file($entry_file)->exists()) {
return (bool) filesystem()->file($entry_file)->put(flextype('frontmatter')->encode($this->storage['create']['data']));
$entryFile = $entryDir . '/entry' . '.' . flextype('registry')->get('flextype.settings.entries.extension');
if (! filesystem()->file($entryFile)->exists()) {
return (bool) filesystem()->file($entryFile)->put(flextype('frontmatter')->encode($this->storage['create']['data']));
}
return false;
@@ -297,29 +298,33 @@ class Entries
// Run event: onEntryDelete
flextype('emitter')->emit('onEntryDelete');
return filesystem()->directory($this->getDirectoryLocation($this->storage['delete']['id']))->delete();
return filesystem()
->directory($this->getDirectoryLocation($this->storage['delete']['id']))
->delete();
}
/**
* Copy entry(s)
*
* @param string $id Unique identifier of the entry(entries).
* @param string $new_id New Unique identifier of the entry(entries).
* @param string $id Unique identifier of the entry(entries).
* @param string $newID New Unique identifier of the entry(entries).
*
* @return bool|null True on success, false on failure.
*
* @access public
*/
public function copy(string $id, string $new_id): ?bool
public function copy(string $id, string $newID): ?bool
{
// Store data
$this->storage['copy']['id'] = $id;
$this->storage['copy']['new_id'] = $new_id;
$this->storage['copy']['new_id'] = $newID;
// Run event: onEntryCopy
flextype('emitter')->emit('onEntryCopy');
return filesystem()->directory($this->getDirectoryLocation($this->storage['copy']['id']))->copy($this->getDirectoryLocation($this->storage['copy']['new_id']));
return filesystem()
->directory($this->getDirectoryLocation($this->storage['copy']['id']))
->copy($this->getDirectoryLocation($this->storage['copy']['new_id']));
}
/**
@@ -385,12 +390,12 @@ class Entries
return '';
}
$entry_file = $this->getFileLocation($id);
$entryFile = $this->getFileLocation($id);
if (filesystem()->file($entry_file)->exists()) {
return md5('entry' . $entry_file . (filesystem()->file($entry_file)->lastModified() ?: ''));
if (filesystem()->file($entryFile)->exists()) {
return strings('entry' . $entryFile . (filesystem()->file($entryFile)->lastModified() ?: ''))->hash()->toString();
}
return md5('entry' . $entry_file);
return strings('entry' . $entryFile)->hash()->toString();
}
}

View File

@@ -7,20 +7,21 @@ declare(strict_types=1);
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
use Atomastic\Strings\Strings;
if (flextype('registry')->get('flextype.settings.entries.fields.created_at.enabled')) {
flextype('emitter')->addListener('onEntryAfterInitialized', static function (): void {
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());
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 {
flextype('entries')->setStorage('fetch_single.data.created_at', (int) strtotime((string) flextype('entries')->getStorage('fetch_single.data.created_at')));
flextype('entries')->setStorage('fetch.data.created_at', (int) strtotime((string) flextype('entries')->getStorage('fetch.data.created_at')));
}
});
flextype('emitter')->addListener('onEntryCreate', static function (): void {
if (flextype('entries')->getStorage('create.data.created_at') === null) {
flextype('entries')->setStorage('create.data.created_at', date(flextype('registry')->get('flextype.settings.date_format'), time()));
if (flextype('entries')->getStorage('create.data.created_at') !== null) {
return;
}
flextype('entries')->setStorage('create.data.created_at', date(flextype('registry')->get('flextype.settings.date_format'), time()));
});
}

View File

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

View File

@@ -11,8 +11,10 @@ use Atomastic\Strings\Strings;
if (flextype('registry')->get('flextype.settings.entries.fields.id.enabled')) {
flextype('emitter')->addListener('onEntryAfterInitialized', static function (): void {
if (flextype('entries')->getStorage('fetch_single.data.id') == null) {
flextype('entries')->setStorage('fetch_single.data.id', (string) Strings::create(flextype('entries')->getStorage('fetch_single.id'))->trimSlashes());
if (flextype('entries')->getStorage('fetch.data.id') !== null) {
return;
}
flextype('entries')->setStorage('fetch.data.id', (string) Strings::create(flextype('entries')->getStorage('fetch.id'))->trimSlashes());
});
}

View File

@@ -9,8 +9,10 @@ declare(strict_types=1);
if (flextype('registry')->get('flextype.settings.entries.fields.modified_at.enabled')) {
flextype('emitter')->addListener('onEntryAfterInitialized', static function (): void {
if (flextype('entries')->getStorage('fetch_single.data.modified_at') === null) {
flextype('entries')->setStorage('fetch_single.data.modified_at', (int) filesystem()->file(flextype('entries')->getFileLocation(flextype('entries')->getStorage('fetch_single.id')))->lastModified());
if (flextype('entries')->getStorage('fetch.data.modified_at') !== null) {
return;
}
flextype('entries')->setStorage('fetch.data.modified_at', (int) filesystem()->file(flextype('entries')->getFileLocation(flextype('entries')->getStorage('fetch.id')))->lastModified());
});
}

View File

@@ -15,30 +15,30 @@ if (flextype('registry')->get('flextype.settings.entries.fields.parsers.enabled'
function processParsersField(): void
{
if (flextype('entries')->getStorage('fetch_single.data.cache.enabled') == null) {
if (flextype('entries')->getStorage('fetch.data.cache.enabled') == null) {
$cache = false;
} else {
$cache = (bool) flextype('entries')->getStorage('fetch_single.data.cache.enabled');
$cache = (bool) flextype('entries')->getStorage('fetch.data.cache.enabled');
}
if (flextype('entries')->getStorage('fetch_single.data.parsers') != null) {
foreach (flextype('entries')->getStorage('fetch_single.data.parsers') as $parser_name => $parser_data) {
if (flextype('entries')->getStorage('fetch.data.parsers') != null) {
foreach (flextype('entries')->getStorage('fetch.data.parsers') as $parser_name => $parser_data) {
if (in_array($parser_name, ['markdown', 'shortcode'])) {
if (flextype('entries')->getStorage('fetch_single.data.parsers.'.$parser_name.'.enabled') === true) {
if (flextype('entries')->getStorage('fetch_single.data.parsers.'.$parser_name.'.fields') != null) {
if (is_array(flextype('entries')->getStorage('fetch_single.data.parsers.'.$parser_name.'.fields'))) {
foreach (flextype('entries')->getStorage('fetch_single.data.parsers.'.$parser_name.'.fields') as $field) {
if (flextype('entries')->getStorage('fetch.data.parsers.'.$parser_name.'.enabled') === true) {
if (flextype('entries')->getStorage('fetch.data.parsers.'.$parser_name.'.fields') != null) {
if (is_array(flextype('entries')->getStorage('fetch.data.parsers.'.$parser_name.'.fields'))) {
foreach (flextype('entries')->getStorage('fetch.data.parsers.'.$parser_name.'.fields') as $field) {
if (! in_array($field, flextype('registry')->get('flextype.settings.entries.fields'))) {
if ($parser_name == 'markdown') {
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 (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 ($parser_name == 'shortcode') {
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));
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));
}
}
}

View File

@@ -9,16 +9,18 @@ declare(strict_types=1);
if (flextype('registry')->get('flextype.settings.entries.fields.published_at.enabled')) {
flextype('emitter')->addListener('onEntryAfterInitialized', static function (): void {
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());
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 {
flextype('entries')->setStorage('fetch_single.data.published_at', (int) strtotime((string) flextype('entries')->getStorage('fetch_single.data.published_at')));
flextype('entries')->setStorage('fetch.data.published_at', (int) strtotime((string) flextype('entries')->getStorage('fetch.data.published_at')));
}
});
flextype('emitter')->addListener('onEntryCreate', static function (): void {
if (flextype('entries')->getStorage('create.data.published_at') === null) {
flextype('entries')->setStorage('create.data.published_at', date(flextype('registry')->get('flextype.settings.date_format'), time()));
if (flextype('entries')->getStorage('create.data.published_at') !== null) {
return;
}
flextype('entries')->setStorage('create.data.published_at', date(flextype('registry')->get('flextype.settings.date_format'), time()));
});
}

View File

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

View File

@@ -10,10 +10,10 @@ declare(strict_types=1);
if (flextype('registry')->get('flextype.settings.entries.fields.routable.enabled')) {
flextype('emitter')->addListener('onEntryAfterInitialized', static function (): void {
if (flextype('entries')->getStorage('fetch_single.data.routable') === null) {
flextype('entries')->setStorage('fetch_single.data.routable', true);
if (flextype('entries')->getStorage('fetch.data.routable') === null) {
flextype('entries')->setStorage('fetch.data.routable', true);
} else {
flextype('entries')->setStorage('fetch_single.data.routable', (bool) flextype('entries')->getStorage('fetch_single.data.routable'));
flextype('entries')->setStorage('fetch.data.routable', (bool) flextype('entries')->getStorage('fetch.data.routable'));
}
});

View File

@@ -11,9 +11,11 @@ use Atomastic\Strings\Strings;
if (flextype('registry')->get('flextype.settings.entries.fields.slug.enabled')) {
flextype('emitter')->addListener('onEntryAfterInitialized', static function (): void {
if (flextype('entries')->getStorage('fetch_single.data.slug') == null) {
$parts = Strings::create(flextype('entries')->getStorage('fetch_single.id'))->trimSlashes()->segments();
flextype('entries')->setStorage('fetch_single.data.slug', (string) end($parts));
if (flextype('entries')->getStorage('fetch.data.slug') !== null) {
return;
}
$parts = Strings::create(flextype('entries')->getStorage('fetch.id'))->trimSlashes()->segments();
flextype('entries')->setStorage('fetch.data.slug', (string) end($parts));
});
}

View File

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

View File

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