vendor/shopware/storefront/Framework/Twig/TwigAppVariable.php line 16

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Framework\Twig;
  3. use Symfony\Bridge\Twig\AppVariable;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\RequestStack;
  6. use Symfony\Component\HttpFoundation\Session\Session;
  7. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. /**
  11.  * To allow custom server parameters,
  12.  */
  13. class TwigAppVariable extends AppVariable
  14. {
  15.     private ?Request $request null;
  16.     private AppVariable $appVariable;
  17.     private array $allowList;
  18.     public function __construct(AppVariable $appVariable, array $allowList = [])
  19.     {
  20.         $this->allowList $allowList;
  21.         $this->appVariable $appVariable;
  22.     }
  23.     /**
  24.      * @return Request|null
  25.      */
  26.     public function getRequest()
  27.     {
  28.         if ($this->request !== null) {
  29.             return $this->request;
  30.         }
  31.         $request $this->appVariable->getRequest();
  32.         if ($request === null) {
  33.             throw new \RuntimeException('The "app.request" variable is not available.');
  34.         }
  35.         $clonedRequest = clone $request;
  36.         $clonedRequest->server = clone $clonedRequest->server;
  37.         foreach ($clonedRequest->server->all() as $key => $_) {
  38.             if (!\in_array(strtolower($key), $this->allowListtrue)) {
  39.                 $clonedRequest->server->remove($key);
  40.             }
  41.         }
  42.         $this->request $clonedRequest;
  43.         return $clonedRequest;
  44.     }
  45.     public function setTokenStorage(TokenStorageInterface $tokenStorage): void
  46.     {
  47.         $this->appVariable->setTokenStorage($tokenStorage);
  48.     }
  49.     public function setRequestStack(RequestStack $requestStack): void
  50.     {
  51.         $this->appVariable->setRequestStack($requestStack);
  52.     }
  53.     public function setEnvironment(string $environment): void
  54.     {
  55.         $this->appVariable->setEnvironment($environment);
  56.     }
  57.     public function setDebug(bool $debug): void
  58.     {
  59.         $this->appVariable->setDebug($debug);
  60.     }
  61.     /**
  62.      * @return TokenInterface|null
  63.      */
  64.     public function getToken()
  65.     {
  66.         return $this->appVariable->getToken();
  67.     }
  68.     /**
  69.      * @return UserInterface|null
  70.      */
  71.     public function getUser()
  72.     {
  73.         return $this->appVariable->getUser();
  74.     }
  75.     /**
  76.      * @return Session|null
  77.      */
  78.     public function getSession()
  79.     {
  80.         return $this->appVariable->getSession();
  81.     }
  82.     /**
  83.      * @return string
  84.      */
  85.     public function getEnvironment()
  86.     {
  87.         return $this->appVariable->getEnvironment();
  88.     }
  89.     /**
  90.      * @return bool
  91.      */
  92.     public function getDebug()
  93.     {
  94.         return $this->appVariable->getDebug();
  95.     }
  96.     /**
  97.      * @return array
  98.      */
  99.     public function getFlashes($types null)
  100.     {
  101.         return $this->appVariable->getFlashes($types);
  102.     }
  103. }