<?php
namespace App\EventListener;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Twig\Environment;
class MaintenanceListener
{
private $maintenance;
private $twig;
public function __construct($maintenance, Environment $twig)
{
$this->twig = $twig;
$this->maintenance = $maintenance;
}
public function onKernelRequest(RequestEvent $event)
{
$allowedIp = '192.168.1.10';
$allowedIpLocal = '127.0.0.1';
$allowedIpLocalSecond = 'localhost';
$allowedProxy = "2a01:cb11:bcc:1900:f83d:8a7:ce07:c7b7";
if(!file_exists($this->maintenance) || $_SERVER['REMOTE_ADDR'] == $allowedIp || $_SERVER['REMOTE_ADDR'] == $allowedIpLocal || $_SERVER['REMOTE_ADDR'] == $allowedIpLocalSecond || $_SERVER['REMOTE_ADDR'] == $allowedProxy) {
return;
}
$event->setResponse(
new Response(
$this->twig->render('maintenance.html.twig'),
Response::HTTP_SERVICE_UNAVAILABLE
)
);
$event->stopPropagation();
}
}