1<?php
2
3/*
4 * This file is part of Twig.
5 *
6 * (c) Fabien Potencier
7 * (c) Armin Ronacher
8 *
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
11 */
12
13namespace Twig\Node\Expression;
14
15use Twig\Compiler;
16use Twig\Template;
17
18class GetAttrExpression extends AbstractExpression
19{
20    public function __construct(AbstractExpression $node, AbstractExpression $attribute, AbstractExpression $arguments = null, $type, $lineno)
21    {
22        $nodes = ['node' => $node, 'attribute' => $attribute];
23        if (null !== $arguments) {
24            $nodes['arguments'] = $arguments;
25        }
26
27        parent::__construct($nodes, ['type' => $type, 'is_defined_test' => false, 'ignore_strict_check' => false, 'disable_c_ext' => false], $lineno);
28    }
29
30    public function compile(Compiler $compiler)
31    {
32        if ($this->getAttribute('disable_c_ext')) {
33            @trigger_error(sprintf('Using the "disable_c_ext" attribute on %s is deprecated since version 1.30 and will be removed in 2.0.', __CLASS__), E_USER_DEPRECATED);
34        }
35
36        if (\function_exists('twig_template_get_attributes') && !$this->getAttribute('disable_c_ext')) {
37            $compiler->raw('twig_template_get_attributes($this, ');
38        } else {
39            $compiler->raw('$this->getAttribute(');
40        }
41
42        if ($this->getAttribute('ignore_strict_check')) {
43            $this->getNode('node')->setAttribute('ignore_strict_check', true);
44        }
45
46        $compiler->subcompile($this->getNode('node'));
47
48        $compiler->raw(', ')->subcompile($this->getNode('attribute'));
49
50        // only generate optional arguments when needed (to make generated code more readable)
51        $needFourth = $this->getAttribute('ignore_strict_check');
52        $needThird = $needFourth || $this->getAttribute('is_defined_test');
53        $needSecond = $needThird || Template::ANY_CALL !== $this->getAttribute('type');
54        $needFirst = $needSecond || $this->hasNode('arguments');
55
56        if ($needFirst) {
57            if ($this->hasNode('arguments')) {
58                $compiler->raw(', ')->subcompile($this->getNode('arguments'));
59            } else {
60                $compiler->raw(', []');
61            }
62        }
63
64        if ($needSecond) {
65            $compiler->raw(', ')->repr($this->getAttribute('type'));
66        }
67
68        if ($needThird) {
69            $compiler->raw(', ')->repr($this->getAttribute('is_defined_test'));
70        }
71
72        if ($needFourth) {
73            $compiler->raw(', ')->repr($this->getAttribute('ignore_strict_check'));
74        }
75
76        $compiler->raw(')');
77    }
78}
79
80class_alias('Twig\Node\Expression\GetAttrExpression', 'Twig_Node_Expression_GetAttr');
81