src/EventListener/MaintenanceListener.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Symfony\Component\HttpKernel\Event\RequestEvent;
  4. use Symfony\Component\DependencyInjection\ContainerInterface;
  5. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  6. use Symfony\Component\HttpFoundation\Response;
  7. class MaintenanceListener
  8. {
  9.     private $container;
  10.     private $params;
  11.     public function __construct(ContainerInterface $containerParameterBagInterface $params)
  12.     {
  13.         $this->container $container;
  14.         $this->params $params;
  15.     }
  16.     public function onKernelRequest(RequestEvent $event)
  17.     {
  18.         $isMaintenanceMode $this->params->get('maintenance_mode');
  19.         $maintenanceIps $this->params->get('maintenance_allowed_ips');
  20.         $ips preg_split("/\,/"$maintenanceIps);
  21.         $found false;
  22.         
  23.         if ($isMaintenanceMode){
  24.             foreach($ips as $ip){
  25.                 if($event->getRequest()->getClientIp() == $ip){
  26.                     $found true;
  27.                     break;
  28.                 }
  29.             }
  30.             if(!$found){
  31.                 $template $this->container->get('twig')->render('maintenance.html.twig');
  32.                 $event->setResponse(new Response($template503));
  33.                 $event->stopPropagation();
  34.             }
  35.         }
  36.     }
  37. }