vendor/twig/twig/src/Node/Expression/NameExpression.php line 32

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Twig.
  4.  *
  5.  * (c) Fabien Potencier
  6.  * (c) Armin Ronacher
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Twig\Node\Expression;
  12. use Twig\Compiler;
  13. use Twig\Node\Expression\Variable\ContextVariable;
  14. class NameExpression extends AbstractExpression implements SupportDefinedTestInterface
  15. {
  16.     use SupportDefinedTestDeprecationTrait;
  17.     use SupportDefinedTestTrait;
  18.     private $specialVars = [
  19.         '_self' => '$this->getTemplateName()',
  20.         '_context' => '$context',
  21.         '_charset' => '$this->env->getCharset()',
  22.     ];
  23.     public function __construct(string $nameint $lineno)
  24.     {
  25.         if (self::class === static::class) {
  26.             trigger_deprecation('twig/twig''3.15''The "%s" class is deprecated, use "%s" instead.'self::class, ContextVariable::class);
  27.         }
  28.         parent::__construct([], ['name' => $name'ignore_strict_check' => false'always_defined' => false], $lineno);
  29.     }
  30.     public function compile(Compiler $compiler): void
  31.     {
  32.         $name $this->getAttribute('name');
  33.         $compiler->addDebugInfo($this);
  34.         if ($this->definedTest) {
  35.             if (isset($this->specialVars[$name]) || $this->getAttribute('always_defined')) {
  36.                 $compiler->repr(true);
  37.             } elseif (\PHP_VERSION_ID >= 70400) {
  38.                 $compiler
  39.                     ->raw('array_key_exists(')
  40.                     ->string($name)
  41.                     ->raw(', $context)')
  42.                 ;
  43.             } else {
  44.                 $compiler
  45.                     ->raw('(isset($context[')
  46.                     ->string($name)
  47.                     ->raw(']) || array_key_exists(')
  48.                     ->string($name)
  49.                     ->raw(', $context))')
  50.                 ;
  51.             }
  52.         } elseif (isset($this->specialVars[$name])) {
  53.             $compiler->raw($this->specialVars[$name]);
  54.         } elseif ($this->getAttribute('always_defined')) {
  55.             $compiler
  56.                 ->raw('$context[')
  57.                 ->string($name)
  58.                 ->raw(']')
  59.             ;
  60.         } else {
  61.             if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) {
  62.                 $compiler
  63.                     ->raw('($context[')
  64.                     ->string($name)
  65.                     ->raw('] ?? null)')
  66.                 ;
  67.             } else {
  68.                 $compiler
  69.                     ->raw('(isset($context[')
  70.                     ->string($name)
  71.                     ->raw(']) || array_key_exists(')
  72.                     ->string($name)
  73.                     ->raw(', $context) ? $context[')
  74.                     ->string($name)
  75.                     ->raw('] : (function () { throw new RuntimeError(\'Variable ')
  76.                     ->string($name)
  77.                     ->raw(' does not exist.\', ')
  78.                     ->repr($this->lineno)
  79.                     ->raw(', $this->source); })()')
  80.                     ->raw(')')
  81.                 ;
  82.             }
  83.         }
  84.     }
  85.     /**
  86.      * @deprecated since Twig 3.11 (to be removed in 4.0)
  87.      */
  88.     public function isSpecial()
  89.     {
  90.         trigger_deprecation('twig/twig''3.11''The "%s()" method is deprecated and will be removed in Twig 4.0.'__METHOD__);
  91.         return isset($this->specialVars[$this->getAttribute('name')]);
  92.     }
  93.     /**
  94.      * @deprecated since Twig 3.11 (to be removed in 4.0)
  95.      */
  96.     public function isSimple()
  97.     {
  98.         trigger_deprecation('twig/twig''3.11''The "%s()" method is deprecated and will be removed in Twig 4.0.'__METHOD__);
  99.         return !isset($this->specialVars[$this->getAttribute('name')]) && !$this->definedTest;
  100.     }
  101. }