<?php
namespace App\EventListener;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
class MaintenanceListener
{
/**
* @var array
*/
private $maintenance;
/**
* @var mixed
*/
private $ipAuthorized;
/**
* @var Environment
*/
private $twig;
public function __construct(array $maintenance, Environment $twig)
{
$this->maintenance = $maintenance["statut"];
$this->ipAuthorized = $maintenance["ipAuthorized"];
$this->twig = $twig;
}
public function onKernelRequest(RequestEvent $event)
{
// This will get the value of our maintenance parameter
$maintenance = $this->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 AND !in_array($currentIP, $this->ipAuthorized)) {
// We load our maintenance template
$template = '';
try {
$template = $this->twig->render('maintenance/maintenance.html.twig');
} catch (LoaderError $e) {
throw new ServiceUnavailableHttpException();
} catch (RuntimeError $e) {
} catch (SyntaxError $e) {
}
// We send our response with a 503 response code (service unavailable)
$event->setResponse(new Response($template, 503));
$event->stopPropagation();
}
}
}