Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. NameExpression.php

class NameExpression

Hierarchy

  • class \Twig\Node\Node implements \Twig\Node\Countable, \Twig\Node\IteratorAggregate
    • class \Twig\Node\Expression\AbstractExpression extends \Twig\Node\Node
      • class \Twig\Node\Expression\NameExpression extends \Twig\Node\Expression\AbstractExpression

Expanded class hierarchy of NameExpression

12 files declare their use of NameExpression
ContextVariable.php in vendor/twig/twig/src/Node/Expression/Variable/ContextVariable.php
DefaultFilter.php in vendor/twig/twig/src/Node/Expression/Filter/DefaultFilter.php
DefinedTest.php in vendor/twig/twig/src/Node/Expression/Test/DefinedTest.php
EmbedTokenParser.php in vendor/twig/twig/src/TokenParser/EmbedTokenParser.php
ExpressionParser.php in vendor/twig/twig/src/ExpressionParser.php

... See full list

File

vendor/twig/twig/src/Node/Expression/NameExpression.php, line 18

Namespace

Twig\Node\Expression
View source
class NameExpression extends AbstractExpression {
    private $specialVars = [
        '_self' => '$this->getTemplateName()',
        '_context' => '$context',
        '_charset' => '$this->env->getCharset()',
    ];
    public function __construct(string $name, int $lineno) {
        if (self::class === static::class) {
            trigger_deprecation('twig/twig', '3.15', 'The "%s" class is deprecated, use "%s" instead.', self::class, ContextVariable::class);
        }
        parent::__construct([], [
            'name' => $name,
            'is_defined_test' => false,
            'ignore_strict_check' => false,
            'always_defined' => false,
        ], $lineno);
    }
    public function compile(Compiler $compiler) : void {
        $name = $this->getAttribute('name');
        $compiler->addDebugInfo($this);
        if ($this->getAttribute('is_defined_test')) {
            if (isset($this->specialVars[$name])) {
                $compiler->repr(true);
            }
            elseif (\PHP_VERSION_ID >= 70400) {
                $compiler->raw('array_key_exists(')
                    ->string($name)
                    ->raw(', $context)');
            }
            else {
                $compiler->raw('(isset($context[')
                    ->string($name)
                    ->raw(']) || array_key_exists(')
                    ->string($name)
                    ->raw(', $context))');
            }
        }
        elseif (isset($this->specialVars[$name])) {
            $compiler->raw($this->specialVars[$name]);
        }
        elseif ($this->getAttribute('always_defined')) {
            $compiler->raw('$context[')
                ->string($name)
                ->raw(']');
        }
        else {
            if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()
                ->isStrictVariables()) {
                $compiler->raw('($context[')
                    ->string($name)
                    ->raw('] ?? null)');
            }
            else {
                $compiler->raw('(isset($context[')
                    ->string($name)
                    ->raw(']) || array_key_exists(')
                    ->string($name)
                    ->raw(', $context) ? $context[')
                    ->string($name)
                    ->raw('] : (function () { throw new RuntimeError(\'Variable ')
                    ->string($name)
                    ->raw(' does not exist.\', ')
                    ->repr($this->lineno)
                    ->raw(', $this->source); })()')
                    ->raw(')');
            }
        }
    }
    
    /**
     * @deprecated since Twig 3.11 (to be removed in 4.0)
     */
    public function isSpecial() {
        trigger_deprecation('twig/twig', '3.11', 'The "%s()" method is deprecated and will be removed in Twig 4.0.', __METHOD__);
        return isset($this->specialVars[$this->getAttribute('name')]);
    }
    
    /**
     * @deprecated since Twig 3.11 (to be removed in 4.0)
     */
    public function isSimple() {
        trigger_deprecation('twig/twig', '3.11', 'The "%s()" method is deprecated and will be removed in Twig 4.0.', __METHOD__);
        return !$this->isSpecial() && !$this->getAttribute('is_defined_test');
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
AbstractExpression::hasExplicitParentheses public function
AbstractExpression::isGenerator public function
AbstractExpression::setExplicitParentheses public function
NameExpression::$specialVars private property
NameExpression::compile public function Overrides Node::compile 1
NameExpression::isSimple Deprecated public function
NameExpression::isSpecial Deprecated public function
NameExpression::__construct public function Overrides Node::__construct 1
Node::$attributeNameDeprecations private property @var array<string, NameDeprecation>
Node::$attributes protected property
Node::$lineno protected property
Node::$nodeNameDeprecations private property @var array<string, NameDeprecation>
Node::$nodes protected property
Node::$sourceContext private property
Node::$tag protected property
Node::count public function
Node::deprecateAttribute public function
Node::deprecateNode public function
Node::getAttribute public function
Node::getIterator public function
Node::getNode public function
Node::getNodeTag public function
Node::getSourceContext public function
Node::getTemplateLine public function
Node::getTemplateName public function
Node::hasAttribute public function
Node::hasNode public function
Node::removeAttribute public function
Node::removeNode public function
Node::setAttribute public function
Node::setNode public function 1
Node::setNodeTag public function @internal
Node::setSourceContext public function
Node::__toString public function
RSS feed
Powered by Drupal