4 * This file is part of Twig.
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
12 class Twig_Node_Expression_Name extends Twig_Node_Expression
14 protected $specialVars = array(
16 '_context' => '$context',
17 '_charset' => '$this->env->getCharset()',
20 public function __construct($name, $lineno)
22 parent::__construct(array(), array('name' => $name, 'is_defined_test' => false, 'ignore_strict_check' => false, 'always_defined' => false), $lineno);
25 public function compile(Twig_Compiler $compiler)
27 $name = $this->getAttribute('name');
29 $compiler->addDebugInfo($this);
31 if ($this->getAttribute('is_defined_test')) {
32 if ($this->isSpecial()) {
33 $compiler->repr(true);
35 $compiler->raw('array_key_exists(')->repr($name)->raw(', $context)');
37 } elseif ($this->isSpecial()) {
38 $compiler->raw($this->specialVars[$name]);
39 } elseif ($this->getAttribute('always_defined')) {
46 if (PHP_VERSION_ID >= 70000) {
47 // use PHP 7 null coalescing operator
54 if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) {
55 $compiler->raw('null)');
57 $compiler->raw('$this->getContext($context, ')->string($name)->raw('))');
59 } elseif (PHP_VERSION_ID >= 50400) {
60 // PHP 5.4 ternary operator performance was optimized
62 ->raw('(isset($context[')
64 ->raw(']) ? $context[')
69 if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) {
70 $compiler->raw('null)');
72 $compiler->raw('$this->getContext($context, ')->string($name)->raw('))');
76 ->raw('$this->getContext($context, ')
80 if ($this->getAttribute('ignore_strict_check')) {
81 $compiler->raw(', true');
91 public function isSpecial()
93 return isset($this->specialVars[$this->getAttribute('name')]);
96 public function isSimple()
98 return !$this->isSpecial() && !$this->getAttribute('is_defined_test');