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

Breadcrumb

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

class GetAttrExpression

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\GetAttrExpression extends \Twig\Node\Expression\AbstractExpression

Expanded class hierarchy of GetAttrExpression

9 files declare their use of GetAttrExpression
CoreExtension.php in vendor/twig/twig/src/Extension/CoreExtension.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
ExpressionParser.php in vendor/twig/twig/src/ExpressionParser.php
OptimizerNodeVisitor.php in vendor/twig/twig/src/NodeVisitor/OptimizerNodeVisitor.php

... See full list

File

vendor/twig/twig/src/Node/Expression/GetAttrExpression.php, line 19

Namespace

Twig\Node\Expression
View source
class GetAttrExpression extends AbstractExpression {
    
    /**
     * @param ArrayExpression|NameExpression|null $arguments
     */
    public function __construct(AbstractExpression $node, AbstractExpression $attribute, ?AbstractExpression $arguments, string $type, int $lineno) {
        $nodes = [
            'node' => $node,
            'attribute' => $attribute,
        ];
        if (null !== $arguments) {
            $nodes['arguments'] = $arguments;
        }
        if ($arguments && !$arguments instanceof ArrayExpression && !$arguments instanceof NameExpression) {
            trigger_deprecation('twig/twig', '3.15', \sprintf('Not passing a "%s" instance as the "arguments" argument of the "%s" constructor is deprecated ("%s" given).', ArrayExpression::class, static::class, $arguments::class));
        }
        parent::__construct($nodes, [
            'type' => $type,
            'is_defined_test' => false,
            'ignore_strict_check' => false,
            'optimizable' => true,
        ], $lineno);
    }
    public function compile(Compiler $compiler) : void {
        $env = $compiler->getEnvironment();
        $arrayAccessSandbox = false;
        // optimize array calls
        if ($this->getAttribute('optimizable') && (!$env->isStrictVariables() || $this->getAttribute('ignore_strict_check')) && !$this->getAttribute('is_defined_test') && Template::ARRAY_CALL === $this->getAttribute('type')) {
            $var = '$' . $compiler->getVarName();
            $compiler->raw('((' . $var . ' = ')
                ->subcompile($this->getNode('node'))
                ->raw(') && is_array(')
                ->raw($var);
            if (!$env->hasExtension(SandboxExtension::class)) {
                $compiler->raw(') || ')
                    ->raw($var)
                    ->raw(' instanceof ArrayAccess ? (')
                    ->raw($var)
                    ->raw('[')
                    ->subcompile($this->getNode('attribute'))
                    ->raw('] ?? null) : null)');
                return;
            }
            $arrayAccessSandbox = true;
            $compiler->raw(') || ')
                ->raw($var)
                ->raw(' instanceof ArrayAccess && in_array(')
                ->raw($var . '::class')
                ->raw(', CoreExtension::ARRAY_LIKE_CLASSES, true) ? (')
                ->raw($var)
                ->raw('[')
                ->subcompile($this->getNode('attribute'))
                ->raw('] ?? null) : ');
        }
        $compiler->raw('CoreExtension::getAttribute($this->env, $this->source, ');
        if ($this->getAttribute('ignore_strict_check')) {
            $this->getNode('node')
                ->setAttribute('ignore_strict_check', true);
        }
        $compiler->subcompile($this->getNode('node'))
            ->raw(', ')
            ->subcompile($this->getNode('attribute'));
        if ($this->hasNode('arguments')) {
            $compiler->raw(', ')
                ->subcompile($this->getNode('arguments'));
        }
        else {
            $compiler->raw(', []');
        }
        $compiler->raw(', ')
            ->repr($this->getAttribute('type'))
            ->raw(', ')
            ->repr($this->getAttribute('is_defined_test'))
            ->raw(', ')
            ->repr($this->getAttribute('ignore_strict_check'))
            ->raw(', ')
            ->repr($env->hasExtension(SandboxExtension::class))
            ->raw(', ')
            ->repr($this->getNode('node')
            ->getTemplateLine())
            ->raw(')');
        if ($arrayAccessSandbox) {
            $compiler->raw(')');
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
AbstractExpression::hasExplicitParentheses public function
AbstractExpression::isGenerator public function
AbstractExpression::setExplicitParentheses public function
GetAttrExpression::compile public function Overrides Node::compile
GetAttrExpression::__construct public function Overrides Node::__construct
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