<?php
namespace App\EventSubscriber;
use App\Service\Cart\CartService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Twig\Environment;
class CartSubscriber implements EventSubscriberInterface
{
private $cartService;
private $twig;
public function __construct(CartService $cartService, Environment $twig)
{
$this->cartService = $cartService;
$this->twig = $twig;
}
public static function getSubscribedEvents(): array
{
return [
'kernel.controller' => 'onKernelController',
];
}
public function onKernelController(ControllerEvent $event)
{
// Injecter les variables globales dans tous les templates
$items = $this->cartService->getFullCart();
$total = $this->cartService->getTotal();
// Utiliser Twig pour les rendre disponibles globalement
$this->twig->addGlobal('items', $items);
$this->twig->addGlobal('total', $total);
}
}