<?php
namespace App\EventListener;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
class MaintenanceListener
{
private $maintenance, $ipAuthorized, $container;
public function __construct($maintenance, ContainerInterface $container)
{
$this->container = $container;
$this->maintenance = $maintenance["statut"];
$this->ipAuthorized = $maintenance["ipAuthorized"];
}
public function onKernelRequest(GetResponseEvent $event)
{
// This will get the value of our maintenance parameter
$maintenance = $this->maintenance || false;
$currentIP = $_SERVER['REMOTE_ADDR'];
// This will detect if we are in dev environment (app_dev.php)
// $debug = in_array($this->container->get('kernel')->getEnvironment(), ['dev/']);
// If maintenance is active and in prod environment
if ($maintenance && !in_array($currentIP, $this->ipAuthorized, true)) {
// We load our maintenance template
$engine = $this->container->get('templating');
$template = $engine->render('admin/maintenance/index.html.twig');
// We send our response with a 503 response code (service unavailable)
$event->setResponse(new Response($template, 503));
$event->stopPropagation();
}
}
}