src/EventListener/MaintenanceListener.php line 22

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