<?php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Twig\Environment;
final class OfflineSubscriber implements EventSubscriberInterface
{
private $appOnline;
private $twig;
public function __construct(bool $appOnline, Environment $twig)
{
$this->appOnline = $appOnline;
$this->twig = $twig;
}
public function onKernelRequest(RequestEvent $event): void
{
if ($this->appOnline || !$event->isMainRequest()) {
return;
}
$event->setResponse(new Response(
$this->twig->render('error/offline.html.twig'),
Response::HTTP_NOT_FOUND
));
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => ['onKernelRequest', 256],
];
}
}