From 7c63eba2b3888e5424684a9b8a1f292f08add3b1 Mon Sep 17 00:00:00 2001 From: Giuseppe Criscione Date: Sat, 20 Apr 2019 19:55:20 +0200 Subject: [PATCH] Move to `Site` the selection of current page --- formwork/Core/Formwork.php | 26 +++++++++++--------------- formwork/Core/Site.php | 35 +++++++++++++++++++---------------- 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/formwork/Core/Formwork.php b/formwork/Core/Formwork.php index 0763dd3cd..248e9daba 100755 --- a/formwork/Core/Formwork.php +++ b/formwork/Core/Formwork.php @@ -63,13 +63,6 @@ class Formwork */ protected $cacheKey; - /** - * Current resource - * - * @var mixed - */ - protected $resource; - /** * Create a new Formwork instance */ @@ -171,13 +164,16 @@ public function run() '/{page}/' ), $this->defaultRoute()); - $this->resource = $this->router->dispatch(); + $resource = $this->router->dispatch(); - if ($this->resource instanceof Page) { - $data = $this->resource->render(); - if ($this->option('cache.enabled') && $this->resource->get('cacheable')) { - $this->cache->save($this->cacheKey, $data); - } + if (is_null($this->site->currentPage())) { + $this->site->setCurrentPage($resource); + } + + $data = $this->site->currentPage()->render(); + + if ($this->option('cache.enabled') && $resource->get('cacheable')) { + $this->cache->save($this->cacheKey, $data); } } @@ -209,7 +205,7 @@ private function defaultRoute() if ($page = $this->site->findPage($route)) { if ($params->has('tagName') || $params->has('paginationPage')) { if ($page->template()->scheme()->get('type') !== 'listing') { - return $this->site->errorPage(true); + return $this->site->errorPage(); } } if ($page->routable() && $page->published()) { @@ -228,7 +224,7 @@ private function defaultRoute() } } - return $this->site->errorPage(true); + return $this->site->errorPage(); }; } diff --git a/formwork/Core/Site.php b/formwork/Core/Site.php index 2ede52c92..c738d1be3 100755 --- a/formwork/Core/Site.php +++ b/formwork/Core/Site.php @@ -136,20 +136,25 @@ public function alias($route) } /** - * Get site current page + * Set and return site current page * - * @return Page|null + * @return Page */ - public function currentPage() + public function setCurrentPage(Page $page) { - if (!empty($this->currentPage)) { - return $this->currentPage; - } - $resource = Formwork::instance()->resource(); - if ($resource instanceof Page) { - return $this->currentPage = $resource; - } - return null; + return $this->currentPage = $page; + } + + /** + * Navigate to and return a page from its route, setting then the current page + * + * @param string $route + * + * @return Page + */ + public function navigate($route) + { + return $this->currentPage = $this->findPage($route); } /** @@ -165,17 +170,15 @@ public function indexPage() /** * Return or render site error page * - * @param bool $render Whether to render error page or not + * @param bool $navigate Whether to navigate to the error page or not * * @return Page|null */ - public function errorPage($render = false) + public function errorPage($navigate = false) { $errorPage = $this->findPage(Formwork::instance()->option('pages.error')); - if ($render) { + if ($navigate) { $this->currentPage = $errorPage; - $errorPage->render(); - exit; } return $errorPage; }