src/EventListener/MaintenanceListener.php line 22

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 const AUTHORIZED_IP = '82.65.24.15';
  8.     private $authorizedIp;
  9.     private $maintenance;
  10.     private $twig;
  11.     
  12.     public function __construct($maintenanceEnvironment $twig$authorizedIp){
  13.         $this->maintenance $maintenance;
  14.         $this->twig $twig;
  15.         $this->authorizedIp $authorizedIp;
  16.     }
  17.     public function onKernelRequest(RequestEvent $event){
  18.         $userIp $event->getRequest()->getClientIp();
  19.         
  20.         //if(!file_exists($this->maintenance) || $userIp == self::AUTHORIZED_IP){
  21.         if(!file_exists($this->maintenance) || $userIp == $this->authorizedIp){
  22.             return;
  23.         }
  24.         $event->setResponse(
  25.             new Response(
  26.                 $this->twig->render('maintenance/index.html.twig',
  27.                 [
  28.                     'title_meta' => 'Site en maintenance',
  29.                     'page' => 'maintenance',
  30.                 ]),
  31.                 Response::HTTP_SERVICE_UNAVAILABLE
  32.             )
  33.         );
  34.         $event->stopPropagation();
  35.     }
  36. }