src/EventSubscriber/TwigGlobalsSubscriber.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Service\Space\SpaceProvider;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpKernel\Event\RequestEvent;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use Twig\Environment;
  8. class TwigGlobalsSubscriber implements EventSubscriberInterface
  9. {
  10.     private Environment $twig;
  11.     private SpaceProvider $spaceProvider;
  12.     public function __construct(
  13.         Environment   $twig,
  14.         SpaceProvider $spaceProvider
  15.     )
  16.     {
  17.         $this->twig $twig;
  18.         $this->spaceProvider $spaceProvider;
  19.     }
  20.     /**
  21.      * @param RequestEvent $event
  22.      * @return void
  23.      */
  24.     public function onKernelRequest(
  25.         RequestEvent $event
  26.     ): void
  27.     {
  28.         $this->twig->addGlobal('_space'$this->spaceProvider->getSpaceFromRequest($event->getRequest()));
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             KernelEvents::REQUEST => 'onKernelRequest',
  34.         ];
  35.     }
  36. }