From 4715e864ef5f4bd5ca33a65ba2f50dcf3b9a821e Mon Sep 17 00:00:00 2001 From: Awilum Date: Fri, 27 Apr 2018 21:28:57 +0300 Subject: [PATCH] Updates for event listener and templates loader --- flextype/Pages.php | 19 +++++++++++-------- flextype/Plugins.php | 4 ++-- flextype/Themes.php | 25 ++++++++++++++++++++++++- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/flextype/Pages.php b/flextype/Pages.php index ebba9735..32c35e52 100755 --- a/flextype/Pages.php +++ b/flextype/Pages.php @@ -12,7 +12,7 @@ namespace Flextype; -use Flextype\Component\{Arr\Arr, Http\Http, Filesystem\Filesystem}; +use Flextype\Component\{Arr\Arr, Http\Http, Filesystem\Filesystem, Event\Event}; use Symfony\Component\Yaml\Yaml; class Pages @@ -50,7 +50,10 @@ class Pages protected static function init() : void { // The page is not processed and not sent to the display. - Events::dispatch('onPageBeforeRender'); + Event::dispatch('onPageBeforeRender'); + + // Add parseContent on content event + Event::addListener('content', 'Flextype\Pages::parseContent'); // Get current page static::$page = static::getPage(Http::getUriString()); @@ -59,7 +62,7 @@ class Pages static::renderPage(static::$page); // The page has been fully processed and sent to the display. - Events::dispatch('onPageAfterRender'); + Event::dispatch('onPageAfterRender'); } /** @@ -101,7 +104,7 @@ class Pages */ public static function renderPage(array $page) { - View::factory(empty($page['template']) ? 'default' : $page['template']) + Themes::template(empty($page['template']) ? 'default' : $page['template']) ->assign('page', $page, true) ->display(); } @@ -157,12 +160,12 @@ class Pages if ($raw) { $page = trim(Filesystem::getFileContent($file)); static::$page = $page; - Events::dispatch('onPageContentRawAfter'); + Event::dispatch('onPageContentRawAfter'); } else { $page = static::parseFile($file); static::$page = $page; - static::$page['content'] = Filters::dispatch('content', static::parseContent(static::$page['content'])); - Events::dispatch('onPageContentAfter'); + static::$page['content'] = Event::dispatch('content', ['content' => static::$page['content']], true); + Event::dispatch('onPageContentAfter'); } return static::$page; @@ -220,7 +223,7 @@ class Pages // Sort and Slice pages if $raw === false if (!$raw) { - $pages = Arr::subvalSort($pages, $order_by, $order_type); + $pages = Arr::sort($pages, $order_by, $order_type); if ($offset !== null && $length !== null) { $pages = array_slice($pages, $offset, $length); diff --git a/flextype/Plugins.php b/flextype/Plugins.php index 76bd0034..b38ba1a4 100755 --- a/flextype/Plugins.php +++ b/flextype/Plugins.php @@ -12,7 +12,7 @@ namespace Flextype; -use Flextype\Component\Filesystem\Filesystem; +use Flextype\Component\{Filesystem\Filesystem, Event\Event}; use Symfony\Component\Yaml\Yaml; class Plugins @@ -97,7 +97,7 @@ class Plugins } } - Events::dispatch('onPluginsInitialized'); + Event::dispatch('onPluginsInitialized'); } /** diff --git a/flextype/Themes.php b/flextype/Themes.php index b78a51ad..fbcd9e96 100644 --- a/flextype/Themes.php +++ b/flextype/Themes.php @@ -12,7 +12,7 @@ namespace Flextype; -use Flextype\Component\Filesystem\Filesystem; +use Flextype\Component\{Filesystem\Filesystem, View\View}; use Symfony\Component\Yaml\Yaml; class Themes @@ -65,6 +65,29 @@ class Themes } } + /** + * Return the Themes instance. + * Create it if it's not already created. + * + * @param string $template Template file + * @param string $variables Variables + * @access public + * @return object + */ + public static function template(string $template, array $variables = []) + { + // Set view file + // From current theme folder or from plugin folder + if (Filesystem::fileExists(THEMES_PATH . '/' . Config::get('site.theme') . '/templates/' . $template . View::$view_ext)) { + $template = THEMES_PATH . '/' . Config::get('site.theme') . '/templates/' . $template; + } else { + $template = PLUGINS_PATH . '/' . $template; + } + + // Return template + return new View($template, $variables); + } + /** * Return the Themes instance. * Create it if it's not already created.