From b522291366d6d8aed637ead5af5147862df8107d Mon Sep 17 00:00:00 2001 From: Awilum Date: Fri, 16 Jul 2021 21:20:10 +0300 Subject: [PATCH] feat(entries): New Entries API and Content API #552 - Entries API basic class for any other Data managing APIs - Content API based on Entries API class - Content Fields for Content API BREAKING CHANGES - use Content API for site content managing instead of directly using Entries API Example: `flextype('content')->fetch()` instead of `flextype('entries')->fetch()` - all events should starts with `onContent` instead of `onEntries` - settings section `flextype.settings.entries` updated --- src/flextype/Foundation/Content/Content.php | 19 + .../Fields/ContentField.php} | 22 +- .../Content/Fields/CreatedAtField.php | 27 ++ .../Content/Fields/CreatedByField.php | 18 + .../Foundation/Content/Fields/IdField.php | 19 + .../Fields/MediaField.php | 36 +- .../Content/Fields/ModifiedAtField.php | 18 + .../Content/Fields/ParsersField.php | 52 +++ .../Content/Fields/PublishedAtField.php | 26 ++ .../Content/Fields/PublishedByField.php | 18 + .../Fields/RegistryField.php | 12 +- .../Content/Fields/RoutableField.php | 27 ++ .../Foundation/Content/Fields/SlugField.php | 20 + .../Foundation/Content/Fields/UuidField.php | 20 + .../Content/Fields/VisibilityField.php | 32 ++ src/flextype/Foundation/Entries/Entries.php | 424 ------------------ .../Entries/Fields/CreatedAtField.php | 27 -- .../Entries/Fields/CreatedByField.php | 18 - .../Foundation/Entries/Fields/IdField.php | 19 - .../Entries/Fields/ModifiedAtField.php | 18 - .../Entries/Fields/ParsersField.php | 52 --- .../Entries/Fields/PublishedAtField.php | 26 -- .../Entries/Fields/PublishedByField.php | 18 - .../Entries/Fields/RoutableField.php | 27 -- .../Foundation/Entries/Fields/SlugField.php | 20 - .../Foundation/Entries/Fields/UuidField.php | 20 - .../Entries/Fields/VisibilityField.php | 32 -- src/flextype/flextype.php | 22 +- 28 files changed, 334 insertions(+), 755 deletions(-) create mode 100644 src/flextype/Foundation/Content/Content.php rename src/flextype/Foundation/{Entries/Fields/EntriesField.php => Content/Fields/ContentField.php} (63%) create mode 100644 src/flextype/Foundation/Content/Fields/CreatedAtField.php create mode 100644 src/flextype/Foundation/Content/Fields/CreatedByField.php create mode 100644 src/flextype/Foundation/Content/Fields/IdField.php rename src/flextype/Foundation/{Entries => Content}/Fields/MediaField.php (67%) create mode 100644 src/flextype/Foundation/Content/Fields/ModifiedAtField.php create mode 100644 src/flextype/Foundation/Content/Fields/ParsersField.php create mode 100644 src/flextype/Foundation/Content/Fields/PublishedAtField.php create mode 100644 src/flextype/Foundation/Content/Fields/PublishedByField.php rename src/flextype/Foundation/{Entries => Content}/Fields/RegistryField.php (56%) create mode 100644 src/flextype/Foundation/Content/Fields/RoutableField.php create mode 100644 src/flextype/Foundation/Content/Fields/SlugField.php create mode 100644 src/flextype/Foundation/Content/Fields/UuidField.php create mode 100644 src/flextype/Foundation/Content/Fields/VisibilityField.php delete mode 100755 src/flextype/Foundation/Entries/Entries.php delete mode 100644 src/flextype/Foundation/Entries/Fields/CreatedAtField.php delete mode 100644 src/flextype/Foundation/Entries/Fields/CreatedByField.php delete mode 100644 src/flextype/Foundation/Entries/Fields/IdField.php delete mode 100644 src/flextype/Foundation/Entries/Fields/ModifiedAtField.php delete mode 100644 src/flextype/Foundation/Entries/Fields/ParsersField.php delete mode 100644 src/flextype/Foundation/Entries/Fields/PublishedAtField.php delete mode 100644 src/flextype/Foundation/Entries/Fields/PublishedByField.php delete mode 100644 src/flextype/Foundation/Entries/Fields/RoutableField.php delete mode 100644 src/flextype/Foundation/Entries/Fields/SlugField.php delete mode 100644 src/flextype/Foundation/Entries/Fields/UuidField.php delete mode 100644 src/flextype/Foundation/Entries/Fields/VisibilityField.php diff --git a/src/flextype/Foundation/Content/Content.php b/src/flextype/Foundation/Content/Content.php new file mode 100644 index 00000000..3d0988a8 --- /dev/null +++ b/src/flextype/Foundation/Content/Content.php @@ -0,0 +1,19 @@ +get('flextype.settings.entries.fields.entries.fetch.enabled')) { - flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void { - if (flextype('entries')->storage()->has('fetch.data.entries.fetch')) { +if (flextype('registry')->get('flextype.settings.entries.content.fields.entries.fetch.enabled')) { + flextype('emitter')->addListener('onContentFetchSingleHasResult', static function (): void { + if (flextype('content')->registry()->has('fetch.data.entries.fetch')) { // Get fetch. - $original = flextype('entries')->storage()->get('fetch'); + $original = flextype('content')->registry()->get('fetch'); $data = []; - switch (flextype('registry')->get('flextype.settings.entries.fields.entries.fetch.result')) { + switch (flextype('registry')->get('flextype.settings.entries.content.fields.content.fetch.result')) { case 'toArray': $resultTo = 'toArray'; break; @@ -28,11 +28,11 @@ if (flextype('registry')->get('flextype.settings.entries.fields.entries.fetch.en } // Modify fetch. - foreach (flextype('entries')->storage()->get('fetch.data.entries.fetch') as $field => $body) { + foreach (flextype('content')->registry()->get('fetch.data.content.fetch') as $field => $body) { if (isset($body['options']['method']) && strpos($body['options']['method'], 'fetch') !== false && - is_callable([flextype('entries'), $body['options']['method']])) { + is_callable([flextype('content'), $body['options']['method']])) { $fetchFromCallbackMethod = $body['options']['method']; } else { $fetchFromCallbackMethod = 'fetch'; @@ -40,7 +40,7 @@ if (flextype('registry')->get('flextype.settings.entries.fields.entries.fetch.en $result = isset($body['result']) && in_array($body['result'], ['toArray', 'toObject']) ? $body['result'] : $resultTo; - $data[$field] = flextype('entries')->{$fetchFromCallbackMethod}($body['id'], + $data[$field] = flextype('content')->{$fetchFromCallbackMethod}($body['id'], isset($body['options']) ? $body['options'] : []); @@ -49,9 +49,9 @@ if (flextype('registry')->get('flextype.settings.entries.fields.entries.fetch.en } // Save fetch. - flextype('entries')->storage()->set('fetch.id', $original['id']); - flextype('entries')->storage()->set('fetch.options', $original['options']); - flextype('entries')->storage()->set('fetch.data', arrays($original['data'])->merge($data)->toArray()); + flextype('content')->registry()->set('fetch.id', $original['id']); + flextype('content')->registry()->set('fetch.options', $original['options']); + flextype('content')->registry()->set('fetch.data', arrays($original['data'])->merge($data)->toArray()); } }); } diff --git a/src/flextype/Foundation/Content/Fields/CreatedAtField.php b/src/flextype/Foundation/Content/Fields/CreatedAtField.php new file mode 100644 index 00000000..37b89104 --- /dev/null +++ b/src/flextype/Foundation/Content/Fields/CreatedAtField.php @@ -0,0 +1,27 @@ +get('flextype.settings.entries.content.fields.created_at.enabled')) { + flextype('emitter')->addListener('onContentFetchSingleHasResult', static function (): void { + if (flextype('content')->registry()->get('fetch.data.created_at') === null) { + flextype('content')->registry()->set('fetch.data.created_at', (int) filesystem()->file(flextype('content')->getFileLocation(flextype('content')->registry()->get('fetch.id')))->lastModified()); + } else { + flextype('content')->registry()->set('fetch.data.created_at', (int) strtotime((string) flextype('content')->registry()->get('fetch.data.created_at'))); + } + }); + + flextype('emitter')->addListener('onContentCreate', static function (): void { + if (flextype('content')->registry()->get('create.data.created_at') !== null) { + return; + } + + flextype('content')->registry()->set('create.data.created_at', date(flextype('registry')->get('flextype.settings.date_format'), time())); + }); +} diff --git a/src/flextype/Foundation/Content/Fields/CreatedByField.php b/src/flextype/Foundation/Content/Fields/CreatedByField.php new file mode 100644 index 00000000..b9a72c84 --- /dev/null +++ b/src/flextype/Foundation/Content/Fields/CreatedByField.php @@ -0,0 +1,18 @@ +get('flextype.settings.entries.content.fields.created_by.enabled')) { + flextype('emitter')->addListener('onContentCreate', static function (): void { + if (flextype('content')->registry()->get('create.data.created_by') !== null) { + return; + } + + flextype('content')->registry()->set('create.data.created_by', ''); + }); +} diff --git a/src/flextype/Foundation/Content/Fields/IdField.php b/src/flextype/Foundation/Content/Fields/IdField.php new file mode 100644 index 00000000..20490dc8 --- /dev/null +++ b/src/flextype/Foundation/Content/Fields/IdField.php @@ -0,0 +1,19 @@ +get('flextype.settings.entries.content.fields.id.enabled')) { + flextype('emitter')->addListener('onContentFetchSingleHasResult', static function (): void { + if (flextype('content')->registry()->get('fetch.data.id') !== null) { + return; + } + + flextype('content')->registry()->set('fetch.data.id', (string) strings(flextype('content')->registry()->get('fetch.id'))->trimSlashes()); + }); +} diff --git a/src/flextype/Foundation/Entries/Fields/MediaField.php b/src/flextype/Foundation/Content/Fields/MediaField.php similarity index 67% rename from src/flextype/Foundation/Entries/Fields/MediaField.php rename to src/flextype/Foundation/Content/Fields/MediaField.php index bd1d23ae..d9f9c84d 100644 --- a/src/flextype/Foundation/Entries/Fields/MediaField.php +++ b/src/flextype/Foundation/Content/Fields/MediaField.php @@ -9,14 +9,14 @@ declare(strict_types=1); use Atomastic\Arrays\Arrays; -if (flextype('registry')->get('flextype.settings.entries.fields.media.files.fetch.enabled')) { - flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void { - if (flextype('entries')->storage()->has('fetch.data.media.files.fetch')) { +if (flextype('registry')->get('flextype.settings.entries.content.fields.media.files.fetch.enabled')) { + flextype('emitter')->addListener('onContentFetchSingleHasResult', static function (): void { + if (flextype('content')->registry()->has('fetch.data.media.files.fetch')) { // Get fetch. - $original = flextype('entries')->storage()->get('fetch'); + $original = flextype('content')->registry()->get('fetch'); $data = []; - switch (flextype('registry')->get('flextype.settings.entries.fields.media.files.fetch.result')) { + switch (flextype('registry')->get('flextype.settings.entries.content.fields.media.files.fetch.result')) { case 'toArray': $resultTo = 'toArray'; break; @@ -28,7 +28,7 @@ if (flextype('registry')->get('flextype.settings.entries.fields.media.files.fetc } // Modify fetch. - foreach (flextype('entries')->storage()->get('fetch.data.media.files.fetch') as $field => $body) { + foreach (flextype('content')->registry()->get('fetch.data.media.files.fetch') as $field => $body) { if (isset($body['options']['method']) && strpos($body['options']['method'], 'fetch') !== false && @@ -50,23 +50,23 @@ if (flextype('registry')->get('flextype.settings.entries.fields.media.files.fetc } // Save fetch. - flextype('entries')->storage()->set('fetch.id', $original['id']); - flextype('entries')->storage()->set('fetch.options', $original['options']); - flextype('entries')->storage()->set('fetch.data', arrays($original['data'])->merge($data)->toArray()); + flextype('content')->registry()->set('fetch.id', $original['id']); + flextype('content')->registry()->set('fetch.options', $original['options']); + flextype('content')->registry()->set('fetch.data', arrays($original['data'])->merge($data)->toArray()); } }); } -if (flextype('registry')->get('flextype.settings.entries.fields.media.folders.fetch.enabled')) { - flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void { - if (flextype('entries')->storage()->has('fetch.data.media.folders.fetch')) { +if (flextype('registry')->get('flextype.settings.entries.content.fields.media.folders.fetch.enabled')) { + flextype('emitter')->addListener('onContentFetchSingleHasResult', static function (): void { + if (flextype('content')->registry()->has('fetch.data.media.folders.fetch')) { // Get fetch. - $original = flextype('entries')->storage()->get('fetch'); + $original = flextype('content')->registry()->get('fetch'); $data = []; - switch (flextype('registry')->get('flextype.settings.entries.fields.media.folders.fetch.result')) { + switch (flextype('registry')->get('flextype.settings.entries.content.fields.media.folders.fetch.result')) { case 'toArray': $resultTo = 'toArray'; break; @@ -78,7 +78,7 @@ if (flextype('registry')->get('flextype.settings.entries.fields.media.folders.fe } // Modify fetch. - foreach (flextype('entries')->storage()->get('fetch.data.media.folders.fetch') as $field => $body) { + foreach (flextype('content')->registry()->get('fetch.data.media.folders.fetch') as $field => $body) { if (isset($body['options']['method']) && strpos($body['options']['method'], 'fetch') !== false && @@ -100,9 +100,9 @@ if (flextype('registry')->get('flextype.settings.entries.fields.media.folders.fe } // Save fetch. - flextype('entries')->storage()->set('fetch.id', $original['id']); - flextype('entries')->storage()->set('fetch.options', $original['options']); - flextype('entries')->storage()->set('fetch.data', arrays($original['data'])->merge($data)->toArray()); + flextype('content')->registry()->set('fetch.id', $original['id']); + flextype('content')->registry()->set('fetch.options', $original['options']); + flextype('content')->registry()->set('fetch.data', arrays($original['data'])->merge($data)->toArray()); } }); } diff --git a/src/flextype/Foundation/Content/Fields/ModifiedAtField.php b/src/flextype/Foundation/Content/Fields/ModifiedAtField.php new file mode 100644 index 00000000..612a96f0 --- /dev/null +++ b/src/flextype/Foundation/Content/Fields/ModifiedAtField.php @@ -0,0 +1,18 @@ +get('flextype.settings.entries.content.fields.modified_at.enabled')) { + flextype('emitter')->addListener('onContentFetchSingleHasResult', static function (): void { + if (flextype('content')->registry()->get('fetch.data.modified_at') !== null) { + return; + } + + flextype('content')->registry()->set('fetch.data.modified_at', (int) filesystem()->file(flextype('content')->getFileLocation(flextype('content')->registry()->get('fetch.id')))->lastModified()); + }); +} diff --git a/src/flextype/Foundation/Content/Fields/ParsersField.php b/src/flextype/Foundation/Content/Fields/ParsersField.php new file mode 100644 index 00000000..02fd63cc --- /dev/null +++ b/src/flextype/Foundation/Content/Fields/ParsersField.php @@ -0,0 +1,52 @@ +get('flextype.settings.entries.content.fields.parsers.enabled')) { + flextype('emitter')->addListener('onContentFetchSingleHasResult', static function (): void { + processParsersField(); + }); +} + +function processParsersField(): void +{ + if (flextype('content')->registry()->get('fetch.data.cache.enabled') == null) { + $cache = false; + } else { + $cache = (bool) flextype('content')->registry()->get('fetch.data.cache.enabled'); + } + + if (flextype('content')->registry()->get('fetch.data.parsers') != null) { + foreach (flextype('content')->registry()->get('fetch.data.parsers') as $parserName => $parserData) { + if (in_array($parserName, ['markdown', 'shortcode'])) { + if (flextype('content')->registry()->get('fetch.data.parsers.'.$parserName.'.enabled') === true) { + if (flextype('content')->registry()->get('fetch.data.parsers.'.$parserName.'.fields') != null) { + if (is_array(flextype('content')->registry()->get('fetch.data.parsers.'.$parserName.'.fields'))) { + foreach (flextype('content')->registry()->get('fetch.data.parsers.'.$parserName.'.fields') as $field) { + if (! in_array($field, flextype('registry')->get('flextype.settings.entries.content.fields'))) { + if ($parserName == 'markdown') { + if (arrays(flextype('content')->registry()->get('fetch.data'))->has($field)) { + flextype('content')->registry()->set('fetch.data.'.$field, + flextype('parsers')->markdown()->parse(flextype('content')->registry()->get('fetch.data.'.$field), $cache)); + } + } + if ($parserName == 'shortcode') { + if (arrays(flextype('content')->registry()->get('fetch.data'))->has($field)) { + flextype('content')->registry()->set('fetch.data.'.$field, + flextype('parsers')->shortcode()->process(flextype('content')->registry()->get('fetch.data.'.$field), $cache)); + } + } + } + } + } + } + } + } + } + } +} diff --git a/src/flextype/Foundation/Content/Fields/PublishedAtField.php b/src/flextype/Foundation/Content/Fields/PublishedAtField.php new file mode 100644 index 00000000..7fb4d691 --- /dev/null +++ b/src/flextype/Foundation/Content/Fields/PublishedAtField.php @@ -0,0 +1,26 @@ +get('flextype.settings.entries.content.fields.published_at.enabled')) { + flextype('emitter')->addListener('onContentFetchSingleHasResult', static function (): void { + if (flextype('content')->registry()->get('fetch.data.published_at') === null) { + flextype('content')->registry()->set('fetch.data.published_at', (int) filesystem()->file(flextype('content')->getFileLocation(flextype('content')->registry()->get('fetch.id')))->lastModified()); + } else { + flextype('content')->registry()->set('fetch.data.published_at', (int) strtotime((string) flextype('content')->registry()->get('fetch.data.published_at'))); + } + }); + + flextype('emitter')->addListener('onContentCreate', static function (): void { + if (flextype('content')->registry()->get('create.data.published_at') !== null) { + return; + } + + flextype('content')->registry()->set('create.data.published_at', date(flextype('registry')->get('flextype.settings.date_format'), time())); + }); +} diff --git a/src/flextype/Foundation/Content/Fields/PublishedByField.php b/src/flextype/Foundation/Content/Fields/PublishedByField.php new file mode 100644 index 00000000..d0ea42c3 --- /dev/null +++ b/src/flextype/Foundation/Content/Fields/PublishedByField.php @@ -0,0 +1,18 @@ +get('flextype.settings.entries.content.fields.published_by.enabled')) { + flextype('emitter')->addListener('onContentCreate', static function (): void { + if (flextype('content')->registry()->get('create.data.published_by') !== null) { + return; + } + + flextype('content')->registry()->set('create.data.published_by', ''); + }); +} diff --git a/src/flextype/Foundation/Entries/Fields/RegistryField.php b/src/flextype/Foundation/Content/Fields/RegistryField.php similarity index 56% rename from src/flextype/Foundation/Entries/Fields/RegistryField.php rename to src/flextype/Foundation/Content/Fields/RegistryField.php index 11eeb326..99548567 100644 --- a/src/flextype/Foundation/Entries/Fields/RegistryField.php +++ b/src/flextype/Foundation/Content/Fields/RegistryField.php @@ -7,16 +7,16 @@ declare(strict_types=1); * Founded by Sergey Romanenko and maintained by Flextype Community. */ -if (flextype('registry')->get('flextype.settings.entries.fields.registry.get.enabled')) { - flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void { - if (flextype('entries')->storage()->has('fetch.data.registry.get')) { +if (flextype('registry')->get('flextype.settings.entries.content.fields.registry.get.enabled')) { + flextype('emitter')->addListener('onContentFetchSingleHasResult', static function (): void { + if (flextype('content')->registry()->has('fetch.data.registry.get')) { // Get fetch. - $original = flextype('entries')->storage()->get('fetch'); + $original = flextype('content')->registry()->get('fetch'); $data = []; // Modify fetch. - foreach (flextype('entries')->storage()->get('fetch.data.registry.get') as $field => $body) { + foreach (flextype('content')->registry()->get('fetch.data.registry.get') as $field => $body) { $data = arrays($data)->merge(arrays($data)->set($field, flextype('registry')->get($body['key'], isset($body['default']) ? $body['default'] : @@ -25,7 +25,7 @@ if (flextype('registry')->get('flextype.settings.entries.fields.registry.get.ena } // Save fetch. - flextype('entries')->storage()->set('fetch.data', arrays($original['data'])->merge($data)->toArray()); + flextype('content')->registry()->set('fetch.data', arrays($original['data'])->merge($data)->toArray()); } }); } diff --git a/src/flextype/Foundation/Content/Fields/RoutableField.php b/src/flextype/Foundation/Content/Fields/RoutableField.php new file mode 100644 index 00000000..7ae10572 --- /dev/null +++ b/src/flextype/Foundation/Content/Fields/RoutableField.php @@ -0,0 +1,27 @@ +get('flextype.settings.entries.content.fields.routable.enabled')) { + flextype('emitter')->addListener('onContentFetchSingleHasResult', static function (): void { + if (flextype('content')->registry()->get('fetch.data.routable') === null) { + flextype('content')->registry()->set('fetch.data.routable', true); + } else { + flextype('content')->registry()->set('fetch.data.routable', (bool) flextype('content')->registry()->get('fetch.data.routable')); + } + }); + + flextype('emitter')->addListener('onContentCreate', static function (): void { + if (flextype('content')->registry()->get('create.data.routable') === null) { + flextype('content')->registry()->set('create.data.routable', true); + } else { + flextype('content')->registry()->set('create.data.routable', (bool) flextype('content')->registry()->get('create.data.routable')); + } + }); +} diff --git a/src/flextype/Foundation/Content/Fields/SlugField.php b/src/flextype/Foundation/Content/Fields/SlugField.php new file mode 100644 index 00000000..64f72ca1 --- /dev/null +++ b/src/flextype/Foundation/Content/Fields/SlugField.php @@ -0,0 +1,20 @@ +get('flextype.settings.entries.content.fields.slug.enabled')) { + flextype('emitter')->addListener('onContentFetchSingleHasResult', static function (): void { + if (flextype('content')->registry()->get('fetch.data.slug') !== null) { + return; + } + + $parts = explode('/', ltrim(rtrim(flextype('content')->registry()->get('fetch.id'), '/'), '/')); + flextype('content')->registry()->set('fetch.data.slug', (string) end($parts)); + }); +} diff --git a/src/flextype/Foundation/Content/Fields/UuidField.php b/src/flextype/Foundation/Content/Fields/UuidField.php new file mode 100644 index 00000000..9553236d --- /dev/null +++ b/src/flextype/Foundation/Content/Fields/UuidField.php @@ -0,0 +1,20 @@ +get('flextype.settings.entries.content.fields.uuid.enabled')) { + flextype('emitter')->addListener('onContentCreate', static function (): void { + if (flextype('content')->registry()->get('create.data.uuid') !== null) { + return; + } + + flextype('content')->registry()->set('create.data.uuid', Uuid::uuid4()->toString()); + }); +} diff --git a/src/flextype/Foundation/Content/Fields/VisibilityField.php b/src/flextype/Foundation/Content/Fields/VisibilityField.php new file mode 100644 index 00000000..caf76727 --- /dev/null +++ b/src/flextype/Foundation/Content/Fields/VisibilityField.php @@ -0,0 +1,32 @@ +get('flextype.settings.entries.content.fields.visibility.enabled')) { + $visibility = [ + 'draft' => 'draft', + 'hidden' => 'hidden', + 'visible' => 'visible', + ]; + + flextype('emitter')->addListener('onContentFetchSingleHasResult', static function () use ($visibility): void { + if (flextype('content')->registry()->get('fetch.data.visibility') !== null && in_array(flextype('content')->registry()->get('fetch.data.visibility'), $visibility)) { + flextype('content')->registry()->set('fetch.data.visibility', (string) $visibility[flextype('content')->registry()->get('fetch.data.visibility')]); + } else { + flextype('content')->registry()->set('fetch.data.visibility', (string) $visibility['visible']); + } + }); + + flextype('emitter')->addListener('onContentCreate', static function () use ($visibility): void { + if (flextype('content')->registry()->get('create.data.visibility') !== null && in_array(flextype('content')->registry()->get('create.data.visibility'), $visibility)) { + flextype('content')->registry()->set('create.data.visibility', (string) $visibility[flextype('content')->registry()->get('create.data.visibility')]); + } else { + flextype('content')->registry()->set('create.data.visibility', (string) $visibility['visible']); + } + }); +} diff --git a/src/flextype/Foundation/Entries/Entries.php b/src/flextype/Foundation/Entries/Entries.php deleted file mode 100755 index 59b4f2e5..00000000 --- a/src/flextype/Foundation/Entries/Entries.php +++ /dev/null @@ -1,424 +0,0 @@ -storage = arrays(); - } - - /** - * Get Entries Storage - * - * @return Arrays - */ - public function storage(): Arrays - { - return $this->storage; - } - - /** - * Fetch. - * - * @param string $id Unique identifier of the entry. - * @param array $options Options array. - * - * @access public - * - * @return Arrays Returns instance of The Arrays class with items. - */ - public function fetch(string $id, array $options = []): Arrays - { - // Store data - $this->storage()->set('fetch.id', $id); - $this->storage()->set('fetch.options', $options); - $this->storage()->set('fetch.data', []); - - // Run event: onEntriesFetch - flextype('emitter')->emit('onEntriesFetch'); - - // Single fetch helper - $single = function ($id, $options) { - - // Store data - $this->storage()->set('fetch.id', $id); - $this->storage()->set('fetch.options', $options); - $this->storage()->set('fetch.data', []); - - // Run event: onEntriesFetchSingle - flextype('emitter')->emit('onEntriesFetchSingle'); - - // Get Cache ID for current requested entry - $entryCacheID = $this->getCacheID($this->storage()->get('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()->set('fetch.data', filter(flextype('cache')->get($entryCacheID), - $this->storage()->get('fetch.options.filter', []))); - - // Run event: onEntriesFetchSingleCacheHasResult - flextype('emitter')->emit('onEntriesFetchSingleCacheHasResult'); - - // Return entry from cache - return arrays($this->storage()->get('fetch.data')); - } - - // 2. Try to get current requested entry from filesystem - if ($this->has($this->storage()->get('fetch.id'))) { - // Get entry file location - $entryFile = $this->getFileLocation($this->storage()->get('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->storage()->get('fetch.data')); - } - - // Decode entry file content - $this->storage()->set('fetch.data', flextype('serializers')->frontmatter()->decode($entryFileContent)); - - // Run event: onEntriesFetchSingleHasResult - flextype('emitter')->emit('onEntriesFetchSingleHasResult'); - - // Apply filter for fetch data - $this->storage()->set('fetch.data', filter($this->storage()->get('fetch.data'), - $this->storage()->get('fetch.options.filter', []))); - - // Set cache state - $cache = $this->storage()->get('fetch.data.cache.enabled', - flextype('registry')->get('flextype.settings.cache.enabled')); - - // Save entry data to cache - if ($cache) { - flextype('cache')->set($entryCacheID, $this->storage()->get('fetch.data')); - } - - // Return entry data - return arrays($this->storage()->get('fetch.data')); - } - - // Run event: onEntriesFetchSingleNoResult - flextype('emitter')->emit('onEntriesFetchSingleNoResult'); - - // Return empty array if entry is not founded - return arrays($this->storage()->get('fetch.data')); - }; - - if (isset($this->storage['fetch']['options']['collection']) && - strings($this->storage['fetch']['options']['collection'])->isTrue()) { - - // Run event: onEntriesFetchCollection - flextype('emitter')->emit('onEntriesFetchCollection'); - - if (! $this->getDirectoryLocation($id)) { - // Run event: onEntriesFetchCollectionNoResult - flextype('emitter')->emit('onEntriesFetchCollectionNoResult'); - - // Return entries array - return arrays($this->storage()->get('fetch.data')); - } - - // Find entries in the filesystem - $entries = find($this->getDirectoryLocation($id), - isset($options['find']) ? - $options['find'] : - []); - - // Walk through entries results - if ($entries->hasResults()) { - - $data = []; - - 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] = $single($currentEntryID, [])->toArray(); - } - - $this->storage()->set('fetch.data', $data); - - // Run event: onEntriesFetchCollectionHasResult - flextype('emitter')->emit('onEntriesFetchCollectionHasResult'); - - // Apply filter for fetch data - $this->storage()->set('fetch.data', filter($this->storage()->get('fetch.data'), - isset($options['filter']) ? - $options['filter'] : - [])); - } - - // Run event: onEntriesFetchCollectionNoResult - flextype('emitter')->emit('onEntriesFetchCollectionNoResult'); - - // Return entries array - return arrays($this->storage()->get('fetch.data')); - } else { - return $single($this->storage['fetch']['id'], - $this->storage['fetch']['options']); - } - } - - /** - * Move entry - * - * @param string $id Unique identifier of the entry. - * @param string $newID New Unique identifier of the entry. - * - * @return bool True on success, false on failure. - * - * @access public - */ - public function move(string $id, string $newID): bool - { - // Store data - $this->storage()->set('move.id', $id); - $this->storage()->set('move.newID', $newID); - - // Run event: onEntriesMove - flextype('emitter')->emit('onEntriesMove'); - - if (! $this->has($this->storage()->get('move.newID'))) { - return filesystem() - ->directory($this->getDirectoryLocation($this->storage()->get('move.id'))) - ->move($this->getDirectoryLocation($this->storage()->get('move.newID'))); - } - - return false; - } - - /** - * Update entry - * - * @param string $id Unique identifier of the entry. - * @param array $data Data to update for the entry. - * - * @return bool True on success, false on failure. - * - * @access public - */ - public function update(string $id, array $data): bool - { - // Store data - $this->storage()->set('update.id', $id); - $this->storage()->set('update.data', $data); - - // Run event: onEntriesUpdate - flextype('emitter')->emit('onEntriesUpdate'); - - $entryFile = $this->getFileLocation($this->storage()->get('update.id')); - - if (filesystem()->file($entryFile)->exists()) { - $body = filesystem()->file($entryFile)->get(); - $entry = flextype('serializers')->frontmatter()->decode($body); - - return (bool) filesystem()->file($entryFile)->put(flextype('serializers')->frontmatter()->encode(array_merge($entry, $this->storage()->get('update.data')))); - } - - return false; - } - - /** - * Create entry. - * - * @param string $id Unique identifier of the entry. - * @param array $data Data to create for the entry. - * - * @return bool True on success, false on failure. - * - * @access public - */ - public function create(string $id, array $data = []): bool - { - // Store data - $this->storage()->set('create.id', $id); - $this->storage()->set('create.data', $data); - - // Run event: onEntriesCreate - flextype('emitter')->emit('onEntriesCreate'); - - // Create entry directory first if it is not exists - $entryDir = $this->getDirectoryLocation($this->storage()->get('create.id')); - - if ( - ! filesystem()->directory($entryDir)->exists() && - ! filesystem()->directory($entryDir)->create() - ) { - return false; - } - - // Create entry file - $entryFile = $entryDir . '/entry' . '.' . flextype('registry')->get('flextype.settings.entries.extension'); - if (! filesystem()->file($entryFile)->exists()) { - return (bool) filesystem()->file($entryFile)->put(flextype('serializers')->frontmatter()->encode($this->storage()->get('create.data'))); - } - - return false; - } - - /** - * Delete entry. - * - * @param string $id Unique identifier of the entry. - * - * @return bool True on success, false on failure. - * - * @access public - */ - public function delete(string $id): bool - { - // Store data - $this->storage()->set('delete.id', $id); - - // Run event: onEntriesDelete - flextype('emitter')->emit('onEntriesDelete'); - - return filesystem() - ->directory($this->getDirectoryLocation($this->storage()->get('delete.id'))) - ->delete(); - } - - /** - * Copy entry. - * - * @param string $id Unique identifier of the entry. - * @param string $newID New Unique identifier of the entry. - * - * @return bool|null True on success, false on failure. - * - * @access public - */ - public function copy(string $id, string $newID): ?bool - { - // Store data - $this->storage()->set('copy.id', $id); - $this->storage()->set('copy.newID', $newID); - - // Run event: onEntriesCopy - flextype('emitter')->emit('onEntriesCopy'); - - return filesystem() - ->directory($this->getDirectoryLocation($this->storage()->get('copy.id'))) - ->copy($this->getDirectoryLocation($this->storage()->get('copy.newID'))); - } - - /** - * Check whether entry exists - * - * @param string $id Unique identifier of the entry(entries). - * - * @return bool True on success, false on failure. - * - * @access public - */ - public function has(string $id): bool - { - // Store data - $this->storage()->set('has.id', $id); - - // Run event: onEntriesHas - flextype('emitter')->emit('onEntriesHas'); - - return filesystem()->file($this->getFileLocation($this->storage()->get('has.id')))->exists(); - } - - /** - * Get entry file location - * - * @param string $id Unique identifier of the entry(entries). - * - * @return string entry file location - * - * @access public - */ - public function getFileLocation(string $id): string - { - return PATH['project'] . '/entries/' . $id . '/entry' . '.' . flextype('registry')->get('flextype.settings.entries.extension'); - } - - /** - * Get entry directory location - * - * @param string $id Unique identifier of the entry(entries). - * - * @return string entry directory location - * - * @access public - */ - public function getDirectoryLocation(string $id): string - { - return PATH['project'] . '/entries/' . $id; - } - - /** - * Get Cache ID for entry - * - * @param string $id Unique identifier of the entry(entries). - * - * @return string Cache ID - * - * @access public - */ - public function getCacheID(string $id): string - { - if (flextype('registry')->get('flextype.settings.cache.enabled') === false) { - return ''; - } - - $entryFile = $this->getFileLocation($id); - - if (filesystem()->file($entryFile)->exists()) { - return strings('entry' . $entryFile . (filesystem()->file($entryFile)->lastModified() ?: ''))->hash()->toString(); - } - - return strings('entry' . $entryFile)->hash()->toString(); - } -} diff --git a/src/flextype/Foundation/Entries/Fields/CreatedAtField.php b/src/flextype/Foundation/Entries/Fields/CreatedAtField.php deleted file mode 100644 index 70eed995..00000000 --- a/src/flextype/Foundation/Entries/Fields/CreatedAtField.php +++ /dev/null @@ -1,27 +0,0 @@ -get('flextype.settings.entries.fields.created_at.enabled')) { - flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void { - if (flextype('entries')->storage()->get('fetch.data.created_at') === null) { - flextype('entries')->storage()->set('fetch.data.created_at', (int) filesystem()->file(flextype('entries')->getFileLocation(flextype('entries')->storage()->get('fetch.id')))->lastModified()); - } else { - flextype('entries')->storage()->set('fetch.data.created_at', (int) strtotime((string) flextype('entries')->storage()->get('fetch.data.created_at'))); - } - }); - - flextype('emitter')->addListener('onEntriesCreate', static function (): void { - if (flextype('entries')->storage()->get('create.data.created_at') !== null) { - return; - } - - flextype('entries')->storage()->set('create.data.created_at', date(flextype('registry')->get('flextype.settings.date_format'), time())); - }); -} diff --git a/src/flextype/Foundation/Entries/Fields/CreatedByField.php b/src/flextype/Foundation/Entries/Fields/CreatedByField.php deleted file mode 100644 index f553584b..00000000 --- a/src/flextype/Foundation/Entries/Fields/CreatedByField.php +++ /dev/null @@ -1,18 +0,0 @@ -get('flextype.settings.entries.fields.created_by.enabled')) { - flextype('emitter')->addListener('onEntriesCreate', static function (): void { - if (flextype('entries')->storage()->get('create.data.created_by') !== null) { - return; - } - - flextype('entries')->storage()->set('create.data.created_by', ''); - }); -} diff --git a/src/flextype/Foundation/Entries/Fields/IdField.php b/src/flextype/Foundation/Entries/Fields/IdField.php deleted file mode 100644 index d0fec2ad..00000000 --- a/src/flextype/Foundation/Entries/Fields/IdField.php +++ /dev/null @@ -1,19 +0,0 @@ -get('flextype.settings.entries.fields.id.enabled')) { - flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void { - if (flextype('entries')->storage()->get('fetch.data.id') !== null) { - return; - } - - flextype('entries')->storage()->set('fetch.data.id', (string) strings(flextype('entries')->storage()->get('fetch.id'))->trimSlashes()); - }); -} diff --git a/src/flextype/Foundation/Entries/Fields/ModifiedAtField.php b/src/flextype/Foundation/Entries/Fields/ModifiedAtField.php deleted file mode 100644 index fd8df1b4..00000000 --- a/src/flextype/Foundation/Entries/Fields/ModifiedAtField.php +++ /dev/null @@ -1,18 +0,0 @@ -get('flextype.settings.entries.fields.modified_at.enabled')) { - flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void { - if (flextype('entries')->storage()->get('fetch.data.modified_at') !== null) { - return; - } - - flextype('entries')->storage()->set('fetch.data.modified_at', (int) filesystem()->file(flextype('entries')->getFileLocation(flextype('entries')->storage()->get('fetch.id')))->lastModified()); - }); -} diff --git a/src/flextype/Foundation/Entries/Fields/ParsersField.php b/src/flextype/Foundation/Entries/Fields/ParsersField.php deleted file mode 100644 index 24bb5d19..00000000 --- a/src/flextype/Foundation/Entries/Fields/ParsersField.php +++ /dev/null @@ -1,52 +0,0 @@ -get('flextype.settings.entries.fields.parsers.enabled')) { - flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void { - processParsersField(); - }); -} - -function processParsersField(): void -{ - if (flextype('entries')->storage()->get('fetch.data.cache.enabled') == null) { - $cache = false; - } else { - $cache = (bool) flextype('entries')->storage()->get('fetch.data.cache.enabled'); - } - - if (flextype('entries')->storage()->get('fetch.data.parsers') != null) { - foreach (flextype('entries')->storage()->get('fetch.data.parsers') as $parserName => $parserData) { - if (in_array($parserName, ['markdown', 'shortcode'])) { - if (flextype('entries')->storage()->get('fetch.data.parsers.'.$parserName.'.enabled') === true) { - if (flextype('entries')->storage()->get('fetch.data.parsers.'.$parserName.'.fields') != null) { - if (is_array(flextype('entries')->storage()->get('fetch.data.parsers.'.$parserName.'.fields'))) { - foreach (flextype('entries')->storage()->get('fetch.data.parsers.'.$parserName.'.fields') as $field) { - if (! in_array($field, flextype('registry')->get('flextype.settings.entries.fields'))) { - if ($parserName == 'markdown') { - if (arrays(flextype('entries')->storage()->get('fetch.data'))->has($field)) { - flextype('entries')->storage()->set('fetch.data.'.$field, - flextype('parsers')->markdown()->parse(flextype('entries')->storage()->get('fetch.data.'.$field), $cache)); - } - } - if ($parserName == 'shortcode') { - if (arrays(flextype('entries')->storage()->get('fetch.data'))->has($field)) { - flextype('entries')->storage()->set('fetch.data.'.$field, - flextype('parsers')->shortcode()->process(flextype('entries')->storage()->get('fetch.data.'.$field), $cache)); - } - } - } - } - } - } - } - } - } - } -} diff --git a/src/flextype/Foundation/Entries/Fields/PublishedAtField.php b/src/flextype/Foundation/Entries/Fields/PublishedAtField.php deleted file mode 100644 index 0fda2c8e..00000000 --- a/src/flextype/Foundation/Entries/Fields/PublishedAtField.php +++ /dev/null @@ -1,26 +0,0 @@ -get('flextype.settings.entries.fields.published_at.enabled')) { - flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void { - if (flextype('entries')->storage()->get('fetch.data.published_at') === null) { - flextype('entries')->storage()->set('fetch.data.published_at', (int) filesystem()->file(flextype('entries')->getFileLocation(flextype('entries')->storage()->get('fetch.id')))->lastModified()); - } else { - flextype('entries')->storage()->set('fetch.data.published_at', (int) strtotime((string) flextype('entries')->storage()->get('fetch.data.published_at'))); - } - }); - - flextype('emitter')->addListener('onEntriesCreate', static function (): void { - if (flextype('entries')->storage()->get('create.data.published_at') !== null) { - return; - } - - flextype('entries')->storage()->set('create.data.published_at', date(flextype('registry')->get('flextype.settings.date_format'), time())); - }); -} diff --git a/src/flextype/Foundation/Entries/Fields/PublishedByField.php b/src/flextype/Foundation/Entries/Fields/PublishedByField.php deleted file mode 100644 index 822f964f..00000000 --- a/src/flextype/Foundation/Entries/Fields/PublishedByField.php +++ /dev/null @@ -1,18 +0,0 @@ -get('flextype.settings.entries.fields.published_by.enabled')) { - flextype('emitter')->addListener('onEntriesCreate', static function (): void { - if (flextype('entries')->storage()->get('create.data.published_by') !== null) { - return; - } - - flextype('entries')->storage()->set('create.data.published_by', ''); - }); -} diff --git a/src/flextype/Foundation/Entries/Fields/RoutableField.php b/src/flextype/Foundation/Entries/Fields/RoutableField.php deleted file mode 100644 index d60acb6d..00000000 --- a/src/flextype/Foundation/Entries/Fields/RoutableField.php +++ /dev/null @@ -1,27 +0,0 @@ -get('flextype.settings.entries.fields.routable.enabled')) { - flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void { - if (flextype('entries')->storage()->get('fetch.data.routable') === null) { - flextype('entries')->storage()->set('fetch.data.routable', true); - } else { - flextype('entries')->storage()->set('fetch.data.routable', (bool) flextype('entries')->storage()->get('fetch.data.routable')); - } - }); - - flextype('emitter')->addListener('onEntriesCreate', static function (): void { - if (flextype('entries')->storage()->get('create.data.routable') === null) { - flextype('entries')->storage()->set('create.data.routable', true); - } else { - flextype('entries')->storage()->set('create.data.routable', (bool) flextype('entries')->storage()->get('create.data.routable')); - } - }); -} diff --git a/src/flextype/Foundation/Entries/Fields/SlugField.php b/src/flextype/Foundation/Entries/Fields/SlugField.php deleted file mode 100644 index 132f9a50..00000000 --- a/src/flextype/Foundation/Entries/Fields/SlugField.php +++ /dev/null @@ -1,20 +0,0 @@ -get('flextype.settings.entries.fields.slug.enabled')) { - flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void { - if (flextype('entries')->storage()->get('fetch.data.slug') !== null) { - return; - } - - $parts = explode('/', ltrim(rtrim(flextype('entries')->storage()->get('fetch.id'), '/'), '/')); - flextype('entries')->storage()->set('fetch.data.slug', (string) end($parts)); - }); -} diff --git a/src/flextype/Foundation/Entries/Fields/UuidField.php b/src/flextype/Foundation/Entries/Fields/UuidField.php deleted file mode 100644 index 1ce7b0ea..00000000 --- a/src/flextype/Foundation/Entries/Fields/UuidField.php +++ /dev/null @@ -1,20 +0,0 @@ -get('flextype.settings.entries.fields.uuid.enabled')) { - flextype('emitter')->addListener('onEntriesCreate', static function (): void { - if (flextype('entries')->storage()->get('create.data.uuid') !== null) { - return; - } - - flextype('entries')->storage()->set('create.data.uuid', Uuid::uuid4()->toString()); - }); -} diff --git a/src/flextype/Foundation/Entries/Fields/VisibilityField.php b/src/flextype/Foundation/Entries/Fields/VisibilityField.php deleted file mode 100644 index e48400a2..00000000 --- a/src/flextype/Foundation/Entries/Fields/VisibilityField.php +++ /dev/null @@ -1,32 +0,0 @@ -get('flextype.settings.entries.fields.visibility.enabled')) { - $visibility = [ - 'draft' => 'draft', - 'hidden' => 'hidden', - 'visible' => 'visible', - ]; - - flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function () use ($visibility): void { - if (flextype('entries')->storage()->get('fetch.data.visibility') !== null && in_array(flextype('entries')->storage()->get('fetch.data.visibility'), $visibility)) { - flextype('entries')->storage()->set('fetch.data.visibility', (string) $visibility[flextype('entries')->storage()->get('fetch.data.visibility')]); - } else { - flextype('entries')->storage()->set('fetch.data.visibility', (string) $visibility['visible']); - } - }); - - flextype('emitter')->addListener('onEntriesCreate', static function () use ($visibility): void { - if (flextype('entries')->storage()->get('create.data.visibility') !== null && in_array(flextype('entries')->storage()->get('create.data.visibility'), $visibility)) { - flextype('entries')->storage()->set('create.data.visibility', (string) $visibility[flextype('entries')->storage()->get('create.data.visibility')]); - } else { - flextype('entries')->storage()->set('create.data.visibility', (string) $visibility['visible']); - } - }); -} diff --git a/src/flextype/flextype.php b/src/flextype/flextype.php index 537db2b7..b178c8e7 100755 --- a/src/flextype/flextype.php +++ b/src/flextype/flextype.php @@ -17,7 +17,7 @@ use Cocur\Slugify\Slugify; use DateTimeZone; use Flextype\Foundation\Actions; use Flextype\Foundation\Cors; -use Flextype\Foundation\Entries\Entries; +use Flextype\Foundation\Content\Content; use Flextype\Foundation\Flextype; use Flextype\Foundation\Media\Media; use Flextype\Foundation\Plugins; @@ -371,9 +371,9 @@ flextype()->container()['images'] = static function () { }; /** - * Add entries service to Flextype container + * Add content service to Flextype container */ -flextype()->container()['entries'] = static fn () => new Entries(); +flextype()->container()['content'] = static fn () => new Content(flextype('registry')->get('flextype.settings.entries.content')); /** * Add media service to Flextype container @@ -437,22 +437,6 @@ foreach ($shortcodes as $shortcodeName => $shortcode) { include_once $shortcodeFilePath; } -/** - * Init entries fields - * - * Load Flextype Entries fields from directory /flextype/Foundation/Entries/Fields/ based on flextype.settings.entries.fields array - */ -$entryFields = flextype('registry')->get('flextype.settings.entries.fields'); - -foreach ($entryFields as $fieldName => $field) { - $entryFieldFilePath = ROOT_DIR . '/src/flextype/Foundation/Entries/Fields/' . str_replace('_', '', ucwords($fieldName, '_')) . 'Field.php'; - if (! file_exists($entryFieldFilePath)) { - continue; - } - - include_once $entryFieldFilePath; -} - /** * Init plugins */