/** * routeLoader, lance le chargement d'une route. * * @param string $method La methode HTTP * @param string $path La route à mapper * @param Callable|string|array $name Le nom de la route ou la fonction à lancer. * @param callable $cb La fonction à lancer * * @return Application */ private function routeLoader($method, $path, $name, callable $cb = null) { if (!preg_match('@^/@', $path)) { $path = '/' . $path; } // construction du path original en fonction de la configuration de l'application $path = $this->config->getApproot() . $this->branch . $path; if (is_callable($name)) { $cb = $name; $name = null; } if (is_array($name)) { $cb = $name; $name = null; if (isset($cb['name'])) { $name = $cb['name']; unset($cb['name']); } } if (is_string($name)) { if (!preg_match('/^[a-z]+(\\.|@)[a-z]+$/i', $name)) { $this->namedRoute($path, $name); } else { $cb = $name; $name = null; } } // Ajout d'un nouvelle route sur l'en definie. static::$routes[$method][] = new Route($path, $cb); // route courante $this->currentPath = $path; // methode courante $this->currentMethod = $method; return $this; }
/** * templateLoader, charge le moteur template à utiliser. * * @throws ErrorException * @return \Twig_Environment|\Mustache_Engine|\Jade\Jade */ private function templateLoader() { if ($this->config->getTemplateEngine() !== null) { if (!in_array($this->config->getTemplateEngine(), ['twig', 'mustache', 'jade'], true)) { throw new ErrorException('Le moteur de template n\'est pas implementé.', E_USER_ERROR); } } else { throw new ResponseException('Le moteur de template non défini.', E_USER_ERROR); } $tpl = null; if ($this->config->getTemplateEngine() == 'twig') { $loader = new \Twig_Loader_Filesystem($this->config->getViewpath()); $tpl = new \Twig_Environment($loader, ['cache' => $this->config->getCachepath(), 'auto_reload' => $this->config->getCacheAutoReload(), 'debug' => $this->config->getLoggerMode() == 'develepment' ? true : false]); /** * - Ajout de variable globale * dans le cadre de l'utilisation de Twig */ $tpl->addGlobal('public', $this->config->getPublicPath()); $tpl->addGlobal('root', $this->config->getApproot()); /** * - Ajout de fonction global * dans le cadre de l'utilisation de Twig */ $tpl->addFunction(new \Twig_SimpleFunction('secure', function ($data) { return Security::sanitaze($data, true); })); $tpl->addFunction(new \Twig_SimpleFunction('sanitaze', function ($data) { return Security::sanitaze($data); })); $tpl->addFunction(new \Twig_SimpleFunction('csrf_field', function () { return Security::getCsrfToken()->field; })); $tpl->addFunction(new \Twig_SimpleFunction('csrf_token', function () { return Security::getCsrfToken()->token; })); $tpl->addFunction(new \Twig_SimpleFunction('slugify', [Str::class, 'slugify'])); return $tpl; } if ($this->config->getTemplateEngine() == 'mustache') { return new \Mustache_Engine(['cache' => $this->config->getCachepath(), 'loader' => new \Mustache_Loader_FilesystemLoader($this->config->getViewpath()), 'helpers' => ['secure' => function ($data) { return Security::sanitaze($data, true); }, 'sanitaze' => function ($data) { return Security::sanitaze($data); }, 'slugify' => function ($data) { return Str::slugify($data); }, 'csrf_token' => function () { return Security::getCsrfToken()->token; }, 'csrf_field' => function () { return Security::getCsrfToken()->field; }, 'public', $this->config->getPublicPath(), 'root', $this->config->getApproot()]]); } return new Jade(['cache' => $this->config->getCachepath(), 'prettyprint' => true, 'extension' => $this->config->getTemplateExtension()]); }