<?php
namespace App\EventListener;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Twig\Environment;
class MaintenanceListener
{
private $maintenance;
/**
* @var Environment
*/
private $twig;
public function __construct($maintenance, Environment $twig)
{
$this->maintenance = $maintenance;
$this->twig = $twig;
}
public function onKernelRequest(RequestEvent $requestEvent){
//on verfier si le fichier maintenance existe
if(!file_exists($this->maintenance)){
return;
}
//Permet de definir la response
$requestEvent->setResponse(
new Response(
$this->twig->render('maintenance/maintenance.html.twig'),
Response::HTTP_SERVICE_UNAVAILABLE
)
);
// on stop le traitement des evenement
$requestEvent->stopPropagation();
}
}