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

Breadcrumb

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

function NameExpression::compile

Overrides Node::compile

1 method overrides NameExpression::compile()
AssignNameExpression::compile in vendor/twig/twig/src/Node/Expression/AssignNameExpression.php

File

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

Class

NameExpression

Namespace

Twig\Node\Expression

Code

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(')');
        }
    }
}
RSS feed
Powered by Drupal