<?php
namespace App\EventListener;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;
class MaintenanceListener
{
private $templating;
private $maintenance;
public function __construct(Environment $templating)
{
$this->templating = $templating;
$this->maintenance = $_ENV['MAINTENANCE'];
}
public function onKernelRequest(RequestEvent $event)
{
$request = $event->getRequest();
$path = preg_match("/(^\/moderate\/|^\/login|^\/2fa)(.*)/i", $event->getRequest()->getrequestUri());
$navbar = preg_match("/(^App\\\\Controller\\\\NavigationController::navigation)/i", $event->getRequest()->attributes->get('_controller') );
$sidebar = preg_match("/(^App\\\\Controller\\\\DonationsController::sidebar)/i", $event->getRequest()->attributes->get('_controller') );
if ($this->maintenance == "true" && $path == false && $navbar == false && $sidebar == false) {
$event->setResponse(
new Response(
$this->templating->render('maintenance.html.twig'),
Response::HTTP_SERVICE_UNAVAILABLE
)
);
$event->stopPropagation();
} else {
}
}
}