src/EventSubscriber/MaintenanceSubscriber.php line 40

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 Twig\Environment;
  7. use Twig\Error\LoaderError;
  8. use Twig\Error\RuntimeError;
  9. use Twig\Error\SyntaxError;
  10. /**
  11.  *
  12.  */
  13. class MaintenanceSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var Environment
  17.      */
  18.     private $twig;
  19.     /**
  20.      * @param Environment $twig
  21.      */
  22.     public function __construct(Environment $twig)
  23.     {
  24.         $this->twig $twig;
  25.     }
  26.     /**
  27.      * @param RequestEvent $event
  28.      *
  29.      * @return void
  30.      * @throws LoaderError
  31.      * @throws RuntimeError
  32.      * @throws SyntaxError
  33.      */
  34.     public function onKernelRequest(RequestEvent $event): void
  35.     {
  36.         $maintenanceRawValue $_ENV['MAINTENANCE_MODE'] ?? 0;
  37.         if (!filter_var($maintenanceRawValueFILTER_VALIDATE_BOOLEAN)) {
  38.             return;
  39.         }
  40.         $myIp                    $_SERVER['REMOTE_ADDR'];
  41.         $exclusionList           = [];
  42.         $maintenanceRawExclusion $_ENV['MAINTENANCE_EXCLUSION'] ?? null;
  43.         if ($maintenanceRawExclusion) {
  44.             $exclusionList array_map('trim'explode(','$maintenanceRawExclusion));
  45.         }
  46.         if (in_array($myIp$exclusionList)) {
  47.             return;
  48.         }
  49.         $content  $this->twig->render('default/maintenance.html.twig', [
  50.                 'ip' => $myIp,
  51.             ]
  52.         );
  53.         $response = new response($contentResponse::HTTP_SERVICE_UNAVAILABLE);
  54.         $event->setResponse($response);
  55.         $event->stopPropagation();
  56.     }
  57.     /**
  58.      * Returns an array of event names this subscriber wants to listen to.
  59.      *
  60.      * The array keys are event names and the value can be:
  61.      *
  62.      *  * The method name to call (priority defaults to 0)
  63.      *  * An array composed of the method name to call and the priority
  64.      *  * An array of arrays composed of the method names to call and respective
  65.      *    priorities, or 0 if unset
  66.      *
  67.      * For instance:
  68.      *
  69.      *  * ['eventName' => 'methodName']
  70.      *  * ['eventName' => ['methodName', $priority]]
  71.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  72.      *
  73.      * The code must not depend on runtime state as it will only be called at compile time.
  74.      * All logic depending on runtime state must be put into the individual methods handling the events.
  75.      *
  76.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  77.      */
  78.     public static function getSubscribedEvents(): array
  79.     {
  80.         return [
  81.             'kernel.request' => 'onKernelRequest',
  82.         ];
  83.     }
  84. }