createPage() public static method

public static createPage ( string $title, string $urn, string | null $theme = null, string | null $layout = null ) : Node
$title string
$urn string
$theme string | null
$layout string | null
return Node
Esempio n. 1
0
 /**
  * Creates a Node object based on given $routeName or current route.
  *
  * @param string|null $routeName
  *
  * @return Node
  */
 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;
 }