src/EventSubscriber/MaintenanceSubscriber.php line 29

  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. class MaintenanceSubscriber implements EventSubscriberInterface
  9. {
  10.     private Environment $twig;
  11.     public function __construct(Environment $twig)
  12.     {
  13.         $this->twig $twig;
  14.     }
  15.     public static function getSubscribedEvents(): array
  16.     {
  17.         return [
  18.             KernelEvents::REQUEST => [
  19.                 ['onMaintenance'\PHP_INT_MAX 1000],
  20.             ],
  21.         ];
  22.     }
  23.     public function onMaintenance(RequestEvent $event): void
  24.     {
  25.         /** @var bool $isMaintenance */
  26.         $isMaintenance \filter_var($_ENV['MAINTENANCE_MODE'] ?? '0'\FILTER_VALIDATE_BOOLEAN);
  27.         if ($isMaintenance) {
  28.             $event->setResponse(new Response(
  29.                 $this->twig->render('maintenance.html.twig'),
  30.                 Response::HTTP_SERVICE_UNAVAILABLE,
  31.             ));
  32.             $event->stopPropagation();
  33.         }
  34.     }
  35. }