<?php
namespace App\EventSubscriber;
use App\Repository\InformationRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Twig\Environment;
class GlobalVariableSubscriber implements EventSubscriberInterface
{
private $twig;
private $informationRepository;
public function __construct(Environment $twig, InformationRepository $informationRepository)
{
$this->twig = $twig;
$this->informationRepository = $informationRepository;
}
public function onKernelController(ControllerEvent $event)
{
// Ajoutez la variable globale "informationCount" à Twig
$this->twig->addGlobal('informationCount', $this->informationRepository->count([]));
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::CONTROLLER => 'onKernelController',
];
}
}