<?php
namespace App\EventSubscriber;
use App\Repository\CategoriProductRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Twig\Environment;
use App\Service\TaobaoService ;
class GlobalVariablesSubscriber implements EventSubscriberInterface
{
private Environment $twig;
private CategoriProductRepository $categoriProductRepository;
private RequestStack $requestStack;
private TaobaoService $taobaoService;
public function __construct(Environment $twig, CategoriProductRepository $categoriProductRepository, RequestStack $requestStack, TaobaoService $taobaoService)
{
$this->twig = $twig;
$this->categoriProductRepository = $categoriProductRepository;
$this->requestStack = $requestStack;
$this->taobaoService = $taobaoService;
}
public function onKernelController(ControllerEvent $event)
{
// Récupérer l'objet Request à partir de RequestStack
$request = $this->requestStack->getCurrentRequest();
if (!$request) {
return; // Sécurité : éviter d'accéder à une requête inexistante
}
// Récupération des catégories
$categories = $this->categoriProductRepository->findAll();
// Récupération des données de la session
$session = $request->getSession();
$countListPanierSession = $session->get('listPanier', []);
// dd($countListPanierSession, count($countListPanierSession));
// Récupération des produits Taobao
// $taobaoProducts = $this->taobaoService->getAllProducts();
// dd($taobaoProducts);
// Ajout des variables globales à Twig
$this->twig->addGlobal('categories', $categories);
$this->twig->addGlobal('countListPanier', count($countListPanierSession));
// $this->twig->addGlobal('taobaoProducts', $taobaoProducts);
// dd($taobaoProducts);
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::CONTROLLER => 'onKernelController',
];
}
}