src/EventListener/MaintenanceListener.php line 18

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. {
  8.     private $maintenance;
  9.     private $twig;
  10.     public function __construct($maintenanceEnvironment $twig)
  11.     {
  12.         $this->twig $twig;
  13.         $this->maintenance $maintenance;
  14.     }
  15.     public function onKernelRequest(RequestEvent $event)
  16.     {
  17.         $allowedIp '192.168.1.10';
  18.         $allowedIpLocal '127.0.0.1';
  19.         $allowedIpLocalSecond 'localhost';
  20.         $allowedProxy "2a01:cb11:bcc:1900:f83d:8a7:ce07:c7b7";
  21.         if(!file_exists($this->maintenance) || $_SERVER['REMOTE_ADDR'] == $allowedIp || $_SERVER['REMOTE_ADDR'] == $allowedIpLocal || $_SERVER['REMOTE_ADDR'] == $allowedIpLocalSecond || $_SERVER['REMOTE_ADDR'] == $allowedProxy) {
  22.             return;
  23.         }
  24.         $event->setResponse(
  25.             new Response(
  26.                 $this->twig->render('maintenance.html.twig'),
  27.                 Response::HTTP_SERVICE_UNAVAILABLE
  28.             )
  29.         );
  30.         $event->stopPropagation();
  31.     }
  32. }