Skip to content

Commit

Permalink
Move to Site the selection of current page
Browse files Browse the repository at this point in the history
  • Loading branch information
giuscris committed Apr 20, 2019
1 parent 0e80beb commit 7c63eba
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 31 deletions.
26 changes: 11 additions & 15 deletions formwork/Core/Formwork.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,6 @@ class Formwork
*/
protected $cacheKey;

/**
* Current resource
*
* @var mixed
*/
protected $resource;

/**
* Create a new Formwork instance
*/
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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()) {
Expand All @@ -228,7 +224,7 @@ private function defaultRoute()
}
}

return $this->site->errorPage(true);
return $this->site->errorPage();
};
}

Expand Down
35 changes: 19 additions & 16 deletions formwork/Core/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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;
}
Expand Down

0 comments on commit 7c63eba

Please sign in to comment.