Version 1
[yaffs-website] / vendor / twig / twig / lib / Twig / Node / Expression / Name.php
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 class Twig_Node_Expression_Name extends Twig_Node_Expression
13 {
14     protected $specialVars = array(
15         '_self' => '$this',
16         '_context' => '$context',
17         '_charset' => '$this->env->getCharset()',
18     );
19
20     public function __construct($name, $lineno)
21     {
22         parent::__construct(array(), array('name' => $name, 'is_defined_test' => false, 'ignore_strict_check' => false, 'always_defined' => false), $lineno);
23     }
24
25     public function compile(Twig_Compiler $compiler)
26     {
27         $name = $this->getAttribute('name');
28
29         $compiler->addDebugInfo($this);
30
31         if ($this->getAttribute('is_defined_test')) {
32             if ($this->isSpecial()) {
33                 $compiler->repr(true);
34             } else {
35                 $compiler->raw('array_key_exists(')->repr($name)->raw(', $context)');
36             }
37         } elseif ($this->isSpecial()) {
38             $compiler->raw($this->specialVars[$name]);
39         } elseif ($this->getAttribute('always_defined')) {
40             $compiler
41                 ->raw('$context[')
42                 ->string($name)
43                 ->raw(']')
44             ;
45         } else {
46             if (PHP_VERSION_ID >= 70000) {
47                 // use PHP 7 null coalescing operator
48                 $compiler
49                     ->raw('($context[')
50                     ->string($name)
51                     ->raw('] ?? ')
52                 ;
53
54                 if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) {
55                     $compiler->raw('null)');
56                 } else {
57                     $compiler->raw('$this->getContext($context, ')->string($name)->raw('))');
58                 }
59             } elseif (PHP_VERSION_ID >= 50400) {
60                 // PHP 5.4 ternary operator performance was optimized
61                 $compiler
62                     ->raw('(isset($context[')
63                     ->string($name)
64                     ->raw(']) ? $context[')
65                     ->string($name)
66                     ->raw('] : ')
67                 ;
68
69                 if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) {
70                     $compiler->raw('null)');
71                 } else {
72                     $compiler->raw('$this->getContext($context, ')->string($name)->raw('))');
73                 }
74             } else {
75                 $compiler
76                     ->raw('$this->getContext($context, ')
77                     ->string($name)
78                 ;
79
80                 if ($this->getAttribute('ignore_strict_check')) {
81                     $compiler->raw(', true');
82                 }
83
84                 $compiler
85                     ->raw(')')
86                 ;
87             }
88         }
89     }
90
91     public function isSpecial()
92     {
93         return isset($this->specialVars[$this->getAttribute('name')]);
94     }
95
96     public function isSimple()
97     {
98         return !$this->isSpecial() && !$this->getAttribute('is_defined_test');
99     }
100 }