vendor\klio\klio-bundle\src\Symfony\Controller.php line 64

  1. <?php
  2. namespace Klio\KlioBundle\Symfony;
  3. use Klio\KlioBundle\Debug\Error;
  4. use Klio\KlioBundle\Security\Sanitizer;
  5. /**
  6.  * Cette classe sert uniquement à isoler le Controller.php include dans un espace cadré et borné 
  7.  */
  8. class Controller
  9. {
  10.     private string|false $template false// le template twig si on a pas envoyé le code via $response 
  11.     private int $status 0// le statut de la réponse, 200, 404, etc...
  12.     private array $js = []; // l'injection dans le twig de Scrips.js dans le dossier via webpack
  13.     private array  $css = [];
  14.     private string $buffer "";
  15.     private array $controllers = []; // les fichiers controller .php avec chemin depuis le root /private/test/TestController.php
  16.     private string $controller ""// le fichier controller actif
  17.     private array $twig = [];
  18.     private bool $routeFound false// la route a été trouvée ou pas
  19.     public bool $routeCancel false// si l'URL n'est pas propre, on annule l'appel à la route
  20.     private string $controllerPath ""// en fonciton du controller, on determine la route qui va avec
  21.     // response est en dernier pour qu'il soit en bas dans le dump
  22.     private string $response ""// le code html à envoyer au client
  23.     private Sanitizer $sanitizer;
  24.     private object $privateController;
  25.     public function __construct()
  26.     {
  27.         $this->sanitizer = new Sanitizer();
  28.     }
  29.     /** Renvoie le controller par défaut à partir du path
  30.      *  @return string Le fichier Controller.php à include par défaut dans l'objet COntroller
  31.      */
  32.     private function getDefaultController(): string
  33.     {
  34.         return __PATH__ == '/' '' "/Controller.php" strtolower(__PATH__) . "/Controller.php";
  35.     }
  36.     /**
  37.      * Affectation d'un controller
  38.      *
  39.      * @return string
  40.      */
  41.     public function setController(string $controllerFile): string
  42.     {
  43.         if (!$this->controllers$this->controllers[] = $controllerFile;
  44.         $this->controller $controllerFile;
  45.         $this->controllerPath dirname($controllerFile);
  46.         return $this->controller;
  47.     }
  48.     /**
  49.      * Include du fichier php controller dans private
  50.      * Défini deux éléments essentiels : ->response et ->status
  51.      *
  52.      * @return bool|int
  53.      */
  54.     public function includePrivateController()
  55.     {
  56.         // si le controller est défini de force ou avant, on l'utilise 
  57.         if ($this->getController()) {
  58.             $this->setRouteFound(@include_once(__ROOT__ "/private" $this->getController()));
  59.         }
  60.         // sinon, on teste les controllers un par un
  61.         else {
  62.             if (!$this->controllers) {
  63.                 $this->controllers[] = $this->getDefaultController();
  64.             }
  65.             // on essaye d'include tous les controller dans l'ordre
  66.             foreach ($this->controllers as $controller) {
  67.                 // on affecte la nouvelle route du nouveau controller
  68.                 $this->setRouteFound(@include_once(__ROOT__ "/private" $controller));
  69.                 if ($this->getRouteFound()) {
  70.                     $this->setController($controller);
  71.                     break;
  72.                 }
  73.             }
  74.         }
  75.         // si le fichier php du controller dans le chemin private a été trouvé, on créé l'objet PrivateController à partir de ce fichier php.
  76.         if ($this->getRouteFound()) {
  77.             // 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
  78.             $this->privateController = new \PrivateController($this);
  79.             $this->privateController->render($this);
  80.             // on ajoute par défaut la session auth
  81.             if (isset($_SESSION['auth'])) $this->addTwig('auth'$_SESSION['auth']);
  82.             if (isset($_SESSION['twig'])) $this->addTwig('twig'$_SESSION['twig']);
  83.         }
  84.         return $this->getRouteFound();
  85.     }
  86.     /**
  87.      * Ajoute d'un controller a tester
  88.      *
  89.      * @return array
  90.      */
  91.     public function addController(string $controllerFile): array
  92.     {
  93.         if (!$this->controllers) {
  94.             $this->controllers[] = $this->getDefaultController();
  95.         }
  96.         $this->controllers[] = $controllerFile;
  97.         return $this->controllers;
  98.     }
  99.     /*
  100.      * La liste des controllers
  101.      *
  102.      * @return array
  103.      */
  104.     public function getControllers(): array
  105.     {
  106.         return $this->controllers;
  107.     }
  108.     /*
  109.      * on cherche le controller actif
  110.      *
  111.      * @return string
  112.      */
  113.     public function getController(): string
  114.     {
  115.         return $this->controller;
  116.     }
  117.     /**
  118.      * renvoi $controller->controllerPath 
  119.      *
  120.      * @return string
  121.      */
  122.     public function getControllerPath(): string
  123.     {
  124.         if ($this->controllerPath) return $this->controllerPath;
  125.         else return dirname($this->controller);
  126.     }
  127.     /**
  128.      * Getter : le code brut (html, json, xml...) à renvoyer au client
  129.      *
  130.      * @return string
  131.      */
  132.     public function getResponse(): string
  133.     {
  134.         return $this->response;
  135.     }
  136.     /**
  137.      * Setter : le code brut (html, json, xml...) qui va être renvoyé au client
  138.      *
  139.      * @param string $response le code brut (html, json, xml...) qui va être renvoyé
  140.      *
  141.      * @return string
  142.      */
  143.     public function setResponse(string $responseint $status 0): string
  144.     {
  145.         if ($response and !$this->getStatus() and !$status$this->setStatus(200);
  146.         if ($status$this->setStatus($status);
  147.         $this->response $response;
  148.         return $this->response;
  149.     }
  150.     /**
  151.      * Get the value of status
  152.      *
  153.      * @return int
  154.      */
  155.     public function getStatus(): int
  156.     {
  157.         if (!$this->getResponse() and !$this->getTemplate()) return 0;
  158.         if (($this->getResponse() or $this->getTemplate()) and !$this->status) return 200;
  159.         return $this->status;
  160.     }
  161.     /**
  162.      * Set the value of status
  163.      *
  164.      * @param int $status
  165.      *
  166.      * @return self
  167.      */
  168.     public function setStatus(int $status): int
  169.     {
  170.         $this->status $status;
  171.         return $this->status;
  172.     }
  173.     /**
  174.      * Get the value of template
  175.      *
  176.      * @return string|false
  177.      */
  178.     public function getTemplate(): string|false
  179.     {
  180.         return $this->template;
  181.     }
  182.     /**
  183.      * Set the value of template
  184.      *
  185.      * @param string $template
  186.      *
  187.      * @return string
  188.      */
  189.     public function setTemplate(string|false $templateFile): string|false
  190.     {
  191.         if ($templateFile and $this->response) new Error([
  192.             "msg" => "Erreur Controller->setTemplate",
  193.             "debug" => "Impossible de régler un template si la route renvoie déjà une réponse.",
  194.             "datas" => $this
  195.         ]);
  196.         if ($templateFile and !$this->getStatus()) $this->setStatus(200);
  197.         $this->template $templateFile;
  198.         return $this->template;
  199.     }
  200.     /**
  201.      * Get the value of js
  202.      *
  203.      * @return array
  204.      */
  205.     public function getJs(): array
  206.     {
  207.         return $this->js;
  208.     }
  209.     /**
  210.      * Set the value of js
  211.      *
  212.      * @param array $js
  213.      *
  214.      * @return self
  215.      */
  216.     public function setJs(array $js): array
  217.     {
  218.         $this->js $js;
  219.         return $this->js;
  220.     }
  221.     /**
  222.      * Add values to css
  223.      *
  224.      * @param array $css
  225.      *
  226.      * @return self
  227.      */
  228.     public function addJs(array|string $js): array
  229.     {
  230.         if (is_array($js)) {
  231.             foreach ($js as $file) {
  232.                 $this->js[] = $file;
  233.             }
  234.         } else $this->js[] = $js;
  235.         return $this->js;
  236.     }
  237.     /**
  238.      * Get the value of css
  239.      *
  240.      * @return array
  241.      */
  242.     public function getCss(): array
  243.     {
  244.         return $this->css;
  245.     }
  246.     /**
  247.      * Set the value of css
  248.      *
  249.      * @param array $css
  250.      *
  251.      * @return self
  252.      */
  253.     public function setCss(array $css): array
  254.     {
  255.         $this->css $css;
  256.         return $this->css;
  257.     }
  258.     /**
  259.      * Add values to css
  260.      *
  261.      * @param array $css
  262.      *
  263.      * @return self
  264.      */
  265.     public function addCss(array|string $css): array
  266.     {
  267.         if (is_array($css)) {
  268.             foreach ($css as $file) {
  269.                 $this->css[] = $file;
  270.             }
  271.         } else $this->css[] = $css;
  272.         return $this->css;
  273.     }
  274.     /**
  275.      * Get the value of buffer
  276.      *
  277.      * @return string
  278.      */
  279.     public function getBuffer(): string
  280.     {
  281.         return $this->buffer;
  282.     }
  283.     /**
  284.      * Set the value of buffer
  285.      *
  286.      * @param string $buffer
  287.      *
  288.      * @return self
  289.      */
  290.     public function setBuffer(string $buffer): string
  291.     {
  292.         $this->buffer $buffer;
  293.         return $this->buffer;
  294.     }
  295.     /**
  296.      * Get the value of twig
  297.      *
  298.      * @return array|string
  299.      */
  300.     public function getTwig(string $key ""): array|string
  301.     {
  302.         if (!$key) return $this->twig;
  303.         else return $this->twig[$key];
  304.     }
  305.     /**
  306.      * Affecter un tableau à ->twig, toute la liste des keys/values en une seule fois
  307.      *
  308.      * @param array $keysvals un tableau clés/valeurs avec toutes les varaibles twig
  309.      *
  310.      * @return self
  311.      */
  312.     public function setTwig(array|string $keysvalsstring|array $value ""): array
  313.     {
  314.         $this->twig = [];
  315.         $this->addTwig($keysvals$value);
  316.         return $this->twig;
  317.     }
  318.     /**
  319.      * ajoute une variable twig ou un tableau de variables sanitizées
  320.      *
  321.      * @param string $key la clé du tableau
  322.      * @param string $val la valeur de la clé
  323.      * @param string $encodeEntities forcer l'encodage des htmlentities
  324.      *
  325.      * @return self
  326.      */
  327.     public function addTwig(array|string $keysvalsstring|array $value ""$encodeEntities false): array
  328.     {
  329.         if (is_array($keysvals)) {
  330.             foreach ($keysvals as $key => $val) {
  331.                 $this->twig[$key] = $this->sanitizer->sanitize($val$encodeEntities);
  332.             }
  333.         } else $this->twig[$keysvals] = $this->sanitizer->sanitize($value$encodeEntities);
  334.         return $this->twig;
  335.     }
  336.     /**
  337.      * ajoute une variable twig ou un tableau de variables de confiance non sanitizées
  338.      *
  339.      * @param string $key la clé du tableau
  340.      * @param string $val la valeur de la clé
  341.      * @param string $encodeEntities forcer l'encodage des htmlentities
  342.      *
  343.      * @return self
  344.      */
  345.     public function addTrustedTwig(array|string $keysvalsstring|array $value ""$encodeEntities false): array
  346.     {
  347.         if (is_array($keysvals)) {
  348.             foreach ($keysvals as $key => $val) {
  349.                 $this->twig[$key] = $val;
  350.             }
  351.         } else $this->twig[$keysvals] = $value;
  352.         return $this->twig;
  353.     }
  354.     /**
  355.      * retirer une clé du tableau twig
  356.      *
  357.      * @param string $key
  358.      *
  359.      * @return self
  360.      */
  361.     public function removeTwig(string $key): array
  362.     {
  363.         unset($this->twig[$key]);
  364.         return $this->twig;
  365.     }
  366.     /**
  367.      * Get the value of routeFound
  368.      *
  369.      * @return bool
  370.      */
  371.     public function getRouteFound(): bool
  372.     {
  373.         return $this->routeFound;
  374.     }
  375.     /**
  376.      * Set the value of routeFound
  377.      *
  378.      * @param bool $routeFound
  379.      *
  380.      * @return void
  381.      */
  382.     public function setRouteFound(bool $routeFound): void
  383.     {
  384.         $this->routeFound $routeFound;
  385.     }
  386. }