src/EventSubscriber/OfflineSubscriber.php line 22

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. final class OfflineSubscriber implements EventSubscriberInterface
  9. {
  10. private $appOnline;
  11. private $twig;
  12. public function __construct(bool $appOnline, Environment $twig)
  13. {
  14. $this->appOnline = $appOnline;
  15. $this->twig = $twig;
  16. }
  17. public function onKernelRequest(RequestEvent $event): void
  18. {
  19. if ($this->appOnline || !$event->isMainRequest()) {
  20. return;
  21. }
  22. $event->setResponse(new Response(
  23. $this->twig->render('error/offline.html.twig'),
  24. Response::HTTP_NOT_FOUND
  25. ));
  26. }
  27. public static function getSubscribedEvents(): array
  28. {
  29. return [
  30. KernelEvents::REQUEST => ['onKernelRequest', 256],
  31. ];
  32. }
  33. }