From c4de50ae1e431e48cd27c354225b4feed8123eeb Mon Sep 17 00:00:00 2001 From: Awilum Date: Sat, 25 Jul 2020 21:52:12 +0300 Subject: [PATCH] feat(entries): Simplify and improve Entries API #439 --- src/flextype/Foundation/Entries/Entries.php | 21 +++++-------------- .../Entries/Fields/CreatedAtField.php | 4 ++-- .../Entries/Fields/CreatedByField.php | 2 +- .../Entries/Fields/ModifiedAtField.php | 2 +- .../Entries/Fields/ParsersField.php | 2 +- .../Entries/Fields/PublishedAtField.php | 4 ++-- .../Entries/Fields/PublishedByField.php | 2 +- .../Entries/Fields/RoutableField.php | 4 ++-- .../Foundation/Entries/Fields/SlugField.php | 2 +- .../Foundation/Entries/Fields/UuidField.php | 2 +- .../Entries/Fields/VisibilityField.php | 4 ++-- 11 files changed, 19 insertions(+), 30 deletions(-) diff --git a/src/flextype/Foundation/Entries/Entries.php b/src/flextype/Foundation/Entries/Entries.php index 525b7a66..4ca8607d 100755 --- a/src/flextype/Foundation/Entries/Entries.php +++ b/src/flextype/Foundation/Entries/Entries.php @@ -9,24 +9,15 @@ declare(strict_types=1); namespace Flextype\Foundation\Entries; -use Flextype\Component\Arrays\Arrays; use Flextype\Component\Filesystem\Filesystem; -use Flextype\Component\Session\Session; -use Ramsey\Uuid\Uuid; use function array_merge; use function count; -use function date; -use function in_array; -use function is_array; -use function is_bool; use function ltrim; use function md5; use function rename; use function rtrim; use function str_replace; use function strpos; -use function strtotime; -use function time; class Entries { @@ -138,13 +129,14 @@ class Entries // Try to get current requested entry from filesystem if ($this->has($this->entry_path)) { - // Get entry file location $entry_file = $this->getFileLocation($this->entry_path); // Try to get requested entry from the filesystem $entry_file_content = Filesystem::read($entry_file); - if ($entry_file_content === false) return []; + if ($entry_file_content === false) { + return []; + } // Decode entry file content $this->entry = $this->flextype['frontmatter']->decode($entry_file_content); @@ -153,8 +145,7 @@ class Entries $this->flextype['emitter']->emit('onEntryAfterInitialized'); // Set cache state - $cache = isset($this->flextype['entries']->entry['cache']['enabled']) ? - $this->flextype['entries']->entry['cache']['enabled'] : + $cache = $this->flextype['entries']->entry['cache']['enabled'] ?? $this->flextype['registry']->get('flextype.settings.cache.enabled'); // Save entry data to cache @@ -239,7 +230,6 @@ class Entries public function rename(string $path, string $new_path) : bool { if (! Filesystem::has($this->getDirLocation($new_path))) { - // Run event: onEntryRename $this->flextype['emitter']->emit('onEntryRename'); @@ -298,7 +288,6 @@ class Entries if (Filesystem::createDir($entry_dir)) { // Check if new entry file exists if (! Filesystem::has($entry_file = $entry_dir . '/entry' . '.' . $this->flextype->registry->get('flextype.settings.entries.extension'))) { - // Store data in the entry_create_data $this->entry_create_data = $data; @@ -403,7 +392,7 @@ class Entries * * @access public */ - public function getCacheID($path) : string + public function getCacheID(string $path) : string { if ($this->flextype['registry']->get('flextype.settings.cache.enabled') === false) { return ''; diff --git a/src/flextype/Foundation/Entries/Fields/CreatedAtField.php b/src/flextype/Foundation/Entries/Fields/CreatedAtField.php index 128c8cd7..a32852c9 100644 --- a/src/flextype/Foundation/Entries/Fields/CreatedAtField.php +++ b/src/flextype/Foundation/Entries/Fields/CreatedAtField.php @@ -9,13 +9,13 @@ declare(strict_types=1); use Flextype\Component\Filesystem\Filesystem; -$flextype->emitter->addListener('onEntryAfterInitialized', function() use ($flextype) { +$flextype->emitter->addListener('onEntryAfterInitialized', function () use ($flextype) { $flextype->entries->entry['created_at'] = isset($flextype->entries->entry['created_at']) ? (int) strtotime($flextype->entries->entry['created_at']) : (int) Filesystem::getTimestamp($flextype->entries->getFileLocation($flextype->entries->entry_path)); }); -$flextype->emitter->addListener('onEntryCreate', function() use ($flextype) { +$flextype->emitter->addListener('onEntryCreate', function () use ($flextype) { if (isset($flextype->entries->entry_create_data['created_at'])) { $flextype->entries->entry_create_data['created_at'] = $flextype->entries->entry_create_data['created_at']; } else { diff --git a/src/flextype/Foundation/Entries/Fields/CreatedByField.php b/src/flextype/Foundation/Entries/Fields/CreatedByField.php index ac46db60..eb3865e8 100644 --- a/src/flextype/Foundation/Entries/Fields/CreatedByField.php +++ b/src/flextype/Foundation/Entries/Fields/CreatedByField.php @@ -7,7 +7,7 @@ declare(strict_types=1); * Founded by Sergey Romanenko and maintained by Flextype Community. */ -$flextype->emitter->addListener('onEntryCreate', function() use ($flextype) { +$flextype->emitter->addListener('onEntryCreate', function () use ($flextype) { if (isset($flextype->entries->entry_create_data['created_by'])) { $flextype->entries->entry_create_data['created_by'] = $flextype->entries->entry_create_data['created_by']; } else { diff --git a/src/flextype/Foundation/Entries/Fields/ModifiedAtField.php b/src/flextype/Foundation/Entries/Fields/ModifiedAtField.php index f58c1cc6..e1e2f091 100644 --- a/src/flextype/Foundation/Entries/Fields/ModifiedAtField.php +++ b/src/flextype/Foundation/Entries/Fields/ModifiedAtField.php @@ -9,6 +9,6 @@ declare(strict_types=1); use Flextype\Component\Filesystem\Filesystem; -$flextype->emitter->addListener('onEntryAfterInitialized', function() use ($flextype) { +$flextype->emitter->addListener('onEntryAfterInitialized', function () use ($flextype) { $flextype->entries->entry['modified_at'] = (int) Filesystem::getTimestamp($flextype->entries->getFileLocation($flextype->entries->entry_path)); }); diff --git a/src/flextype/Foundation/Entries/Fields/ParsersField.php b/src/flextype/Foundation/Entries/Fields/ParsersField.php index 42a0f141..94e22309 100644 --- a/src/flextype/Foundation/Entries/Fields/ParsersField.php +++ b/src/flextype/Foundation/Entries/Fields/ParsersField.php @@ -10,7 +10,7 @@ declare(strict_types=1); use Flextype\Component\Filesystem\Filesystem; use Flextype\Component\Arrays\Arrays; -$flextype->emitter->addListener('onEntryAfterInitialized', function() use ($flextype) { +$flextype->emitter->addListener('onEntryAfterInitialized', function () use ($flextype) { processParsersField($flextype); }); diff --git a/src/flextype/Foundation/Entries/Fields/PublishedAtField.php b/src/flextype/Foundation/Entries/Fields/PublishedAtField.php index 2e4ecc40..2536ecd6 100644 --- a/src/flextype/Foundation/Entries/Fields/PublishedAtField.php +++ b/src/flextype/Foundation/Entries/Fields/PublishedAtField.php @@ -9,13 +9,13 @@ declare(strict_types=1); use Flextype\Component\Filesystem\Filesystem; -$flextype->emitter->addListener('onEntryAfterInitialized', function() use ($flextype) { +$flextype->emitter->addListener('onEntryAfterInitialized', function () use ($flextype) { $flextype->entries->entry['published_at'] = isset($flextype->entries->entry['published_at']) ? (int) strtotime($flextype->entries->entry['published_at']) : (int) Filesystem::getTimestamp($flextype->entries->getFileLocation($flextype->entries->entry_path)); }); -$flextype->emitter->addListener('onEntryCreate', function() use ($flextype) { +$flextype->emitter->addListener('onEntryCreate', function () use ($flextype) { if (isset($flextype->entries->entry_create_data['published_at'])) { $flextype->entries->entry_create_data['published_at'] = $flextype->entries->entry_create_data['published_at']; } else { diff --git a/src/flextype/Foundation/Entries/Fields/PublishedByField.php b/src/flextype/Foundation/Entries/Fields/PublishedByField.php index 8b82be79..234a6ad9 100644 --- a/src/flextype/Foundation/Entries/Fields/PublishedByField.php +++ b/src/flextype/Foundation/Entries/Fields/PublishedByField.php @@ -7,7 +7,7 @@ declare(strict_types=1); * Founded by Sergey Romanenko and maintained by Flextype Community. */ -$flextype->emitter->addListener('onEntryCreate', function() use ($flextype) { +$flextype->emitter->addListener('onEntryCreate', function () use ($flextype) { if (isset($flextype->entries->entry_create_data['published_by'])) { $flextype->entries->entry_create_data['published_by'] = $flextype->entries->entry_create_data['published_by']; } else { diff --git a/src/flextype/Foundation/Entries/Fields/RoutableField.php b/src/flextype/Foundation/Entries/Fields/RoutableField.php index 246e4ba9..82a911f2 100644 --- a/src/flextype/Foundation/Entries/Fields/RoutableField.php +++ b/src/flextype/Foundation/Entries/Fields/RoutableField.php @@ -9,13 +9,13 @@ declare(strict_types=1); use Flextype\Component\Filesystem\Filesystem; -$flextype->emitter->addListener('onEntryAfterInitialized', function() use ($flextype) { +$flextype->emitter->addListener('onEntryAfterInitialized', function () use ($flextype) { $flextype->entries->entry['routable'] = isset($flextype->entries->entry['routable']) ? (bool) $flextype->entries->entry['routable'] : true; }); -$flextype->emitter->addListener('onEntryCreate', function() use ($flextype) { +$flextype->emitter->addListener('onEntryCreate', function () use ($flextype) { if (isset($flextype->entries->entry_create_data['routable']) && is_bool($flextype->entries->entry_create_data['routable'])) { $flextype->entries->entry_create_data['routable'] = $flextype->entries->entry_create_data['routable']; } else { diff --git a/src/flextype/Foundation/Entries/Fields/SlugField.php b/src/flextype/Foundation/Entries/Fields/SlugField.php index f24dcdd4..a4905efa 100644 --- a/src/flextype/Foundation/Entries/Fields/SlugField.php +++ b/src/flextype/Foundation/Entries/Fields/SlugField.php @@ -9,6 +9,6 @@ declare(strict_types=1); use Flextype\Component\Filesystem\Filesystem; -$flextype->emitter->addListener('onEntryAfterInitialized', function() use ($flextype) { +$flextype->emitter->addListener('onEntryAfterInitialized', function () use ($flextype) { $flextype->entries->entry['slug'] = isset($flextype->entries->entry['slug']) ? (string) $flextype->entries->entry['slug'] : (string) ltrim(rtrim($flextype->entries->entry_path, '/'), '/'); }); diff --git a/src/flextype/Foundation/Entries/Fields/UuidField.php b/src/flextype/Foundation/Entries/Fields/UuidField.php index 029a02d7..889b4901 100644 --- a/src/flextype/Foundation/Entries/Fields/UuidField.php +++ b/src/flextype/Foundation/Entries/Fields/UuidField.php @@ -10,7 +10,7 @@ declare(strict_types=1); use Flextype\Component\Filesystem\Filesystem; use Ramsey\Uuid\Uuid; -$flextype->emitter->addListener('onEntryCreate', function() use ($flextype) { +$flextype->emitter->addListener('onEntryCreate', function () use ($flextype) { if (isset($flextype->entries->entry_create_data['uuid'])) { $flextype->entries->entry_create_data['uuid'] = $flextype->entries->entry_create_data['uuid']; } else { diff --git a/src/flextype/Foundation/Entries/Fields/VisibilityField.php b/src/flextype/Foundation/Entries/Fields/VisibilityField.php index 490c8c50..a5d89f61 100644 --- a/src/flextype/Foundation/Entries/Fields/VisibilityField.php +++ b/src/flextype/Foundation/Entries/Fields/VisibilityField.php @@ -15,7 +15,7 @@ $visibility = [ 'visible' => 'visible', ]; -$flextype->emitter->addListener('onEntryAfterInitialized', function() use ($flextype, $visibility) { +$flextype->emitter->addListener('onEntryAfterInitialized', function () use ($flextype, $visibility) { if (isset($flextype->entries->entry['visibility']) && in_array($flextype->entries->entry['visibility'], $visibility)) { $flextype->entries->entry['visibility'] = (string) $visibility[$flextype->entries->entry['visibility']]; } else { @@ -23,7 +23,7 @@ $flextype->emitter->addListener('onEntryAfterInitialized', function() use ($flex } }); -$flextype->emitter->addListener('onEntryCreate', function() use ($flextype, $visibility) { +$flextype->emitter->addListener('onEntryCreate', function () use ($flextype, $visibility) { if (isset($flextype->entries->entry_create_data['visibility']) && in_array($flextype->entries->entry_create_data['visibility'], $visibility)) { $flextype->entries->entry_create_data['visibility'] = $flextype->entries->entry_create_data['visibility']; } else {