From 0f719e2965cb37d44e2fcd65b149cbf027f1e4ef Mon Sep 17 00:00:00 2001 From: Awilum Date: Tue, 17 Aug 2021 16:28:49 +0300 Subject: [PATCH] feat(entries): move Entries API to `/entries` and reorganize all fields #563 --- src/flextype/core/{ => Entries}/Entries.php | 13 ++++-- .../Entries/Fields/Default/CreatedAtField.php | 34 ++++++++++++++ .../Entries/Fields/Default/CreatedByField.php | 22 +++++++++ .../Fields/Default/EntriesField.php} | 21 ++++----- .../Fields/Default}/IdField.php | 0 .../Fields/Default/ModifiedAtField.php | 21 +++++++++ .../Fields/Default}/ParsersField.php | 34 +++++++------- .../Fields/Default/PublishedAtField.php | 34 ++++++++++++++ .../Fields/Default/PublishedByField.php | 21 +++++++++ .../Fields/Default}/RegistryField.php | 12 ++--- .../Entries/Fields/Default/RoutableField.php | 35 ++++++++++++++ .../core/Entries/Fields/Default/SlugField.php | 22 +++++++++ .../core/Entries/Fields/Default/UuidField.php | 23 ++++++++++ .../Fields/Default/VisibilityField.php | 46 +++++++++++++++++++ .../Fields/Tokens/Items/CallsField.php | 21 +++++++++ .../Fields/Tokens/Items/StateField.php | 21 +++++++++ .../Fields/Tokens/Items/TotalCallsField.php | 21 +++++++++ 17 files changed, 363 insertions(+), 38 deletions(-) rename src/flextype/core/{ => Entries}/Entries.php (98%) create mode 100644 src/flextype/core/Entries/Fields/Default/CreatedAtField.php create mode 100644 src/flextype/core/Entries/Fields/Default/CreatedByField.php rename src/flextype/core/{Content/Fields/ContentField.php => Entries/Fields/Default/EntriesField.php} (67%) rename src/flextype/core/{Content/Fields => Entries/Fields/Default}/IdField.php (100%) create mode 100644 src/flextype/core/Entries/Fields/Default/ModifiedAtField.php rename src/flextype/core/{Content/Fields => Entries/Fields/Default}/ParsersField.php (56%) create mode 100644 src/flextype/core/Entries/Fields/Default/PublishedAtField.php create mode 100644 src/flextype/core/Entries/Fields/Default/PublishedByField.php rename src/flextype/core/{Content/Fields => Entries/Fields/Default}/RegistryField.php (63%) create mode 100644 src/flextype/core/Entries/Fields/Default/RoutableField.php create mode 100644 src/flextype/core/Entries/Fields/Default/SlugField.php create mode 100644 src/flextype/core/Entries/Fields/Default/UuidField.php create mode 100644 src/flextype/core/Entries/Fields/Default/VisibilityField.php create mode 100644 src/flextype/core/Entries/Fields/Tokens/Items/CallsField.php create mode 100644 src/flextype/core/Entries/Fields/Tokens/Items/StateField.php create mode 100644 src/flextype/core/Entries/Fields/Tokens/Items/TotalCallsField.php diff --git a/src/flextype/core/Entries.php b/src/flextype/core/Entries/Entries.php similarity index 98% rename from src/flextype/core/Entries.php rename to src/flextype/core/Entries/Entries.php index c0cfdd0e..c20e590d 100755 --- a/src/flextype/core/Entries.php +++ b/src/flextype/core/Entries/Entries.php @@ -7,9 +7,10 @@ declare(strict_types=1); * Founded by Sergey Romanenko and maintained by Flextype Community. */ -namespace Flextype; +namespace Flextype\Entries; use Atomastic\Arrays\Arrays; +use Atomastic\Macroable\Macroable; use function array_merge; use function arrays; @@ -27,6 +28,8 @@ use function strings; class Entries { + use Macroable; + /** * Entries Registry. * @@ -74,8 +77,10 @@ class Entries $this->registry()->set('collectionOptions', $this->options['collections']['default']); foreach ($this->options['collections'] as $collection) { - if (boolval(preg_match_all('#^' . $collection['pattern'] . '$#', $id, $matches, PREG_OFFSET_CAPTURE))) { - $this->registry()->set('collectionOptions', $collection); + if (isset($collection['pattern'])) { + if (boolval(preg_match_all('#^' . $collection['pattern'] . '$#', $id, $matches, PREG_OFFSET_CAPTURE))) { + $this->registry()->set('collectionOptions', $collection); + } } } @@ -310,7 +315,7 @@ class Entries * @access public */ public function move(string $id, string $newID): bool - { + { // Entry data $this->registry()->set('move.id', $id); $this->registry()->set('move.newID', $newID); diff --git a/src/flextype/core/Entries/Fields/Default/CreatedAtField.php b/src/flextype/core/Entries/Fields/Default/CreatedAtField.php new file mode 100644 index 00000000..493d899b --- /dev/null +++ b/src/flextype/core/Entries/Fields/Default/CreatedAtField.php @@ -0,0 +1,34 @@ +addListener('onDefaultFetchSingleHasResult', static function (): void { + + if (! registry()->get('flextype.settings.entries.collections.default.fields.created_at.enabled')) { + return; + } + + if (entries()->registry()->get('fetch.data.created_at') === null) { + entries()->registry()->set('fetch.data.created_at', (int) filesystem()->file(entries()->getFileLocation(entries()->registry()->get('fetch.id')))->lastModified()); + } else { + entries()->registry()->set('fetch.data.created_at', (int) strtotime((string) entries()->registry()->get('fetch.data.created_at'))); + } +}); + +emitter()->addListener('onDefaultCreate', static function (): void { + + if (! registry()->get('flextype.settings.entries.collections.default.fields.created_at.enabled')) { + return; + } + + if (entries()->registry()->get('create.data.created_at') !== null) { + return; + } + + entries()->registry()->set('create.data.created_at', date(registry()->get('flextype.settings.date_format'), time())); +}); diff --git a/src/flextype/core/Entries/Fields/Default/CreatedByField.php b/src/flextype/core/Entries/Fields/Default/CreatedByField.php new file mode 100644 index 00000000..3efa2d1e --- /dev/null +++ b/src/flextype/core/Entries/Fields/Default/CreatedByField.php @@ -0,0 +1,22 @@ +addListener('onDefaultCreate', static function (): void { + + if (! registry()->get('flextype.settings.entries.collections.default.fields.created_by.enabled')) { + return; + } + + if (entries()->registry()->get('create.data.created_by') !== null) { + return; + } + + entries()->registry()->set('create.data.created_by', ''); +}); + diff --git a/src/flextype/core/Content/Fields/ContentField.php b/src/flextype/core/Entries/Fields/Default/EntriesField.php similarity index 67% rename from src/flextype/core/Content/Fields/ContentField.php rename to src/flextype/core/Entries/Fields/Default/EntriesField.php index e8f0331b..f2009d16 100644 --- a/src/flextype/core/Content/Fields/ContentField.php +++ b/src/flextype/core/Entries/Fields/Default/EntriesField.php @@ -9,19 +9,18 @@ declare(strict_types=1); use Atomastic\Arrays\Arrays; +emitter()->addListener('onDefaultFetchSingleHasResult', static function (): void { -emitter()->addListener('onContentFetchSingleHasResult', static function (): void { - - if (! registry()->get('flextype.settings.entries.content.fields.content.enabled')) { + if (! registry()->get('flextype.settings.entries.collections.default.fields.entries.enabled')) { return; } - if (content()->registry()->has('fetch.data.content.fetch')) { + if (entries()->registry()->has('fetch.data.entries.fetch')) { // Get fetch. - $original = content()->registry()->get('fetch'); + $original = entries()->registry()->get('fetch'); $data = []; - switch (registry()->get('flextype.settings.entries.content.fields.content.fetch.result')) { + switch (registry()->get('flextype.settings.entries.collections.default.fields.entries.fetch.result')) { case 'toArray': $resultTo = 'toArray'; break; @@ -33,7 +32,7 @@ emitter()->addListener('onContentFetchSingleHasResult', static function (): void } // Modify fetch. - foreach (content()->registry()->get('fetch.data.content.fetch') as $field => $body) { + foreach (entries()->registry()->get('fetch.data.entries.fetch') as $field => $body) { if (isset($body['options']['method']) && strpos($body['options']['method'], 'fetch') !== false && @@ -45,7 +44,7 @@ emitter()->addListener('onContentFetchSingleHasResult', static function (): void $result = isset($body['result']) && in_array($body['result'], ['toArray', 'toObject']) ? $body['result'] : $resultTo; - $data[$field] = content()->{$fetchFromCallbackMethod}($body['id'], + $data[$field] = entries()->{$fetchFromCallbackMethod}($body['id'], isset($body['options']) ? $body['options'] : []); @@ -54,8 +53,8 @@ emitter()->addListener('onContentFetchSingleHasResult', static function (): void } // Save fetch. - content()->registry()->set('fetch.id', $original['id']); - content()->registry()->set('fetch.options', $original['options']); - content()->registry()->set('fetch.data', arrays($original['data'])->merge($data)->toArray()); + entries()->registry()->set('fetch.id', $original['id']); + entries()->registry()->set('fetch.options', $original['options']); + entries()->registry()->set('fetch.data', arrays($original['data'])->merge($data)->toArray()); } }); \ No newline at end of file diff --git a/src/flextype/core/Content/Fields/IdField.php b/src/flextype/core/Entries/Fields/Default/IdField.php similarity index 100% rename from src/flextype/core/Content/Fields/IdField.php rename to src/flextype/core/Entries/Fields/Default/IdField.php diff --git a/src/flextype/core/Entries/Fields/Default/ModifiedAtField.php b/src/flextype/core/Entries/Fields/Default/ModifiedAtField.php new file mode 100644 index 00000000..8f925a6c --- /dev/null +++ b/src/flextype/core/Entries/Fields/Default/ModifiedAtField.php @@ -0,0 +1,21 @@ +addListener('onDefaultFetchSingleHasResult', static function (): void { + + if (! registry()->get('flextype.settings.entries.collections.default.fields.modified_at.enabled')) { + return; + } + + if (entries()->registry()->get('fetch.data.modified_at') !== null) { + return; + } + + entries()->registry()->set('fetch.data.modified_at', (int) filesystem()->file(entries()->getFileLocation(entries()->registry()->get('fetch.id')))->lastModified()); +}); diff --git a/src/flextype/core/Content/Fields/ParsersField.php b/src/flextype/core/Entries/Fields/Default/ParsersField.php similarity index 56% rename from src/flextype/core/Content/Fields/ParsersField.php rename to src/flextype/core/Entries/Fields/Default/ParsersField.php index 0e3837ea..dca5bb02 100644 --- a/src/flextype/core/Content/Fields/ParsersField.php +++ b/src/flextype/core/Entries/Fields/Default/ParsersField.php @@ -7,39 +7,39 @@ declare(strict_types=1); * Founded by Sergey Romanenko and maintained by Flextype Community. */ -emitter()->addListener('onContentFetchSingleHasResult', static function (): void { +emitter()->addListener('onDefaultFetchSingleHasResult', static function (): void { - if (! registry()->get('flextype.settings.entries.content.fields.parsers.enabled')) { + if (! registry()->get('flextype.settings.entries.collections.default.fields.parsers.enabled')) { return; } - if (content()->registry()->get('fetch.data.cache.enabled') == null) { + if (entries()->registry()->get('fetch.data.cache.enabled') == null) { $cache = false; } else { - $cache = (bool) content()->registry()->get('fetch.data.cache.enabled'); + $cache = (bool) entries()->registry()->get('fetch.data.cache.enabled'); } - if (content()->registry()->get('fetch.data.parsers') != null) { + if (entries()->registry()->get('fetch.data.parsers') != null) { - foreach (content()->registry()->get('fetch.data.parsers') as $parserName => $parserData) { + foreach (entries()->registry()->get('fetch.data.parsers') as $parserName => $parserData) { if (in_array($parserName, ['shortcodes', 'markdown'])) { - if (content()->registry()->get('fetch.data.parsers.'.$parserName.'.enabled') === true) { - if (content()->registry()->get('fetch.data.parsers.'.$parserName.'.fields') != null) { - if (is_array(content()->registry()->get('fetch.data.parsers.'.$parserName.'.fields'))) { - foreach (content()->registry()->get('fetch.data.parsers.'.$parserName.'.fields') as $field) { - if (! in_array($field, registry()->get('flextype.settings.entries.content.fields'))) { + if (entries()->registry()->get('fetch.data.parsers.'.$parserName.'.enabled') === true) { + if (entries()->registry()->get('fetch.data.parsers.'.$parserName.'.fields') != null) { + if (is_array(entries()->registry()->get('fetch.data.parsers.'.$parserName.'.fields'))) { + foreach (entries()->registry()->get('fetch.data.parsers.'.$parserName.'.fields') as $field) { + if (! in_array($field, registry()->get('flextype.settings.entries.collections.default.fields'))) { if ($parserName == 'markdown') { - if (arrays(content()->registry()->get('fetch.data'))->has($field)) { - content()->registry()->set('fetch.data.'.$field, - parsers()->markdown()->parse(content()->registry()->get('fetch.data.'.$field), $cache)); + if (arrays(entries()->registry()->get('fetch.data'))->has($field)) { + entries()->registry()->set('fetch.data.'.$field, + parsers()->markdown()->parse(entries()->registry()->get('fetch.data.'.$field), $cache)); } } if ($parserName == 'shortcodes') { - if (arrays(content()->registry()->get('fetch.data'))->has($field)) { - content()->registry()->set('fetch.data.'.$field, - parsers()->shortcodes()->parse(content()->registry()->get('fetch.data.'.$field), $cache)); + if (arrays(entries()->registry()->get('fetch.data'))->has($field)) { + entries()->registry()->set('fetch.data.'.$field, + parsers()->shortcodes()->parse(entries()->registry()->get('fetch.data.'.$field), $cache)); } } } diff --git a/src/flextype/core/Entries/Fields/Default/PublishedAtField.php b/src/flextype/core/Entries/Fields/Default/PublishedAtField.php new file mode 100644 index 00000000..d099884a --- /dev/null +++ b/src/flextype/core/Entries/Fields/Default/PublishedAtField.php @@ -0,0 +1,34 @@ +addListener('onDefaultFetchSingleHasResult', static function (): void { + + if (! registry()->get('flextype.settings.entries.collections.default.fields.published_at.enabled')) { + return; + } + + if (entries()->registry()->get('fetch.data.published_at') === null) { + entries()->registry()->set('fetch.data.published_at', (int) filesystem()->file(entries()->getFileLocation(entries()->registry()->get('fetch.id')))->lastModified()); + } else { + entries()->registry()->set('fetch.data.published_at', (int) strtotime((string) entries()->registry()->get('fetch.data.published_at'))); + } +}); + +emitter()->addListener('onDefaultCreate', static function (): void { + + if (! registry()->get('flextype.settings.entries.collections.default.fields.published_at.enabled')) { + return; + } + + if (entries()->registry()->get('create.data.published_at') !== null) { + return; + } + + entries()->registry()->set('create.data.published_at', date(registry()->get('flextype.settings.date_format'), time())); +}); \ No newline at end of file diff --git a/src/flextype/core/Entries/Fields/Default/PublishedByField.php b/src/flextype/core/Entries/Fields/Default/PublishedByField.php new file mode 100644 index 00000000..f5301b82 --- /dev/null +++ b/src/flextype/core/Entries/Fields/Default/PublishedByField.php @@ -0,0 +1,21 @@ +addListener('onDefaultCreate', static function (): void { + + if (! registry()->get('flextype.settings.entries.collections.default.fields.published_by.enabled')) { + return; + } + + if (entries()->registry()->get('create.data.published_by') !== null) { + return; + } + + entries()->registry()->set('create.data.published_by', ''); +}); \ No newline at end of file diff --git a/src/flextype/core/Content/Fields/RegistryField.php b/src/flextype/core/Entries/Fields/Default/RegistryField.php similarity index 63% rename from src/flextype/core/Content/Fields/RegistryField.php rename to src/flextype/core/Entries/Fields/Default/RegistryField.php index 6181cac8..efbeaa62 100644 --- a/src/flextype/core/Content/Fields/RegistryField.php +++ b/src/flextype/core/Entries/Fields/Default/RegistryField.php @@ -7,20 +7,20 @@ declare(strict_types=1); * Founded by Sergey Romanenko and maintained by Flextype Community. */ -emitter()->addListener('onContentFetchSingleHasResult', static function (): void { +emitter()->addListener('onDefaultFetchSingleHasResult', static function (): void { - if (! registry()->get('flextype.settings.entries.content.fields.registry.enabled')) { + if (! registry()->get('flextype.settings.entries.collections.default.fields.registry.enabled')) { return; } - if (content()->registry()->has('fetch.data.registry.get')) { + if (entries()->registry()->has('fetch.data.registry.get')) { // Get fetch. - $original = content()->registry()->get('fetch'); + $original = entries()->registry()->get('fetch'); $data = []; // Modify fetch. - foreach (content()->registry()->get('fetch.data.registry.get') as $field => $body) { + foreach (entries()->registry()->get('fetch.data.registry.get') as $field => $body) { $data = arrays($data)->merge(arrays($data)->set($field, registry()->get($body['key'], isset($body['default']) ? $body['default'] : @@ -29,6 +29,6 @@ emitter()->addListener('onContentFetchSingleHasResult', static function (): void } // Save fetch. - content()->registry()->set('fetch.data', arrays($original['data'])->merge($data)->toArray()); + entries()->registry()->set('fetch.data', arrays($original['data'])->merge($data)->toArray()); } }); \ No newline at end of file diff --git a/src/flextype/core/Entries/Fields/Default/RoutableField.php b/src/flextype/core/Entries/Fields/Default/RoutableField.php new file mode 100644 index 00000000..ade73e8c --- /dev/null +++ b/src/flextype/core/Entries/Fields/Default/RoutableField.php @@ -0,0 +1,35 @@ +addListener('onDefaultFetchSingleHasResult', static function (): void { + + if (registry()->get('flextype.settings.entries.collections.default.fields.routable.enabled')) { + return; + } + + if (entries()->registry()->get('fetch.data.routable') === null) { + entries()->registry()->set('fetch.data.routable', true); + } else { + entries()->registry()->set('fetch.data.routable', (bool) entries()->registry()->get('fetch.data.routable')); + } + +}); + +emitter()->addListener('onDefaultCreate', static function (): void { + + if (! registry()->get('flextype.settings.entries.collections.default.fields.routable.enabled')) { + return; + } + + if (entries()->registry()->get('create.data.routable') === null) { + entries()->registry()->set('create.data.routable', true); + } else { + entries()->registry()->set('create.data.routable', (bool) entries()->registry()->get('create.data.routable')); + } +}); diff --git a/src/flextype/core/Entries/Fields/Default/SlugField.php b/src/flextype/core/Entries/Fields/Default/SlugField.php new file mode 100644 index 00000000..c83307db --- /dev/null +++ b/src/flextype/core/Entries/Fields/Default/SlugField.php @@ -0,0 +1,22 @@ +addListener('onDefaultFetchSingleHasResult', static function (): void { + + if (! registry()->get('flextype.settings.entries.collections.default.fields.slug.enabled')) { + return; + } + + if (entries()->registry()->get('fetch.data.slug') !== null) { + return; + } + + $parts = explode('/', ltrim(rtrim(entries()->registry()->get('fetch.id'), '/'), '/')); + entries()->registry()->set('fetch.data.slug', (string) end($parts)); +}); diff --git a/src/flextype/core/Entries/Fields/Default/UuidField.php b/src/flextype/core/Entries/Fields/Default/UuidField.php new file mode 100644 index 00000000..ac9bb623 --- /dev/null +++ b/src/flextype/core/Entries/Fields/Default/UuidField.php @@ -0,0 +1,23 @@ +addListener('onDefaultCreate', static function (): void { + + if (! registry()->get('flextype.settings.entries.collections.default.fields.uuid.enabled')) { + return; + } + + if (entries()->registry()->get('create.data.uuid') !== null) { + return; + } + + entries()->registry()->set('create.data.uuid', Uuid::uuid4()->toString()); +}); diff --git a/src/flextype/core/Entries/Fields/Default/VisibilityField.php b/src/flextype/core/Entries/Fields/Default/VisibilityField.php new file mode 100644 index 00000000..5bb740d6 --- /dev/null +++ b/src/flextype/core/Entries/Fields/Default/VisibilityField.php @@ -0,0 +1,46 @@ +addListener('onDefaultFetchSingleHasResult', static function (): void { + + $visibility = [ + 'draft' => 'draft', + 'hidden' => 'hidden', + 'visible' => 'visible', + ]; + + if (! registry()->get('flextype.settings.entries.collections.default.fields.visibility.enabled')) { + return; + } + + if (entries()->registry()->get('fetch.data.visibility') !== null && in_array(entries()->registry()->get('fetch.data.visibility'), $visibility)) { + entries()->registry()->set('fetch.data.visibility', (string) $visibility[entries()->registry()->get('fetch.data.visibility')]); + } else { + entries()->registry()->set('fetch.data.visibility', (string) $visibility['visible']); + } +}); + +emitter()->addListener('onDefaultCreate', static function (): void { + + $visibility = [ + 'draft' => 'draft', + 'hidden' => 'hidden', + 'visible' => 'visible', + ]; + + if (! registry()->get('flextype.settings.entries.collections.default.fields.visibility.enabled')) { + return; + } + + if (entries()->registry()->get('create.data.visibility') !== null && in_array(entries()->registry()->get('create.data.visibility'), $visibility)) { + entries()->registry()->set('create.data.visibility', (string) $visibility[entries()->registry()->get('create.data.visibility')]); + } else { + entries()->registry()->set('create.data.visibility', (string) $visibility['visible']); + } +}); \ No newline at end of file diff --git a/src/flextype/core/Entries/Fields/Tokens/Items/CallsField.php b/src/flextype/core/Entries/Fields/Tokens/Items/CallsField.php new file mode 100644 index 00000000..2864af19 --- /dev/null +++ b/src/flextype/core/Entries/Fields/Tokens/Items/CallsField.php @@ -0,0 +1,21 @@ +addListener('onTokensItemsCreate', static function (): void { + + if (! registry()->get('flextype.settings.entries.tokens_items.fields.calls.enabled')) { + return; + } + + if (entries()->registry()->get('create.data.calls') !== null) { + return; + } + + entries()->registry()->set('create.data.calls', 0); +}); diff --git a/src/flextype/core/Entries/Fields/Tokens/Items/StateField.php b/src/flextype/core/Entries/Fields/Tokens/Items/StateField.php new file mode 100644 index 00000000..86fa9b48 --- /dev/null +++ b/src/flextype/core/Entries/Fields/Tokens/Items/StateField.php @@ -0,0 +1,21 @@ +addListener('onTokensItemsCreate', static function (): void { + + if (! registry()->get('flextype.settings.entries.tokens_items.fields.state.enabled')) { + return; + } + + if (entries()->registry()->get('create.data.state') !== null) { + return; + } + + entries()->registry()->set('create.data.state', 'enabled'); +}); \ No newline at end of file diff --git a/src/flextype/core/Entries/Fields/Tokens/Items/TotalCallsField.php b/src/flextype/core/Entries/Fields/Tokens/Items/TotalCallsField.php new file mode 100644 index 00000000..53da3d9e --- /dev/null +++ b/src/flextype/core/Entries/Fields/Tokens/Items/TotalCallsField.php @@ -0,0 +1,21 @@ +addListener('onTokensItemsCreate', static function (): void { + + if (! registry()->get('flextype.settings.entries.tokens_items.fields.total_calls.enabled')) { + return; + } + + if (entries()->registry()->get('create.data.total_calls') !== null) { + return; + } + + entries()->registry()->set('create.data.total_calls', 0); +});