Project

General

Profile

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), $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
        } else {
38
            // remove the non-PHP 5.4 version when PHP 5.3 support is dropped
39
            // as the non-optimized version is just a workaround for slow ternary operator
40
            // when the context has a lot of variables
41
            if (version_compare(phpversion(), '5.4.0RC1', '>=') && ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables())) {
42
                // PHP 5.4 ternary operator performance was optimized
43
                $compiler
44
                    ->raw('(isset($context[')
45
                    ->string($name)
46
                    ->raw(']) ? $context[')
47
                    ->string($name)
48
                    ->raw('] : null)')
49
                ;
50
            } else {
51
                $compiler
52
                    ->raw('$this->getContext($context, ')
53
                    ->string($name)
54
                ;
55

    
56
                if ($this->getAttribute('ignore_strict_check')) {
57
                    $compiler->raw(', true');
58
                }
59

    
60
                $compiler
61
                    ->raw(')')
62
                ;
63
            }
64
        }
65
    }
66

    
67
    public function isSpecial()
68
    {
69
        return isset($this->specialVars[$this->getAttribute('name')]);
70
    }
71

    
72
    public function isSimple()
73
    {
74
        return !$this->isSpecial() && !$this->getAttribute('is_defined_test');
75
    }
76
}
(12-12/16)