vendor\klio\klio-bundle\src\Symfony\Controller.php line 92
<?phpnamespace Klio\KlioBundle\Symfony;use Klio\KlioBundle\Debug\Error;use Klio\KlioBundle\Security\Sanitizer;/*** Cette classe sert uniquement à isoler le Controller.php include dans un espace cadré et borné*/class Controller{private string|false $template = false; // le template twig si on a pas envoyé le code via $responseprivate int $status = 0; // le statut de la réponse, 200, 404, etc...private array $js = []; // l'injection dans le twig de Scrips.js dans le dossier via webpackprivate array $css = [];private string $buffer = "";private array $controllers = []; // les fichiers controller .php avec chemin depuis le root /private/test/TestController.phpprivate string $controller = ""; // le fichier controller actifprivate array $twig = [];private bool $routeFound = false; // la route a été trouvée ou paspublic bool $routeCancel = false; // si l'URL n'est pas propre, on annule l'appel à la routeprivate string $controllerPath = ""; // en fonciton du controller, on determine la route qui va avec// response est en dernier pour qu'il soit en bas dans le dumpprivate string $response = ""; // le code html à envoyer au clientprivate Sanitizer $sanitizer;private object $privateController;public function __construct(){$this->sanitizer = new Sanitizer();}/** Renvoie le controller par défaut à partir du path* @return string Le fichier Controller.php à include par défaut dans l'objet COntroller*/private function getDefaultController(): string{return __PATH__ == '/' ? '' . "/Controller.php" : strtolower(__PATH__) . "/Controller.php";}/*** Affectation d'un controller** @return string*/public function setController(string $controllerFile): string{if (!$this->controllers) $this->controllers[] = $controllerFile;$this->controller = $controllerFile;$this->controllerPath = dirname($controllerFile);return $this->controller;}/*** Include du fichier php controller dans private* Défini deux éléments essentiels : ->response et ->status** @return bool|int*/public function includePrivateController(){// si le controller est défini de force ou avant, on l'utiliseif ($this->getController()) {$this->setRouteFound(@include_once(__ROOT__ . "/private" . $this->getController()));}// sinon, on teste les controllers un par unelse {if (!$this->controllers) {$this->controllers[] = $this->getDefaultController();}// on essaye d'include tous les controller dans l'ordreforeach ($this->controllers as $controller) {// on affecte la nouvelle route du nouveau controller$this->setRouteFound(@include_once(__ROOT__ . "/private" . $controller));if ($this->getRouteFound()) {$this->setController($controller);break;}}}// si le fichier php du controller dans le chemin private a été trouvé, on créé l'objet PrivateController à partir de ce fichier php.if ($this->getRouteFound()) {// la classe PrivateController est un objet qui est construit dans chaque route, il permet d'accèder à l'objet Controller tout en instanciant une classe privée à chaque fois$this->privateController = new \PrivateController($this);$this->privateController->render($this);// on ajoute par défaut la session authif (isset($_SESSION['auth'])) $this->addTwig('auth', $_SESSION['auth']);if (isset($_SESSION['twig'])) $this->addTwig('twig', $_SESSION['twig']);}return $this->getRouteFound();}/*** Ajoute d'un controller a tester** @return array*/public function addController(string $controllerFile): array{if (!$this->controllers) {$this->controllers[] = $this->getDefaultController();}$this->controllers[] = $controllerFile;return $this->controllers;}/** La liste des controllers** @return array*/public function getControllers(): array{return $this->controllers;}/** on cherche le controller actif** @return string*/public function getController(): string{return $this->controller;}/*** renvoi $controller->controllerPath** @return string*/public function getControllerPath(): string{if ($this->controllerPath) return $this->controllerPath;else return dirname($this->controller);}/*** Getter : le code brut (html, json, xml...) à renvoyer au client** @return string*/public function getResponse(): string{return $this->response;}/*** Setter : le code brut (html, json, xml...) qui va être renvoyé au client** @param string $response le code brut (html, json, xml...) qui va être renvoyé** @return string*/public function setResponse(string $response, int $status = 0): string{if ($response and !$this->getStatus() and !$status) $this->setStatus(200);if ($status) $this->setStatus($status);$this->response = $response;return $this->response;}/*** Get the value of status** @return int*/public function getStatus(): int{if (!$this->getResponse() and !$this->getTemplate()) return 0;if (($this->getResponse() or $this->getTemplate()) and !$this->status) return 200;return $this->status;}/*** Set the value of status** @param int $status** @return self*/public function setStatus(int $status): int{$this->status = $status;return $this->status;}/*** Get the value of template** @return string|false*/public function getTemplate(): string|false{return $this->template;}/*** Set the value of template** @param string $template** @return string*/public function setTemplate(string|false $templateFile): string|false{if ($templateFile and $this->response) new Error(["msg" => "Erreur Controller->setTemplate","debug" => "Impossible de régler un template si la route renvoie déjà une réponse.","datas" => $this]);if ($templateFile and !$this->getStatus()) $this->setStatus(200);$this->template = $templateFile;return $this->template;}/*** Get the value of js** @return array*/public function getJs(): array{return $this->js;}/*** Set the value of js** @param array $js** @return self*/public function setJs(array $js): array{$this->js = $js;return $this->js;}/*** Add values to css** @param array $css** @return self*/public function addJs(array|string $js): array{if (is_array($js)) {foreach ($js as $file) {$this->js[] = $file;}} else $this->js[] = $js;return $this->js;}/*** Get the value of css** @return array*/public function getCss(): array{return $this->css;}/*** Set the value of css** @param array $css** @return self*/public function setCss(array $css): array{$this->css = $css;return $this->css;}/*** Add values to css** @param array $css** @return self*/public function addCss(array|string $css): array{if (is_array($css)) {foreach ($css as $file) {$this->css[] = $file;}} else $this->css[] = $css;return $this->css;}/*** Get the value of buffer** @return string*/public function getBuffer(): string{return $this->buffer;}/*** Set the value of buffer** @param string $buffer** @return self*/public function setBuffer(string $buffer): string{$this->buffer = $buffer;return $this->buffer;}/*** Get the value of twig** @return array|string*/public function getTwig(string $key = ""): array|string{if (!$key) return $this->twig;else return $this->twig[$key];}/*** Affecter un tableau à ->twig, toute la liste des keys/values en une seule fois** @param array $keysvals un tableau clés/valeurs avec toutes les varaibles twig** @return self*/public function setTwig(array|string $keysvals, string|array $value = ""): array{$this->twig = [];$this->addTwig($keysvals, $value);return $this->twig;}/*** ajoute une variable twig ou un tableau de variables sanitizées** @param string $key la clé du tableau* @param string $val la valeur de la clé* @param string $encodeEntities forcer l'encodage des htmlentities** @return self*/public function addTwig(array|string $keysvals, string|array $value = "", $encodeEntities = false): array{if (is_array($keysvals)) {foreach ($keysvals as $key => $val) {$this->twig[$key] = $this->sanitizer->sanitize($val, $encodeEntities);}} else $this->twig[$keysvals] = $this->sanitizer->sanitize($value, $encodeEntities);return $this->twig;}/*** ajoute une variable twig ou un tableau de variables de confiance non sanitizées** @param string $key la clé du tableau* @param string $val la valeur de la clé* @param string $encodeEntities forcer l'encodage des htmlentities** @return self*/public function addTrustedTwig(array|string $keysvals, string|array $value = "", $encodeEntities = false): array{if (is_array($keysvals)) {foreach ($keysvals as $key => $val) {$this->twig[$key] = $val;}} else $this->twig[$keysvals] = $value;return $this->twig;}/*** retirer une clé du tableau twig** @param string $key** @return self*/public function removeTwig(string $key): array{unset($this->twig[$key]);return $this->twig;}/*** Get the value of routeFound** @return bool*/public function getRouteFound(): bool{return $this->routeFound;}/*** Set the value of routeFound** @param bool $routeFound** @return void*/public function setRouteFound(bool $routeFound): void{$this->routeFound = $routeFound;}}