src/EventSubscriber/EntityListSubscriber.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Repository\Configuration\CompanyEntityRepository;
  4. use App\Service\SelectedEntityService;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. use Twig\Environment;
  9. class EntityListSubscriber implements EventSubscriberInterface
  10. {
  11.     private $twig;
  12.     private $companyEntityRepository;
  13.     private $selectedEntityService;
  14.     public function __construct(Environment $twigCompanyEntityRepository $companyEntityRepositorySelectedEntityService  $selectedEntityService)
  15.     {
  16.         $this->twig $twig;
  17.         $this->companyEntityRepository $companyEntityRepository;
  18.         $this->selectedEntityService $selectedEntityService;
  19.     }
  20.     public function onKernelController(ControllerEvent $event)
  21.     {
  22.         // Ne pas exécuter pour les sous-requêtes
  23.         if (!$event->isMainRequest()) {
  24.             return;
  25.         }
  26.         // Ajouter la liste des entités aux variables globales de Twig
  27.         $this->twig->addGlobal('selectedEntity'$this->selectedEntityService->getSelectedEntity());
  28.     }
  29.     public static function getSubscribedEvents()
  30.     {
  31.         return [
  32.             KernelEvents::CONTROLLER => 'onKernelController',
  33.         ];
  34.     }