src/Listener/MaintenanceListener.php line 20

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