src/EventSubscriber/MaintenanceSubscriber.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpKernel\Event\RequestEvent;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use Twig\Environment;
  8. use Twig\Error\LoaderError;
  9. use Twig\Error\RuntimeError;
  10. use Twig\Error\SyntaxError;
  11. class MaintenanceSubscriber implements EventSubscriberInterface
  12. {
  13.     private $environment;
  14.     private $twig;
  15.     public function __construct(string $envEnvironment $twig)
  16.     {
  17.         $this->environment $env;
  18.         $this->twig $twig;
  19.     }
  20.     /**
  21.      * @param RequestEvent $event
  22.      * @throws LoaderError
  23.      * @throws RuntimeError
  24.      * @throws SyntaxError
  25.      */
  26.     public function onKernelRequest(RequestEvent $event)
  27.     {
  28.         if ($this->environment !== 'update') {
  29.             return;
  30.         }
  31.         $html $this->twig->render('maintenance_page.html.twig');
  32.         $response = new Response($html);
  33.         $event->setResponse($response);
  34.     }
  35.     /**
  36.      * @inheritDoc
  37.      */
  38.     public static function getSubscribedEvents()
  39.     {
  40.         return [
  41.             KernelEvents::REQUEST => [
  42.                 ['onKernelRequest'10],
  43.             ],
  44.         ];
  45.     }
  46. }