Name.php (2976B)
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 $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 // remove the non-PHP 5.4 version when PHP 5.3 support is dropped 47 // as the non-optimized version is just a workaround for slow ternary operator 48 // when the context has a lot of variables 49 if (PHP_VERSION_ID >= 50400) { 50 // PHP 5.4 ternary operator performance was optimized 51 $compiler 52 ->raw('(isset($context[') 53 ->string($name) 54 ->raw(']) ? $context[') 55 ->string($name) 56 ->raw('] : ') 57 ; 58 59 if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) { 60 $compiler->raw('null)'); 61 } else { 62 $compiler->raw('$this->getContext($context, ')->string($name)->raw('))'); 63 } 64 } else { 65 $compiler 66 ->raw('$this->getContext($context, ') 67 ->string($name) 68 ; 69 70 if ($this->getAttribute('ignore_strict_check')) { 71 $compiler->raw(', true'); 72 } 73 74 $compiler 75 ->raw(')') 76 ; 77 } 78 } 79 } 80 81 public function isSpecial() 82 { 83 return isset($this->specialVars[$this->getAttribute('name')]); 84 } 85 86 public function isSimple() 87 { 88 return !$this->isSpecial() && !$this->getAttribute('is_defined_test'); 89 } 90 }