public function __construct(\Niysu\Server $server, \Monolog\Logger $log = null) { // creating the loader $this->filesystemLoader = new \Twig_Loader_Filesystem([]); $loader = new \Twig_Loader_Chain([$this->filesystemLoader, new \Twig_Loader_String()]); // creating twig $this->twig = new \Twig_Environment($loader, []); // the "path" function // TODO: log when something wrong happens $pathFunction = function ($name, $params = []) use($server, $log) { $route = $server->getRouteByName($name); if (!$route) { $log->err('Unable to find route named ' . $name . ' in Twig template'); return ''; } if (!isset($params) || !is_array($params)) { $params = []; } try { return $route->getURL($params); } catch (\Exception $e) { $log->err('Unable to build route URL for ' . $name . ' in Twig template', ['params' => $params]); return ''; } }; // registering functions $this->twig->addFunction(new \Twig_SimpleFunction('path', $pathFunction)); $this->twig->addFunction(new \Twig_SimpleFunction('url', $pathFunction)); }
/** * @url /routes/{routeID} * @pattern routeID .* * @method GET */ public function routeAnalysis($routeID, \Niysu\Server $server, $twigOutput) { $route = null; if (is_numeric($routeID)) { $route = $server->getRoutesList()[$routeID]; } if (!$route) { try { $route = $server->getRouteByName($routeID); } catch (\Exception $e) { } } $twigOutput->setTemplate('@niysuAdminSite/routeAnalysis.htm'); $twigOutput->setVariables(['route' => $route]); }