src/EventListener/MaintenanceListener.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Symfony\Component\HttpKernel\Event\RequestEvent;
  4. use Symfony\Component\DependencyInjection\ContainerInterface;
  5. use Symfony\Component\HttpFoundation\Response;
  6. class MaintenanceListener
  7. {
  8.     private $container;
  9.     public function __construct(ContainerInterface $container)
  10.     {
  11.         $this->container $container;
  12.     }
  13.     public function onKernelRequest(RequestEvent $event)
  14.     {
  15.         $date = new \DateTime();
  16.         $dateStart = new \DateTime($this->container->getParameter('maintenance_start'));
  17.         $dateEnd = new \DateTime($this->container->getParameter('maintenance_end'));
  18.         if ($date->format("Ymdhis") >= $dateStart->format("Ymdhis") && $date->format("Ymdhis") <= $dateEnd->format("Ymdhis")) {
  19.             $engine $this->container->get('templating');
  20.             $template $engine->render('::maintenance.html.twig', array('dateEnd' => $dateEnd));
  21.             // We send our response with a 503 response code (service unavailable)
  22.             $event->setResponse(new Response($template503));
  23.             $event->stopPropagation();
  24.         }
  25.     }
  26. }