<?php
namespace App\Event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\EventDispatcher\Event;
use Twig\Environment;
class MaintenanceListener implements EventSubscriberInterface
{
private $twig;
public function __construct(Environment $twig){
$this->twig = $twig;
}
public static function getSubscribedEvents()
{
return [
'kernel.request' => 'onKernelRequest',
];
}
public function onKernelRequest(Event $event)
{
$isLocked = false;
if ($isLocked === true && $_SERVER['REMOTE_ADDR'] !== '::1') {
// if ($isLocked === true) {
$event->setResponse(
new Response(
$this->twig->render('bundles/TwigBundle/Exception/maintenance.html.twig'),
Response::HTTP_SERVICE_UNAVAILABLE
)
);
$event->stopPropagation();
}
}
}