src/Listeners/MaintenanceListener.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Listeners;
  3. use Symfony\Component\DependencyInjection\ContainerInterface;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpKernel\Event\RequestEvent;
  6. use Twig\Environment;
  7. class MaintenanceListener
  8. {
  9.     private $container;
  10.     private $engine;
  11.     private $maintenance;
  12.     private $ipAuthorized;
  13.     public function __construct($maintenanceContainerInterface $containerEnvironment $engine)
  14.     {
  15.         $this->container $container;
  16.         $this->engine $engine;
  17.         $this->maintenance $maintenance["statut"];
  18.         $this->ipAuthorized $maintenance["ipAuthorized"];
  19.     }
  20.     public function onKernelRequest(RequestEvent $event)
  21.     {
  22.         $debug false;
  23.         $maintenance $this->maintenance === 'true' $this->maintenance false;
  24.         $currentIP $_SERVER['REMOTE_ADDR'];
  25.         // If maintenance is active and in prod environment
  26.         if (!$debug && $maintenance && !in_array($currentIP$this->ipAuthorized)) {
  27.             $template $this->engine->render('maintenance/maintenance.html.twig');
  28.             $event->setResponse(new Response($template503));
  29.             $event->stopPropagation();
  30.         }
  31.     }
  32. }