<?php
namespace App\Listeners;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Twig\Environment;
class MaintenanceListener
{
private $container;
private $twig;
public function __construct(ContainerInterface $container, Environment $twig)
{
$this->container = $container;
$this->twig = $twig;
}
public function onKernelRequest(RequestEvent $event)
{
// get maintenance parameters
$underMaintenanceUntil = $this->container->hasParameter('under_maintenance_until') ? $this->container->getParameter('under_maintenance_until') : false;
$maintenance = $this->container->hasParameter('maintenance') ? $this->container->getParameter('maintenance') : null;
$debug = in_array($this->container->get('kernel')->getEnvironment(), array('test'));
// $mailto = $this->container->getParameter('mailto');
// dump($event->getRequest()->getPathInfo());die;
if (
$maintenance
&& !$debug
&& !$this->container->get('security.authorization_checker')->isGranted('ROLE_ADMIN')
&& !in_array($event->getRequest()->getPathInfo(), [ '/login', 'admin' ])
) {
// be sure to create the template and link to it. Location doesn't matter (for me), /web/Resources/views/ might be your best bet
$content = $this->twig->render('maintenance.html.twig',
array(
'underMaintenanceUntil' => $underMaintenanceUntil,
// 'mailto' => $mailto
));
$event->setResponse(new Response($content, 503));
$event->stopPropagation();
}
}
}