<?php
namespace App\EventSubscriber;
use App\Repository\Configuration\CompanyEntityRepository;
use App\Service\SelectedEntityService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Twig\Environment;
class EntityListSubscriber implements EventSubscriberInterface
{
private $twig;
private $companyEntityRepository;
private $selectedEntityService;
public function __construct(Environment $twig, CompanyEntityRepository $companyEntityRepository, SelectedEntityService $selectedEntityService)
{
$this->twig = $twig;
$this->companyEntityRepository = $companyEntityRepository;
$this->selectedEntityService = $selectedEntityService;
}
public function onKernelController(ControllerEvent $event)
{
// Ne pas exécuter pour les sous-requêtes
if (!$event->isMainRequest()) {
return;
}
// Ajouter la liste des entités aux variables globales de Twig
$this->twig->addGlobal('selectedEntity', $this->selectedEntityService->getSelectedEntity());
}
public static function getSubscribedEvents()
{
return [
KernelEvents::CONTROLLER => 'onKernelController',
];
}
}