src/EventSubscriber/GlobalSpacesSubscriber.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Repository\SpaceRepository;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use Twig\Environment;
  8. class GlobalSpacesSubscriber implements EventSubscriberInterface
  9. {
  10.     private $twig;
  11.     private $spaceRepository;
  12.     public function __construct(Environment $twigSpaceRepository $spaceRepository)
  13.     {
  14.         $this->twig $twig;
  15.         $this->spaceRepository $spaceRepository;
  16.     }
  17.     public function onKernelController(ControllerEvent $event): void
  18.     {
  19.         $spaces $this->spaceRepository->findBy([], ['name' => 'ASC']);
  20.         $this->twig->addGlobal('spaces'$spaces);
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             KernelEvents::CONTROLLER => 'onKernelController',
  26.         ];
  27.     }
  28. }