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

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
  15. {
  16.     private $specialVars = [
  17.         '_self' => '$this->getTemplateName()',
  18.         '_context' => '$context',
  19.         '_charset' => '$this->env->getCharset()',
  20.     ];
  21.     public function __construct(string $nameint $lineno)
  22.     {
  23.         if (self::class === static::class) {
  24.             trigger_deprecation('twig/twig''3.15''The "%s" class is deprecated, use "%s" instead.'self::class, ContextVariable::class);
  25.         }
  26.         parent::__construct([], ['name' => $name'is_defined_test' => false'ignore_strict_check' => false'always_defined' => false], $lineno);
  27.     }
  28.     public function compile(Compiler $compiler): void
  29.     {
  30.         $name $this->getAttribute('name');
  31.         $compiler->addDebugInfo($this);
  32.         if ($this->getAttribute('is_defined_test')) {
  33.             if (isset($this->specialVars[$name])) {
  34.                 $compiler->repr(true);
  35.             } elseif (\PHP_VERSION_ID >= 70400) {
  36.                 $compiler
  37.                     ->raw('array_key_exists(')
  38.                     ->string($name)
  39.                     ->raw(', $context)')
  40.                 ;
  41.             } else {
  42.                 $compiler
  43.                     ->raw('(isset($context[')
  44.                     ->string($name)
  45.                     ->raw(']) || array_key_exists(')
  46.                     ->string($name)
  47.                     ->raw(', $context))')
  48.                 ;
  49.             }
  50.         } elseif (isset($this->specialVars[$name])) {
  51.             $compiler->raw($this->specialVars[$name]);
  52.         } elseif ($this->getAttribute('always_defined')) {
  53.             $compiler
  54.                 ->raw('$context[')
  55.                 ->string($name)
  56.                 ->raw(']')
  57.             ;
  58.         } else {
  59.             if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) {
  60.                 $compiler
  61.                     ->raw('($context[')
  62.                     ->string($name)
  63.                     ->raw('] ?? null)')
  64.                 ;
  65.             } else {
  66.                 $compiler
  67.                     ->raw('(isset($context[')
  68.                     ->string($name)
  69.                     ->raw(']) || array_key_exists(')
  70.                     ->string($name)
  71.                     ->raw(', $context) ? $context[')
  72.                     ->string($name)
  73.                     ->raw('] : (function () { throw new RuntimeError(\'Variable ')
  74.                     ->string($name)
  75.                     ->raw(' does not exist.\', ')
  76.                     ->repr($this->lineno)
  77.                     ->raw(', $this->source); })()')
  78.                     ->raw(')')
  79.                 ;
  80.             }
  81.         }
  82.     }
  83.     /**
  84.      * @deprecated since Twig 3.11 (to be removed in 4.0)
  85.      */
  86.     public function isSpecial()
  87.     {
  88.         trigger_deprecation('twig/twig''3.11''The "%s()" method is deprecated and will be removed in Twig 4.0.'__METHOD__);
  89.         return isset($this->specialVars[$this->getAttribute('name')]);
  90.     }
  91.     /**
  92.      * @deprecated since Twig 3.11 (to be removed in 4.0)
  93.      */
  94.     public function isSimple()
  95.     {
  96.         trigger_deprecation('twig/twig''3.11''The "%s()" method is deprecated and will be removed in Twig 4.0.'__METHOD__);
  97.         return !$this->isSpecial() && !$this->getAttribute('is_defined_test');
  98.     }
  99. }