src/Twig/AppExtension.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\Twig;
  3. use App\Entity\User;
  4. use App\Service\WarehouseContextService;
  5. use App\Service\WebsiteSettingService;
  6. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  7. use Twig\Extension\AbstractExtension;
  8. use Twig\Extension\GlobalsInterface;
  9. use Twig\TwigFunction;
  10. class AppExtension extends AbstractExtension implements GlobalsInterface
  11. {
  12.     private WebsiteSettingService $websiteSettingService;
  13.     private WarehouseContextService $warehouseContextService;
  14.     private TokenStorageInterface $tokenStorage;
  15.     public function __construct(
  16.         WebsiteSettingService $websiteSettingService,
  17.         WarehouseContextService $warehouseContextService,
  18.         TokenStorageInterface $tokenStorage
  19.     )
  20.     {
  21.         $this->websiteSettingService $websiteSettingService;
  22.         $this->warehouseContextService $warehouseContextService;
  23.         $this->tokenStorage $tokenStorage;
  24.     }
  25.     /* ======================================================
  26.      * GLOBALS TWIG (injectés automatiquement partout)
  27.      * ====================================================== */
  28.     public function getGlobals(): array
  29.     {
  30.         $user $this->tokenStorage->getToken()?->getUser();
  31.         $user $user instanceof User $user null;
  32.         $warehouses $this->warehouseContextService->getAccessibleWarehouses($user);
  33.         return [
  34.             'websiteSettings' => $this->websiteSettingService->all(),
  35.             'currentWarehouseContext' => $this->warehouseContextService->getCurrentWarehouse($user),
  36.             'currentWarehouseName' => $this->warehouseContextService->getCurrentWarehouseName($user),
  37.             'currentUserWarehouses' => $warehouses,
  38.             'currentUserHasMultipleWarehouses' => \count($warehouses) > 1,
  39.         ];
  40.     }
  41.     /* ======================================================
  42.      * FONCTIONS TWIG (optionnelles)
  43.      * ====================================================== */
  44.     public function getFunctions(): array
  45.     {
  46.         return [
  47.             new TwigFunction(
  48.                 'website_setting',
  49.                 [$this'websiteSetting']
  50.             ),
  51.             new TwigFunction(
  52.                 'website_settings',
  53.                 [$this'websiteSettings']
  54.             ),
  55.             new TwigFunction(
  56.                 'current_warehouse',
  57.                 [$this'currentWarehouse']
  58.             ),
  59.         ];
  60.     }
  61.     public function websiteSetting(string $key$default null)
  62.     {
  63.         return $this->websiteSettingService->get($key$default);
  64.     }
  65.     public function websiteSettings(): array
  66.     {
  67.         return $this->websiteSettingService->all();
  68.     }
  69.     public function currentWarehouse()
  70.     {
  71.         $user $this->tokenStorage->getToken()?->getUser();
  72.         return $this->warehouseContextService->getCurrentWarehouse($user instanceof User $user null);
  73.     }
  74. }