<?php
namespace App\EventSubscriber;
use App\Repository\Configuration\CompanyEntityRepository;
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;
public function __construct(Environment $twig, CompanyEntityRepository $companyEntityRepository)
{
$this->twig = $twig;
$this->companyEntityRepository = $companyEntityRepository;
}
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('company_entities', $this->companyEntityRepository->findAll());
}
public static function getSubscribedEvents()
{
return [
KernelEvents::CONTROLLER => 'onKernelController',
];
}
}