<?php
namespace App\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Twig\Environment;
class MaintenanceListener implements EventSubscriberInterface
{
private bool $maintenanceMode;
private Environment $engin;
public function __construct(bool $maintenanceMode, Environment $engine)
{
$this->maintenanceMode = $maintenanceMode;
$this->engin = $engine;
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => 'onKernelRequest',
];
}
public function onKernelRequest(RequestEvent $event)
{
//display a specific twig if maintenanceMode == true
if ($this->maintenanceMode) {
$html = $this->engin->render('maintenance/maintenance.html.twig');
$event->setResponse(new Response($html, 503));
}
return;
}
}