src/EventSubscriber/KernelRequestSubscriber.php line 36

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventSubscriber;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpKernel\Event\RequestEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. use Twig\Environment as TwigEnvironment;
  9. class KernelRequestSubscriber implements EventSubscriberInterface
  10. {
  11.     private TwigEnvironment $twig;
  12.     const IP_AUTORIZED = ['192.168.1.69','192.168.1.83','41.188.43.105','41.63.134.150'];
  13.     public function __construct(TwigEnvironment $twig)
  14.     {
  15.         $this->twig $twig;
  16.     }
  17.     /**
  18.      * {@inheritdoc}
  19.      */
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             KernelEvents::REQUEST => [
  24.                 ['onMaintenance'\PHP_INT_MAX 1000],
  25.             ],
  26.         ];
  27.     }
  28.     public function onMaintenance(RequestEvent $event): void
  29.     {
  30.         /** @var bool $isMaintenance */
  31.         $isMaintenance \filter_var($_ENV['MAINTENANCE_MODE'] ?? '0'\FILTER_VALIDATE_BOOLEAN);
  32.         if ($isMaintenance && !in_array($_SERVER['REMOTE_ADDR'],KernelRequestSubscriber::IP_AUTORIZED)) {
  33.             $event->setResponse(new Response(
  34.                 $this->twig->render('maintenance.html.twig'),
  35.                 Response::HTTP_SERVICE_UNAVAILABLE,
  36.             ));
  37.             $event->stopPropagation();
  38.         }
  39.     }
  40. }