src/EventListener/MaintenanceListener.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  4. use Symfony\Component\DependencyInjection\ContainerInterface;
  5. use Symfony\Component\HttpFoundation\Response;
  6. class MaintenanceListener
  7. {
  8.     private $container$maintenance$ipAuthorized;
  9.     public function __construct($maintenanceContainerInterface $container)
  10.     {
  11.         $this->container $container;
  12.         $this->maintenance $maintenance["status"];
  13.         $this->ipAuthorized $maintenance["ipAuthorized"];
  14.     }
  15.     public function onKernelRequest(GetResponseEvent $event)
  16.     {
  17.         // This will get the value of our maintenance parameter
  18.         $maintenance $this->maintenance $this->maintenance false;
  19.         $currentIP $_SERVER['REMOTE_ADDR'];
  20.         // This will detect if we are in dev environment (app_dev.php)
  21.         // $debug = in_array($this->container->get('kernel')->getEnvironment(), ['dev']);
  22.         // If maintenance is active and in prod environment
  23.         if ($maintenance AND !in_array($currentIP$this->ipAuthorized)) {
  24.             // We load our maintenance template
  25.             $engine $this->container->get('templating');
  26.             $template $engine->render('maintenance/maintenance.html.twig');
  27.             // We send our response with a 503 response code (service unavailable)
  28.             $event->setResponse(new Response($template503));
  29.             $event->stopPropagation();
  30.         }
  31.     }
  32. }