public createPageFromRoute ( string | null $routeName = null ) : |
||
$routeName | string | null | |
return |
public function createPageFromRoute($routeName = null)
{
if (!$routeName) {
$routeName = $this->pageStack->getRequest()->attributes->get('_route');
if (!$routeName) {
throw new \RuntimeException('Could not detect route name');
}
}
$reflection = new \ReflectionClass($this->router->getGenerator());
$key = 'jarves_routes';
$cache = $this->cacher->getFastCache($key);
$validCache = false;
$routes = [];
if ($cache) {
$validCache = $cache['time'] === filemtime($reflection->getFileName()) && isset($cache['routes']) && is_string($cache['routes']);
if ($validCache) {
$routes = unserialize($cache['routes']);
}
}
if (!$validCache) {
$routes = $this->router->getRouteCollection()->all();
$this->cacher->setFastCache($key, ['time' => filemtime($reflection->getFileName()), 'routes' => serialize($routes)]);
}
if (!isset($routes[$routeName])) {
throw new \RuntimeException("Route with name `{$routeName}` does not exist");
}
$route = $routes[$routeName];
$url = $this->router->generate($routeName, $this->pageStack->getRequest()->attributes->all());
$page = Node::createPage($route->getOption('title'), parse_url($url)['path'], $route->getOption('theme'), $route->getOption('layout'));
if ($route->getOption('meta')) {
foreach ((array) $route->getOption('meta') as $key => $value) {
$page->meta->set($key, $value);
}
}
return $page;
}