vendor/symfony/twig-bridge/AppVariable.php line 151

  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bridge\Twig;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\HttpFoundation\Session\FlashBagAwareSessionInterface;
  14. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  15. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  16. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  17. use Symfony\Component\Security\Core\User\UserInterface;
  18. use Symfony\Component\Translation\LocaleSwitcher;
  19. /**
  20.  * Exposes some Symfony parameters and services as an "app" global variable.
  21.  *
  22.  * @author Fabien Potencier <fabien@symfony.com>
  23.  */
  24. class AppVariable
  25. {
  26.     private TokenStorageInterface $tokenStorage;
  27.     private RequestStack $requestStack;
  28.     private string $environment;
  29.     private bool $debug;
  30.     private LocaleSwitcher $localeSwitcher;
  31.     private array $enabledLocales;
  32.     public function setTokenStorage(TokenStorageInterface $tokenStorage): void
  33.     {
  34.         $this->tokenStorage $tokenStorage;
  35.     }
  36.     public function setRequestStack(RequestStack $requestStack): void
  37.     {
  38.         $this->requestStack $requestStack;
  39.     }
  40.     public function setEnvironment(string $environment): void
  41.     {
  42.         $this->environment $environment;
  43.     }
  44.     public function setDebug(bool $debug): void
  45.     {
  46.         $this->debug $debug;
  47.     }
  48.     public function setLocaleSwitcher(LocaleSwitcher $localeSwitcher): void
  49.     {
  50.         $this->localeSwitcher $localeSwitcher;
  51.     }
  52.     public function setEnabledLocales(array $enabledLocales): void
  53.     {
  54.         $this->enabledLocales $enabledLocales;
  55.     }
  56.     /**
  57.      * Returns the current token.
  58.      *
  59.      * @throws \RuntimeException When the TokenStorage is not available
  60.      */
  61.     public function getToken(): ?TokenInterface
  62.     {
  63.         if (!isset($this->tokenStorage)) {
  64.             throw new \RuntimeException('The "app.token" variable is not available.');
  65.         }
  66.         return $this->tokenStorage->getToken();
  67.     }
  68.     /**
  69.      * Returns the current user.
  70.      *
  71.      * @see TokenInterface::getUser()
  72.      */
  73.     public function getUser(): ?UserInterface
  74.     {
  75.         if (!isset($this->tokenStorage)) {
  76.             throw new \RuntimeException('The "app.user" variable is not available.');
  77.         }
  78.         return $this->tokenStorage->getToken()?->getUser();
  79.     }
  80.     /**
  81.      * Returns the current request.
  82.      */
  83.     public function getRequest(): ?Request
  84.     {
  85.         if (!isset($this->requestStack)) {
  86.             throw new \RuntimeException('The "app.request" variable is not available.');
  87.         }
  88.         return $this->requestStack->getCurrentRequest();
  89.     }
  90.     /**
  91.      * Returns the current session.
  92.      */
  93.     public function getSession(): ?SessionInterface
  94.     {
  95.         if (!isset($this->requestStack)) {
  96.             throw new \RuntimeException('The "app.session" variable is not available.');
  97.         }
  98.         $request $this->getRequest();
  99.         return $request?->hasSession() ? $request->getSession() : null;
  100.     }
  101.     /**
  102.      * Returns the current app environment.
  103.      */
  104.     public function getEnvironment(): string
  105.     {
  106.         if (!isset($this->environment)) {
  107.             throw new \RuntimeException('The "app.environment" variable is not available.');
  108.         }
  109.         return $this->environment;
  110.     }
  111.     /**
  112.      * Returns the current app debug mode.
  113.      */
  114.     public function getDebug(): bool
  115.     {
  116.         if (!isset($this->debug)) {
  117.             throw new \RuntimeException('The "app.debug" variable is not available.');
  118.         }
  119.         return $this->debug;
  120.     }
  121.     public function getLocale(): string
  122.     {
  123.         if (!isset($this->localeSwitcher)) {
  124.             throw new \RuntimeException('The "app.locale" variable is not available.');
  125.         }
  126.         return $this->localeSwitcher->getLocale();
  127.     }
  128.     public function getEnabled_locales(): array
  129.     {
  130.         if (!isset($this->enabledLocales)) {
  131.             throw new \RuntimeException('The "app.enabled_locales" variable is not available.');
  132.         }
  133.         return $this->enabledLocales;
  134.     }
  135.     /**
  136.      * Returns some or all the existing flash messages:
  137.      *  * getFlashes() returns all the flash messages
  138.      *  * getFlashes('notice') returns a simple array with flash messages of that type
  139.      *  * getFlashes(['notice', 'error']) returns a nested array of type => messages.
  140.      */
  141.     public function getFlashes(string|array|null $types null): array
  142.     {
  143.         try {
  144.             $session $this->getSession();
  145.         } catch (\RuntimeException) {
  146.             return [];
  147.         }
  148.         if (!$session instanceof FlashBagAwareSessionInterface) {
  149.             return [];
  150.         }
  151.         if (null === $types || '' === $types || [] === $types) {
  152.             return $session->getFlashBag()->all();
  153.         }
  154.         if (\is_string($types)) {
  155.             return $session->getFlashBag()->get($types);
  156.         }
  157.         $result = [];
  158.         foreach ($types as $type) {
  159.             $result[$type] = $session->getFlashBag()->get($type);
  160.         }
  161.         return $result;
  162.     }
  163.     public function getCurrent_route(): ?string
  164.     {
  165.         if (!isset($this->requestStack)) {
  166.             throw new \RuntimeException('The "app.current_route" variable is not available.');
  167.         }
  168.         return $this->getRequest()?->attributes->get('_route');
  169.     }
  170.     /**
  171.      * @return array<string, mixed>
  172.      */
  173.     public function getCurrent_route_parameters(): array
  174.     {
  175.         if (!isset($this->requestStack)) {
  176.             throw new \RuntimeException('The "app.current_route_parameters" variable is not available.');
  177.         }
  178.         return $this->getRequest()?->attributes->get('_route_params') ?? [];
  179.     }
  180. }