src/Listener/MaintenanceListener.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\Listener;
  3. use App\Entity\Configuracion;
  4. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Doctrine\ORM\EntityManagerInterface as Doctrine;
  7. use Twig\Environment;
  8. use Symfony\Component\HttpKernel\Event\RequestEvent;
  9. class MaintenanceListener
  10. {
  11.     private $appEnv;
  12.     private $em;
  13.     private $twig;
  14.     public function __construct(Doctrine $em,Environment $twig$app_env)
  15.     {
  16.         $this->em $em;
  17.         $this->appEnv $app_env;
  18.         $this->twig $twig;
  19.     }
  20.     public function onKernelRequest(RequestEvent $event)
  21.     {
  22.         if (!$event->isMainRequest()) {
  23.             // don't do anything if it's not the main request
  24.             return;
  25.         }
  26.         $maintenance $this->em -> getRepository(Configuracion::class) -> findOneBy(array('clave' => 'mantenimiento'));
  27.         if($maintenance){
  28.             $underMaintenance $maintenance->getValor();
  29.             $maintenanceUntil $maintenance->getParametros();
  30.             $debug in_array($this->appEnv, array('test''dev'));
  31.             if ($underMaintenance =='1' && !$debug) {
  32.             //if ($underMaintenance =='1') {
  33.                 $engine $this->twig;
  34.                 $content $engine->render('theme/maintenance.html.twig', array('maintenanceUntil'=>$maintenanceUntil));
  35.                 $event->setResponse(new Response($content503));
  36.                 $event->stopPropagation();
  37.             }
  38.         }
  39.     }
  40. }