<?php
namespace App\EventListener;
use Exception;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Twig\Environment as Twig;
class MaintenanceListener
{
/** @var bool */
private $isMaintenance;
/** @var string */
private $environment;
private $twig;
/**
* @param bool $isMaintenance
* @param string $environment
* @param Twig $twig
*/
public function __construct(bool $isMaintenance, string $environment, Twig $twig)
{
$this->isMaintenance = $isMaintenance;
$this->environment = $environment;
$this->twig = $twig;
}
/**
* @param GetResponseEvent $event
* @throws Exception
*/
public function onKernelRequest(GetResponseEvent $event)
{
if ($this->isMaintenance && !in_array($this->environment, ['dev'])) {
$template = $this->twig->render('maintenance/index.html.twig');
$event->setResponse(new Response($template, 503));
$event->stopPropagation();
}
}
}