src/EventListener/MaintenanceListener.php line 51

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Symfony\Component\HttpFoundation\Response;
  4. use Symfony\Component\HttpKernel\Event\RequestEvent;
  5. use Symfony\Component\Templating\EngineInterface;
  6. /**
  7.  * Class MaintenanceListener
  8.  *
  9.  * @package App\EventListener
  10.  */
  11. class MaintenanceListener
  12. {
  13.     /**
  14.      * @var string
  15.      */
  16.     public const ENV_DEV 'dev';
  17.     /**
  18.      * @var int
  19.      */
  20.     private $maintenance;
  21.     /**
  22.      * @var string
  23.      */
  24.     private $env;
  25.     /**
  26.      * @var EngineInterface
  27.      */
  28.     private $templating;
  29.     /**
  30.      * MaintenanceListener constructor.
  31.      *
  32.      * @param EngineInterface $templating
  33.      */
  34.     public function __construct(EngineInterface $templating)
  35.     {
  36.         $this->maintenance getenv('MAINTENANCE');
  37.         $this->env         getenv('APP_ENV');
  38.         $this->templating  $templating;
  39.     }
  40.     /**
  41.      * @param RequestEvent $event
  42.      */
  43.     public function onKernelRequest(RequestEvent $event)
  44.     {
  45.          $debug $this->env === self::ENV_DEV;
  46.          if ($this->maintenance && !$debug) {
  47.              $template $this->templating->render('maintenance.html.twig');
  48.              $event->setResponse(new Response($templateResponse::HTTP_SERVICE_UNAVAILABLE));
  49.          }
  50.     }
  51. }