src/EventListener/MaintenanceListener.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Symfony\Component\HttpFoundation\Response;
  4. use Symfony\Component\HttpKernel\Event\RequestEvent;
  5. use Twig\Environment;
  6. class MaintenanceListener{
  7.     private $maintenance;
  8.     private $twig;
  9.     public function __construct($maintenanceEnvironment $twig){
  10.         $this->maintenance $maintenance;
  11.         $this->twig $twig;
  12.     }
  13.     public function onKernelRequest(RequestEvent $event){
  14.         //On vérifie si le fichier .maintenance n'existe pas
  15.         if (!file_exists($this->maintenance)){
  16.             return;
  17.         }
  18.         // Le fichier existe
  19.         // On définit la réponse
  20.         $event->setResponse(
  21.             new Response(
  22.                 $this->twig->render('maintenance/maintenance.html.twig'),Response::HTTP_SERVICE_UNAVAILABLE
  23.             )
  24.         );
  25.         //On stop le traitement des évènements
  26.         $event->stopPropagation();
  27.     }
  28. }