src/Listeners/MaintenanceListener.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Listeners;
  3. use Symfony\Component\HttpKernel\Event\RequestEvent;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\DependencyInjection\ContainerInterface;
  6. use Twig\Environment;
  7. class MaintenanceListener
  8. {
  9.     private $container;
  10.     private $twig;
  11.     public function __construct(ContainerInterface $containerEnvironment $twig)
  12.     {
  13.         $this->container $container;
  14.         $this->twig $twig;
  15.     }
  16.     public function onKernelRequest(RequestEvent $event)
  17.     {
  18.         // get maintenance parameters
  19.         $underMaintenanceUntil $this->container->hasParameter('under_maintenance_until') ? $this->container->getParameter('under_maintenance_until') : false;
  20.         $maintenance $this->container->hasParameter('maintenance') ? $this->container->getParameter('maintenance') : null;
  21.         $debug in_array($this->container->get('kernel')->getEnvironment(), array('test'));
  22.         // $mailto = $this->container->getParameter('mailto');
  23.         // dump($event->getRequest()->getPathInfo());die;
  24.         if (
  25.             $maintenance
  26.             && !$debug 
  27.             && !$this->container->get('security.authorization_checker')->isGranted('ROLE_ADMIN')
  28.             && !in_array($event->getRequest()->getPathInfo(), [ '/login''admin' ]) 
  29.         ) {
  30.             // be sure to create the template and link to it. Location doesn't matter (for me), /web/Resources/views/ might be your best bet
  31.             $content $this->twig->render('maintenance.html.twig'
  32.             array(
  33.                 'underMaintenanceUntil' => $underMaintenanceUntil,
  34.                 // 'mailto' => $mailto
  35.             ));
  36.             $event->setResponse(new Response($content503));
  37.             $event->stopPropagation();
  38.         }
  39.     }
  40. }