src/EventSubscriber/MaintenanceModeSubscriber.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  7. use Symfony\Component\Security\Core\Security;
  8. use Symfony\Contracts\Translation\TranslatorInterface;
  9. use App\Service\AppServices;
  10. use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
  11. use Symfony\Component\HttpFoundation\Response;
  12. class MaintenanceModeSubscriber implements EventSubscriberInterface {
  13.     private $params;
  14.     private $security;
  15.     private $translator;
  16.     private $templating;
  17.     private $services;
  18.     public function __construct(ParameterBagInterface $paramsSecurity $securityTranslatorInterface $translator, \Twig_Environment $templatingAppServices $services) {
  19.         $this->params $params;
  20.         $this->security $security;
  21.         $this->translator $translator;
  22.         $this->templating $templating;
  23.         $this->services $services;
  24.     }
  25.     public function onKernelController(ControllerEvent $event) {
  26.         try {
  27.             if ($this->params->get('maintenance_mode') == '1' && !$this->security->isGranted('ROLE_ADMINISTRATOR')) {
  28.                 $event->setController(
  29.                         function() {
  30.                     return new Response($this->templating->render('Front/Page/maintenance-mode.html.twig', array('customMessage' => $this->services->getSetting('maintenance_mode_custom_message')), 503));
  31.                 });
  32.             }
  33.         } catch (AuthenticationCredentialsNotFoundException $e) {
  34.         }
  35.     }
  36.     public static function getSubscribedEvents() {
  37.         return [
  38.             KernelEvents::CONTROLLER => 'onKernelController',
  39.         ];
  40.     }
  41. }
  42. ?>