src/EventListener/MaintenanceListener.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  4. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpKernel\Event\RequestEvent;
  7. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  8. use Twig\Environment;
  9. class MaintenanceListener
  10. {
  11.     private $maintenance;
  12.     private $twig;
  13.     public function __construct($maintenanceEnvironment $twig)
  14.     {
  15.         $this->maintenance filter_var($maintenanceFILTER_VALIDATE_BOOLEAN);
  16.         $this->twig $twig;
  17.     }
  18.     public function onKernelException(ExceptionEvent $event)
  19.     {
  20.     }
  21.     public function onKernelRequest(RequestEvent $event)
  22.     {
  23.         if($this->maintenance){
  24.             $route $event->getRequest()->getRequestUri();
  25.             if(strpos($route'frontoffice') === false){
  26.                 return;
  27.             }
  28.             $page $this->twig->render('maintenance.html.twig');
  29.             $event->setResponse(
  30.                 new Response(
  31.                     $page,
  32.                     Response::HTTP_SERVICE_UNAVAILABLE
  33.                 )
  34.             );
  35.             $event->stopPropagation();
  36.         }
  37.     }
  38. }