<?php
namespace App\EventListener;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
class MaintenanceListener
{
private $container, $router, $security, $maintenance, $isAuthorized;
public function __construct($maintenance = null, $router = null, $security = null, ContainerInterface $container)
{
$this->container = $container;
$this->router = $router;
$this->security = $security;
$this->maintenance = $maintenance['status'];
$this->isAuthorized = $maintenance['isAuthorized'];
}
public function onKernelRequest(GetResponseEvent $event)
{
// This will get the value of our maintenance parameter
/* $maintenance = $this->maintenance ?: false;
$currentIP = $_SERVER['REMOTE_ADDR'];
$response = new JsonResponse();
// 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->isAuthorized) and !$this->security->isGranted('ROLE_ADMINISTRADOR')) {
$attributes = $event->getRequest()->attributes->all();
if (isset($attributes['_route'])) {
if ($attributes['_route'] != 'app_login') {
// We load our maintenance template
$engine = $this->container->get('templating');
$template = $engine->render('frontend/maintenance/maintenance.html.twig');
// We send our response with a 503 response code (service unavailable)
$event->setResponse(new Response($template, 503));
$event->stopPropagation();
}
}
} */
}
}