src/EventListener/MaintenanceListener.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Symfony\Component\HttpKernel\Event\RequestEvent;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Twig\Environment;
  6. class MaintenanceListener
  7. {
  8.     public $engine;
  9.     private $maintenance$ipAuthorized;
  10.     public function __construct($maintenanceEnvironment $engine)
  11.     {
  12.         $this->engine $engine;
  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.             $template $this->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. }