From 5482f51d1a703ffb38e33f82240301cf6df9d41a Mon Sep 17 00:00:00 2001 From: Awilum Date: Sat, 7 Sep 2019 19:44:14 +0300 Subject: [PATCH] feat(core): revert multi data for Entries #212 #186 --- flextype/core/Entries.php | 90 +++++++------------ .../app/Controllers/EntriesController.php | 8 +- .../templates/content/entries/source.html | 1 - 3 files changed, 35 insertions(+), 64 deletions(-) diff --git a/flextype/core/Entries.php b/flextype/core/Entries.php index e08331ee..a6c0a540 100755 --- a/flextype/core/Entries.php +++ b/flextype/core/Entries.php @@ -13,6 +13,7 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Criteria; use Doctrine\Common\Collections\Expr\Comparison; use Flextype\Component\Filesystem\Filesystem; +use function array_replace_recursive; use function ltrim; use function md5; use function rename; @@ -99,14 +100,17 @@ class Entries */ public function fetch(string $id) { + // Get entry file location + $entry_file = $this->_file_location($id); + // If requested entry file founded then process it - if ($_entry = $this->read($id)) { + if (Filesystem::has($entry_file)) { // Create unique entry cache_id // Entry Cache ID = entry + entry file + entry file time stamp - if ($timestamp = Filesystem::getTimestamp($_entry['file_path'])) { - $entry_cache_id = md5('entry' . $_entry['file_path'] . $timestamp); + if ($timestamp = Filesystem::getTimestamp($entry_file)) { + $entry_cache_id = md5('entry' . $entry_file . $timestamp); } else { - $entry_cache_id = md5('entry' . $_entry['file_path']); + $entry_cache_id = md5('entry' . $entry_file); } // Try to get the requested entry from cache @@ -125,15 +129,15 @@ class Entries // else Try to get requested entry from the filesystem } - $entry_decoded = $_entry['file_data']; + $entry_decoded = Parser::decode(Filesystem::read($entry_file), 'frontmatter'); // Add predefined entry items // Entry Date - $entry_decoded['published_at'] = $entry_decoded['published_at'] ? $entry_decoded['published_at'] : Filesystem::getTimestamp($_entry['file_path']); - $entry_decoded['created_at'] = $entry_decoded['created_at'] ? $entry_decoded['created_at'] : Filesystem::getTimestamp($_entry['file_path']); + $entry_decoded['published_at'] = $entry_decoded['published_at'] ? $entry_decoded['published_at'] : Filesystem::getTimestamp($entry_file); + $entry_decoded['created_at'] = $entry_decoded['created_at'] ? $entry_decoded['created_at'] : Filesystem::getTimestamp($entry_file); // Entry Timestamp - $entry_decoded['modified_at'] = Filesystem::getTimestamp($_entry['file_path']); + $entry_decoded['modified_at'] = Filesystem::getTimestamp($entry_file); // Entry Slug $entry_decoded['slug'] = $entry_decoded['slug'] ?? ltrim(rtrim($id, '/'), '/'); @@ -260,17 +264,12 @@ class Entries // Create entries array from entries list and ignore current requested entry //echo count($entries_list); foreach ($entries_list as $current_entry) { - if (strpos($current_entry['path'], $bind_id . '/entry' . '.' . $current_entry['ext']) !== false) { + if (strpos($current_entry['path'], $bind_id . '/entry' . '.' . 'md') !== false) { // ignore ... } else { // We are checking... // Whether the requested entry is a director and whether the file entry is in this directory. - if ($current_entry['type'] === 'dir' && ( - // @todo refactoring this - Filesystem::has($current_entry['path'] . '/entry' . '.' . Parser::$parsers['frontmatter']['ext']) || - Filesystem::has($current_entry['path'] . '/entry' . '.' . Parser::$parsers['json']['ext']) || - Filesystem::has($current_entry['path'] . '/entry' . '.' . Parser::$parsers['yaml']['ext']) - )) { + if ($current_entry['type'] === 'dir' && Filesystem::has($current_entry['path'] . '/entry' . '.' . 'md')) { // Get entry uid // 1. Remove entries path // 2. Remove left and right slashes @@ -371,8 +370,12 @@ class Entries */ public function update(string $id, array $data) : bool { - if ($_entry = $this->read($id)) { - return Filesystem::write($_entry['file_path'], Parser::encode(array_replace_recursive($_entry['file_data'], $data), $_entry['file_parser'])); + $entry_file = $this->_file_location($id); + + if (Filesystem::has($entry_file)) { + $entry = Parser::decode($entry_file, 'frontmatter'); + + return Filesystem::write($entry_file, Parser::encode(array_replace_recursive($entry, $data), 'frontmatter')); } return false; @@ -381,15 +384,14 @@ class Entries /** * Create entry * - * @param string $id Entry ID - * @param array $data Data - * @param string $parser Parser - on of Parser::$parsers + * @param string $id Entry ID + * @param array $data Data * * @return bool True on success, false on failure. * * @access public */ - public function create(string $id, array $data, string $parser = 'frontmatter') : bool + public function create(string $id, array $data) : bool { $entry_dir = $this->_dir_location($id); @@ -398,11 +400,11 @@ class Entries // Try to create directory for new entry if (Filesystem::createDir($entry_dir)) { // Entry file path - $entry_file = $entry_dir . '/entry' . '.' . Parser::$parsers[$parser]['ext']; + $entry_file = $entry_dir . '/entry' . '.' . 'md'; // Check if new entry file exists if (! Filesystem::has($entry_file)) { - return Filesystem::write($entry_file, Parser::encode($data, $parser)); + return Filesystem::write($entry_file, Parser::encode($data, 'frontmatter')); } return false; @@ -433,7 +435,7 @@ class Entries * @param string $new_id New entry id * @param bool $recursive Recursive copy entries. * - * @return bool True on success, false on failure. + * @return bool|null True on success, false on failure. * * @access public */ @@ -453,47 +455,21 @@ class Entries */ public function has(string $id) : bool { - foreach (Parser::$parsers as $parser) { - if (Filesystem::has(PATH['entries'] . '/' . $id . '/' . 'entry' . '.' . $parser['ext'])) { - return true; - } - } - - return false; + return Filesystem::has($this->_file_location($id)); } /** - * Read entry + * Helper method _file_location * - * @param string $id Entry ID - * @param bool $raw Return raw or decoded content + * @param string $id Entry ID * - * @return bool|array Array of file path, decoded file data and file parser, - * false on failure. + * @return string entry directory location * - * @access public + * @access private */ - public function read(string $id, bool $raw = false) + private function _file_location(string $id) : string { - foreach (Parser::$parsers as $parser) { - if (Filesystem::has(PATH['entries'] . '/' . $id . '/' . 'entry' . '.' . $parser['ext'])) { - $file_path = PATH['entries'] . '/' . $id . '/' . 'entry' . '.' . $parser['ext']; - - if ($raw) { - $file_data = Filesystem::read($file_path); - } else { - $file_data = Parser::decode(Filesystem::read($file_path), $parser['name']); - } - - return [ - 'file_path' => $file_path, - 'file_data' => $file_data, - 'file_parser' => $parser['name'], - ]; - } - } - - return false; + return PATH['entries'] . '/' . $id . '/entry.md'; } /** diff --git a/site/plugins/admin/app/Controllers/EntriesController.php b/site/plugins/admin/app/Controllers/EntriesController.php index 07c7831b..78861306 100644 --- a/site/plugins/admin/app/Controllers/EntriesController.php +++ b/site/plugins/admin/app/Controllers/EntriesController.php @@ -606,9 +606,6 @@ class EntriesController extends Controller Arr::delete($entry, 'slug'); Arr::delete($entry, 'modified_at'); - // Read Entry - $_entry = $this->entries->read($this->getEntryID($query), true); - // Fieldsets for current entry template $fieldsets_path = PATH['site'] . '/fieldsets/' . (isset($entry['fieldset']) ? $entry['fieldset'] : 'default') . '.yaml'; $fieldsets = Parser::decode(Filesystem::read($fieldsets_path), 'yaml'); @@ -623,8 +620,7 @@ class EntriesController extends Controller 'i' => count($parts), 'last' => Arr::last($parts), 'id' => $this->getEntryID($query), - 'data' => $_entry['file_data'], - 'parser' => $_entry['file_parser'], + 'data' => Parser::encode($entry, 'frontmatter'), 'type' => $type, 'menu_item' => 'entries', 'links' => [ @@ -761,7 +757,7 @@ class EntriesController extends Controller $data['published_by'] = Session::get('uuid'); // Update entry - if ($this->entries->update($id, Parser::decode($data['data'], $data['parser']))) { + if ($this->entries->update($id, Parser::decode($data['data'], 'frontmatter'))) { $this->flash->addMessage('success', __('admin_message_entry_changes_saved')); } else { $this->flash->addMessage('error', __('admin_message_entry_changes_not_saved')); diff --git a/site/plugins/admin/views/templates/content/entries/source.html b/site/plugins/admin/views/templates/content/entries/source.html index 26a913ba..a3241a4c 100644 --- a/site/plugins/admin/views/templates/content/entries/source.html +++ b/site/plugins/admin/views/templates/content/entries/source.html @@ -5,7 +5,6 @@ {{ csrf() }} -