4 * This file is part of Twig.
6 * (c) 2009 Fabien Potencier
7 * (c) 2009 Armin Ronacher
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 if ($this->getAttribute('is_defined_test')) {
30 if ($this->isSpecial()) {
31 $compiler->repr(true);
33 $compiler->raw('array_key_exists(')->repr($name)->raw(', $context)');
35 } elseif ($this->isSpecial()) {
36 $compiler->raw($this->specialVars
[$name]);
37 } elseif ($this->getAttribute('always_defined')) {
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
50 ->raw('(isset($context[')
52 ->raw(']) ? $context[')
57 if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) {
58 $compiler->raw('null)');
60 $compiler->raw('$this->getContext($context, ')->string($name)->raw('))');
64 ->raw('$this->getContext($context, ')
68 if ($this->getAttribute('ignore_strict_check')) {
69 $compiler->raw(', true');
79 public function isSpecial()
81 return isset($this->specialVars
[$this->getAttribute('name')]);
84 public function isSimple()
86 return !$this->isSpecial() && !$this->getAttribute('is_defined_test');