From 4771572b6729387d9ed0eb21d99f93fe29bc7b07 Mon Sep 17 00:00:00 2001 From: Awilum Date: Wed, 16 May 2018 02:58:39 +0300 Subject: [PATCH] Content: general refactoring - next round --- flextype/Content.php | 57 ++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 34 deletions(-) diff --git a/flextype/Content.php b/flextype/Content.php index 1bd67d36..6c9680c9 100755 --- a/flextype/Content.php +++ b/flextype/Content.php @@ -106,39 +106,18 @@ class Content } /** - * Method for searhing pages in the system + * Update current page * - * @access public - * @param string $url - * @param bool $url_abs - * @return string + * Content::updateCurrentPage('title', 'New page title'); + * + * @access public + * @param string $path Array path + * @param mixed $value Value to set + * @return void */ - public static function pageFinder(string $url = '', bool $url_abs = false) : string + public static function updateCurrentPage(string $path, $value) : void { - // If url is empty that its a homepage - if ($url_abs) { - if ($url) { - $file = $url; - } else { - $file = PATH['pages'] . '/' . Registry::get('site.pages.main') . '/' . 'page.md'; - } - } else { - if ($url) { - $file = PATH['pages'] . '/' . $url . '/page.md'; - } else { - $file = PATH['pages'] . '/' . Registry::get('site.pages.main') . '/' . 'page.md'; - } - } - - // Get 404 page if file not exists - if (Filesystem::fileExists($file)) { - $file = $file; - } else { - $file = PATH['pages'] . '/404/page.md'; - Http::setResponseStatus(404); - } - - return $file; + Arr::set(Content::$page, $path, $value); } /** @@ -172,6 +151,16 @@ class Content } else { Content::$page = Content::processPage($file_path); Event::dispatch('onPageContentAfter'); + + // Get 404 page if page is not published + if (isset(Content::$page['published']) && Content::$page['published'] === false) { + if (Filesystem::fileExists($file_path = PATH['pages'] . '/404/page.md')) { + Content::$page = Content::processPage($file_path); + Http::setResponseStatus(404); + } else { + throw new \RuntimeException("404 page file does not exist."); + } + } } return Content::$page; @@ -187,14 +176,14 @@ class Content // Get pages for $url // If $url is empty then we want to have a list of pages for /pages dir. - if ($url == '') { + if ($url === '') { // Get pages list $pages_list = Filesystem::getFilesList(PATH['pages'] . '/' , 'md'); // Create pages array from pages list foreach ($pages_list as $key => $page) { - $pages[$key] = static::getPage($page, $raw, true); + $pages[$key] = Content::processPage($page, $raw); } } else { @@ -204,10 +193,10 @@ class Content // Create pages array from pages list and ignore current requested page foreach ($pages_list as $key => $page) { - if (strpos($page, $url.'/page.md') !== false) { + if (strpos($page, $url . '/page.md') !== false) { // ignore ... } else { - $pages[$key] = static::getPage($page, $raw, true); + $pages[$key] = Content::processPage($page, $raw); } }