From 02819f97e82ff0689b8fe848d8457a49fa47cd16 Mon Sep 17 00:00:00 2001 From: Awilum Date: Fri, 29 Nov 2019 16:57:58 +0300 Subject: [PATCH] refactor(core): refactor entries fetch methods naming #315 We have: fetch - for single and collection entries request. fetchSingle - for single entry request. fetchCollection - for collection entries request. Important: fetchAll - should be removed. BREAKING CHANGES method fetchAll removed! please use fetch, fetchSingle or fetchCollection. --- flextype/core/Entries.php | 37 +++++++++++++++---- flextype/twig/EntriesTwigExtension.php | 16 ++++++-- .../app/Controllers/EntriesController.php | 6 +-- .../app/Controllers/SettingsController.php | 2 +- .../templates/content/entries/index.html | 2 +- .../default/templates/partials/base.html | 2 +- 6 files changed, 48 insertions(+), 17 deletions(-) diff --git a/flextype/core/Entries.php b/flextype/core/Entries.php index 00705b32..cd9276f1 100755 --- a/flextype/core/Entries.php +++ b/flextype/core/Entries.php @@ -104,16 +104,36 @@ class Entries $this->flextype = $flextype; } + /** + * Fetch entry(enries) + * + * @param string $id Entry ID + * @param array|null $args Query arguments. + * + * @return array The entry array data. + * + * @access public + */ + public function fetch(string $id, $args = null) : array + { + // If args is array then it is entries collection request + if (is_array($args)) { + return $this->fetchCollection($id, $args); + } else { + return $this->fetchSingle($id); + } + } + /** * Fetch single entry * * @param string $id Entry ID * - * @return array|false The entry array data or false on failure. + * @return array The entry array data. * * @access public */ - public function fetch(string $id) + public function fetchSingle(string $id) : array { // Get entry file location $entry_file = $this->getFileLocation($id); @@ -139,7 +159,8 @@ class Entries return $entry; } - return false; + // Return empty array + return []; // else Try to get requested entry from the filesystem } @@ -170,19 +191,21 @@ class Entries return $this->entry; } - return false; + // Return empty array + return []; } /** * Fetch entries collection * - * @param array $args Query arguments + * @param string $id Entry ID + * @param array $args Query arguments. * - * @return array The entries array data + * @return array The entries array data. * * @access public */ - public function fetchAll(string $id, array $args = []) : array + public function fetchCollection(string $id, array $args = []) : array { // Init Entries $entries = []; diff --git a/flextype/twig/EntriesTwigExtension.php b/flextype/twig/EntriesTwigExtension.php index 51ccad82..384e426c 100644 --- a/flextype/twig/EntriesTwigExtension.php +++ b/flextype/twig/EntriesTwigExtension.php @@ -53,19 +53,27 @@ class EntriesTwig $this->flextype = $flextype; } + /** + * Fetch entry(entries) + */ + public function fetch(string $id, $args = null) : array + { + return $this->flextype['entries']->fetch($id, $args); + } + /** * Fetch single entry */ - public function fetch(string $id) + public function fetchSingle(string $id) : array { return $this->flextype['entries']->fetch($id); } /** - * Fetch all entries + * Fetch entries collection */ - public function fetchAll(string $id, array $args = []) : array + public function fetchCollection(string $id, array $args = []) : array { - return $this->flextype['entries']->fetchAll($id, $args); + return $this->flextype['entries']->fetch($id, $args); } } diff --git a/site/plugins/admin/app/Controllers/EntriesController.php b/site/plugins/admin/app/Controllers/EntriesController.php index 0a854a61..ecb5e481 100644 --- a/site/plugins/admin/app/Controllers/EntriesController.php +++ b/site/plugins/admin/app/Controllers/EntriesController.php @@ -73,7 +73,7 @@ class EntriesController extends Controller $response, 'plugins/admin/views/templates/content/entries/index.html', [ - 'entries_list' => $this->entries->fetchAll($this->getEntryID($query), ['order_by' => ['field' => 'published_at', 'direction' => 'desc']]), + 'entries_list' => $this->entries->fetch($this->getEntryID($query), ['order_by' => ['field' => 'published_at', 'direction' => 'desc']]), 'id_current' => $this->getEntryID($query), 'menu_item' => 'entries', 'parts' => $parts, @@ -144,7 +144,7 @@ class EntriesController extends Controller $response, 'plugins/admin/views/templates/content/entries/add.html', [ - 'entries_list' => $this->entries->fetchAll($this->getEntryID($query), ['order_by' => ['field' => 'title', 'direction' => 'asc']]), + 'entries_list' => $this->entries->fetch($this->getEntryID($query), ['order_by' => ['field' => 'title', 'direction' => 'asc']]), 'menu_item' => 'entries', 'fieldsets' => $fieldsets, 'current_id' => $this->getEntryID($query), @@ -399,7 +399,7 @@ class EntriesController extends Controller // Get entries list $entries_list['/'] = '/'; - foreach ($this->entries->fetchAll('', ['order_by' => ['field' => ['slug']], 'recursive' => true]) as $_entry) { + foreach ($this->entries->fetch('', ['order_by' => ['field' => ['slug']], 'recursive' => true]) as $_entry) { if ($_entry['slug'] != '') { $entries_list[$_entry['slug']] = $_entry['slug']; } else { diff --git a/site/plugins/admin/app/Controllers/SettingsController.php b/site/plugins/admin/app/Controllers/SettingsController.php index 8c59d055..49196e95 100644 --- a/site/plugins/admin/app/Controllers/SettingsController.php +++ b/site/plugins/admin/app/Controllers/SettingsController.php @@ -32,7 +32,7 @@ class SettingsController extends Controller public function index(/** @scrutinizer ignore-unused */ Request $request, Response $response) : Response { $entries = []; - foreach ($this->entries->fetchAll('', ['order_by' => ['field' => 'published_at', 'direction' => 'desc']]) as $entry) { + foreach ($this->entries->fetch('', ['order_by' => ['field' => 'published_at', 'direction' => 'desc']]) as $entry) { $entries[$entry['slug']] = $entry['title']; } diff --git a/site/plugins/admin/views/templates/content/entries/index.html b/site/plugins/admin/views/templates/content/entries/index.html index 0e813c2f..2366965e 100644 --- a/site/plugins/admin/views/templates/content/entries/index.html +++ b/site/plugins/admin/views/templates/content/entries/index.html @@ -27,7 +27,7 @@ {% if cache.contains(entry.slug ~ '_counter') %} {% set _entries_length = cache.fetch(entry.slug ~ '_counter') %} {% else %} - {% set _entries = entries.fetchAll(entry.slug) %} + {% set _entries = entries.fetch(entry.slug, {}) %} {% set _entries_length = _entries|length %} {% do cache.save(entry.slug ~ '_counter', _entries_length) %} {% endif %} diff --git a/site/themes/default/templates/partials/base.html b/site/themes/default/templates/partials/base.html index aeaa6469..cc52bd01 100644 --- a/site/themes/default/templates/partials/base.html +++ b/site/themes/default/templates/partials/base.html @@ -36,7 +36,7 @@