]>
Commit | Line | Data |
---|---|---|
a4565e88 NL |
1 | <?php |
2 | ||
3 | /* | |
4 | * This file is part of Twig. | |
5 | * | |
6 | * (c) 2009 Fabien Potencier | |
7 | * (c) 2009 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 | if ($this->getAttribute('is_defined_test')) { | |
30 | if ($this->isSpecial()) { | |
31 | $compiler->repr(true); | |
32 | } else { | |
33 | $compiler->raw('array_key_exists(')->repr($name)->raw(', $context)'); | |
34 | } | |
35 | } elseif ($this->isSpecial()) { | |
36 | $compiler->raw($this->specialVars[$name]); | |
37 | } elseif ($this->getAttribute('always_defined')) { | |
38 | $compiler | |
39 | ->raw('$context[') | |
40 | ->string($name) | |
41 | ->raw(']') | |
42 | ; | |
43 | } else { | |
44 | // remove the non-PHP 5.4 version when PHP 5.3 support is dropped | |
45 | // as the non-optimized version is just a workaround for slow ternary operator | |
46 | // when the context has a lot of variables | |
47 | if (version_compare(phpversion(), '5.4.0RC1', '>=')) { | |
48 | // PHP 5.4 ternary operator performance was optimized | |
49 | $compiler | |
50 | ->raw('(isset($context[') | |
51 | ->string($name) | |
52 | ->raw(']) ? $context[') | |
53 | ->string($name) | |
54 | ->raw('] : ') | |
55 | ; | |
56 | ||
57 | if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) { | |
58 | $compiler->raw('null)'); | |
59 | } else { | |
60 | $compiler->raw('$this->getContext($context, ')->string($name)->raw('))'); | |
61 | } | |
62 | } else { | |
63 | $compiler | |
64 | ->raw('$this->getContext($context, ') | |
65 | ->string($name) | |
66 | ; | |
67 | ||
68 | if ($this->getAttribute('ignore_strict_check')) { | |
69 | $compiler->raw(', true'); | |
70 | } | |
71 | ||
72 | $compiler | |
73 | ->raw(')') | |
74 | ; | |
75 | } | |
76 | } | |
77 | } | |
78 | ||
79 | public function isSpecial() | |
80 | { | |
81 | return isset($this->specialVars[$this->getAttribute('name')]); | |
82 | } | |
83 | ||
84 | public function isSimple() | |
85 | { | |
86 | return !$this->isSpecial() && !$this->getAttribute('is_defined_test'); | |
87 | } | |
88 | } |