Project

General

Profile

« Previous | Next » 

Revision 1862

Added by darkviper over 11 years ago

updated Twig template engine to stable version 1.12.2

View differences:

branches/2.8.x/wb/include/Twig/Node/Print.php
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

  
13
/**
14
 * Represents a node that outputs an expression.
15
 *
16
 * @package    twig
17
 * @author     Fabien Potencier <fabien@symfony.com>
18
 */
19
class Twig_Node_Print extends Twig_Node implements Twig_NodeOutputInterface
20
{
21
    public function __construct(Twig_Node_Expression $expr, $lineno, $tag = null)
22
    {
23
        parent::__construct(array('expr' => $expr), array(), $lineno, $tag);
24
    }
25

  
26
    /**
27
     * Compiles the node to PHP.
28
     *
29
     * @param Twig_Compiler A Twig_Compiler instance
30
     */
31
    public function compile(Twig_Compiler $compiler)
32
    {
33
        $compiler
34
            ->addDebugInfo($this)
35
            ->write('echo ')
36
            ->subcompile($this->getNode('expr'))
37
            ->raw(";\n")
38
        ;
39
    }
40
}
41 0

  
branches/2.8.x/wb/include/Twig/Node/SandboxedPrint.php
1
<?php
2

  
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2010 Fabien Potencier
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

  
12
/**
13
 * Twig_Node_SandboxedPrint adds a check for the __toString() method
14
 * when the variable is an object and the sandbox is activated.
15
 *
16
 * When there is a simple Print statement, like {{ article }},
17
 * and if the sandbox is enabled, we need to check that the __toString()
18
 * method is allowed if 'article' is an object.
19
 *
20
 * @package    twig
21
 * @author     Fabien Potencier <fabien@symfony.com>
22
 */
23
class Twig_Node_SandboxedPrint extends Twig_Node_Print
24
{
25
    public function __construct(Twig_Node_Expression $expr, $lineno, $tag = null)
26
    {
27
        parent::__construct($expr, $lineno, $tag);
28
    }
29

  
30
    /**
31
     * Compiles the node to PHP.
32
     *
33
     * @param Twig_Compiler A Twig_Compiler instance
34
     */
35
    public function compile(Twig_Compiler $compiler)
36
    {
37
        $compiler
38
            ->addDebugInfo($this)
39
            ->write('echo $this->env->getExtension(\'sandbox\')->ensureToStringAllowed(')
40
            ->subcompile($this->getNode('expr'))
41
            ->raw(");\n")
42
        ;
43
    }
44

  
45
    /**
46
     * Removes node filters.
47
     *
48
     * This is mostly needed when another visitor adds filters (like the escaper one).
49
     *
50
     * @param Twig_Node $node A Node
51
     */
52
    protected function removeNodeFilter($node)
53
    {
54
        if ($node instanceof Twig_Node_Expression_Filter) {
55
            return $this->removeNodeFilter($node->getNode('node'));
56
        }
57

  
58
        return $node;
59
    }
60
}
61 0

  
branches/2.8.x/wb/include/Twig/Node/Embed.php
1
<?php
2

  
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2012 Fabien Potencier
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

  
12
/**
13
 * Represents an embed node.
14
 *
15
 * @package    twig
16
 * @author     Fabien Potencier <fabien@symfony.com>
17
 */
18
class Twig_Node_Embed extends Twig_Node_Include
19
{
20
    // we don't inject the module to avoid node visitors to traverse it twice (as it will be already visited in the main module)
21
    public function __construct($filename, $index, Twig_Node_Expression $variables = null, $only = false, $ignoreMissing = false, $lineno, $tag = null)
22
    {
23
        parent::__construct(new Twig_Node_Expression_Constant('not_used', $lineno), $variables, $only, $ignoreMissing, $lineno, $tag);
24

  
25
        $this->setAttribute('filename', $filename);
26
        $this->setAttribute('index', $index);
27
    }
28

  
29
    protected function addGetTemplate(Twig_Compiler $compiler)
30
    {
31
        $compiler
32
            ->write("\$this->env->loadTemplate(")
33
            ->string($this->getAttribute('filename'))
34
            ->raw(', ')
35
            ->string($this->getAttribute('index'))
36
            ->raw(")")
37
        ;
38
    }
39
}
40 0

  
branches/2.8.x/wb/include/Twig/Node/Import.php
1
<?php
2

  
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2009 Fabien Potencier
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

  
12
/**
13
 * Represents an import node.
14
 *
15
 * @package    twig
16
 * @author     Fabien Potencier <fabien@symfony.com>
17
 */
18
class Twig_Node_Import extends Twig_Node
19
{
20
    public function __construct(Twig_Node_Expression $expr, Twig_Node_Expression $var, $lineno, $tag = null)
21
    {
22
        parent::__construct(array('expr' => $expr, 'var' => $var), array(), $lineno, $tag);
23
    }
24

  
25
    /**
26
     * Compiles the node to PHP.
27
     *
28
     * @param Twig_Compiler A Twig_Compiler instance
29
     */
30
    public function compile(Twig_Compiler $compiler)
31
    {
32
        $compiler
33
            ->addDebugInfo($this)
34
            ->write('')
35
            ->subcompile($this->getNode('var'))
36
            ->raw(' = ')
37
        ;
38

  
39
        if ($this->getNode('expr') instanceof Twig_Node_Expression_Name && '_self' === $this->getNode('expr')->getAttribute('name')) {
40
            $compiler->raw("\$this");
41
        } else {
42
            $compiler
43
                ->raw('$this->env->loadTemplate(')
44
                ->subcompile($this->getNode('expr'))
45
                ->raw(")")
46
            ;
47
        }
48

  
49
        $compiler->raw(";\n");
50
    }
51
}
52 0

  
branches/2.8.x/wb/include/Twig/Node/For.php
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

  
13
/**
14
 * Represents a for node.
15
 *
16
 * @package    twig
17
 * @author     Fabien Potencier <fabien@symfony.com>
18
 */
19
class Twig_Node_For extends Twig_Node
20
{
21
    protected $loop;
22

  
23
    public function __construct(Twig_Node_Expression_AssignName $keyTarget, Twig_Node_Expression_AssignName $valueTarget, Twig_Node_Expression $seq, Twig_Node_Expression $ifexpr = null, Twig_NodeInterface $body, Twig_NodeInterface $else = null, $lineno, $tag = null)
24
    {
25
        $body = new Twig_Node(array($body, $this->loop = new Twig_Node_ForLoop($lineno, $tag)));
26

  
27
        if (null !== $ifexpr) {
28
            $body = new Twig_Node_If(new Twig_Node(array($ifexpr, $body)), null, $lineno, $tag);
29
        }
30

  
31
        parent::__construct(array('key_target' => $keyTarget, 'value_target' => $valueTarget, 'seq' => $seq, 'body' => $body, 'else' => $else), array('with_loop' => true, 'ifexpr' => null !== $ifexpr), $lineno, $tag);
32
    }
33

  
34
    /**
35
     * Compiles the node to PHP.
36
     *
37
     * @param Twig_Compiler A Twig_Compiler instance
38
     */
39
    public function compile(Twig_Compiler $compiler)
40
    {
41
        $compiler
42
            ->addDebugInfo($this)
43
            // the (array) cast bypasses a PHP 5.2.6 bug
44
            ->write("\$context['_parent'] = (array) \$context;\n")
45
            ->write("\$context['_seq'] = twig_ensure_traversable(")
46
            ->subcompile($this->getNode('seq'))
47
            ->raw(");\n")
48
        ;
49

  
50
        if (null !== $this->getNode('else')) {
51
            $compiler->write("\$context['_iterated'] = false;\n");
52
        }
53

  
54
        if ($this->getAttribute('with_loop')) {
55
            $compiler
56
                ->write("\$context['loop'] = array(\n")
57
                ->write("  'parent' => \$context['_parent'],\n")
58
                ->write("  'index0' => 0,\n")
59
                ->write("  'index'  => 1,\n")
60
                ->write("  'first'  => true,\n")
61
                ->write(");\n")
62
            ;
63

  
64
            if (!$this->getAttribute('ifexpr')) {
65
                $compiler
66
                    ->write("if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable)) {\n")
67
                    ->indent()
68
                    ->write("\$length = count(\$context['_seq']);\n")
69
                    ->write("\$context['loop']['revindex0'] = \$length - 1;\n")
70
                    ->write("\$context['loop']['revindex'] = \$length;\n")
71
                    ->write("\$context['loop']['length'] = \$length;\n")
72
                    ->write("\$context['loop']['last'] = 1 === \$length;\n")
73
                    ->outdent()
74
                    ->write("}\n")
75
                ;
76
            }
77
        }
78

  
79
        $this->loop->setAttribute('else', null !== $this->getNode('else'));
80
        $this->loop->setAttribute('with_loop', $this->getAttribute('with_loop'));
81
        $this->loop->setAttribute('ifexpr', $this->getAttribute('ifexpr'));
82

  
83
        $compiler
84
            ->write("foreach (\$context['_seq'] as ")
85
            ->subcompile($this->getNode('key_target'))
86
            ->raw(" => ")
87
            ->subcompile($this->getNode('value_target'))
88
            ->raw(") {\n")
89
            ->indent()
90
            ->subcompile($this->getNode('body'))
91
            ->outdent()
92
            ->write("}\n")
93
        ;
94

  
95
        if (null !== $this->getNode('else')) {
96
            $compiler
97
                ->write("if (!\$context['_iterated']) {\n")
98
                ->indent()
99
                ->subcompile($this->getNode('else'))
100
                ->outdent()
101
                ->write("}\n")
102
            ;
103
        }
104

  
105
        $compiler->write("\$_parent = \$context['_parent'];\n");
106

  
107
        // remove some "private" loop variables (needed for nested loops)
108
        $compiler->write('unset($context[\'_seq\'], $context[\'_iterated\'], $context[\''.$this->getNode('key_target')->getAttribute('name').'\'], $context[\''.$this->getNode('value_target')->getAttribute('name').'\'], $context[\'_parent\'], $context[\'loop\']);'."\n");
109

  
110
        // keep the values set in the inner context for variables defined in the outer context
111
        $compiler->write("\$context = array_merge(\$_parent, array_intersect_key(\$context, \$_parent));\n");
112
    }
113
}
114 0

  
branches/2.8.x/wb/include/Twig/Node/Include.php
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

  
13
/**
14
 * Represents an include node.
15
 *
16
 * @package    twig
17
 * @author     Fabien Potencier <fabien@symfony.com>
18
 */
19
class Twig_Node_Include extends Twig_Node implements Twig_NodeOutputInterface
20
{
21
    public function __construct(Twig_Node_Expression $expr, Twig_Node_Expression $variables = null, $only = false, $ignoreMissing = false, $lineno, $tag = null)
22
    {
23
        parent::__construct(array('expr' => $expr, 'variables' => $variables), array('only' => (Boolean) $only, 'ignore_missing' => (Boolean) $ignoreMissing), $lineno, $tag);
24
    }
25

  
26
    /**
27
     * Compiles the node to PHP.
28
     *
29
     * @param Twig_Compiler A Twig_Compiler instance
30
     */
31
    public function compile(Twig_Compiler $compiler)
32
    {
33
        $compiler->addDebugInfo($this);
34

  
35
        if ($this->getAttribute('ignore_missing')) {
36
            $compiler
37
                ->write("try {\n")
38
                ->indent()
39
            ;
40
        }
41

  
42
        $this->addGetTemplate($compiler);
43

  
44
        $compiler->raw('->display(');
45

  
46
        $this->addTemplateArguments($compiler);
47

  
48
        $compiler->raw(");\n");
49

  
50
        if ($this->getAttribute('ignore_missing')) {
51
            $compiler
52
                ->outdent()
53
                ->write("} catch (Twig_Error_Loader \$e) {\n")
54
                ->indent()
55
                ->write("// ignore missing template\n")
56
                ->outdent()
57
                ->write("}\n\n")
58
            ;
59
        }
60
    }
61

  
62
    protected function addGetTemplate(Twig_Compiler $compiler)
63
    {
64
        if ($this->getNode('expr') instanceof Twig_Node_Expression_Constant) {
65
            $compiler
66
                ->write("\$this->env->loadTemplate(")
67
                ->subcompile($this->getNode('expr'))
68
                ->raw(")")
69
            ;
70
        } else {
71
            $compiler
72
                ->write("\$template = \$this->env->resolveTemplate(")
73
                ->subcompile($this->getNode('expr'))
74
                ->raw(");\n")
75
                ->write('$template')
76
            ;
77
        }
78
    }
79

  
80
    protected function addTemplateArguments(Twig_Compiler $compiler)
81
    {
82
        if (false === $this->getAttribute('only')) {
83
            if (null === $this->getNode('variables')) {
84
                $compiler->raw('$context');
85
            } else {
86
                $compiler
87
                    ->raw('array_merge($context, ')
88
                    ->subcompile($this->getNode('variables'))
89
                    ->raw(')')
90
                ;
91
            }
92
        } else {
93
            if (null === $this->getNode('variables')) {
94
                $compiler->raw('array()');
95
            } else {
96
                $compiler->subcompile($this->getNode('variables'));
97
            }
98
        }
99
    }
100
}
101 0

  
branches/2.8.x/wb/include/Twig/Node/Block.php
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

  
13
/**
14
 * Represents a block node.
15
 *
16
 * @package    twig
17
 * @author     Fabien Potencier <fabien@symfony.com>
18
 */
19
class Twig_Node_Block extends Twig_Node
20
{
21
    public function __construct($name, Twig_NodeInterface $body, $lineno, $tag = null)
22
    {
23
        parent::__construct(array('body' => $body), array('name' => $name), $lineno, $tag);
24
    }
25

  
26
    /**
27
     * Compiles the node to PHP.
28
     *
29
     * @param Twig_Compiler A Twig_Compiler instance
30
     */
31
    public function compile(Twig_Compiler $compiler)
32
    {
33
        $compiler
34
            ->addDebugInfo($this)
35
            ->write(sprintf("public function block_%s(\$context, array \$blocks = array())\n", $this->getAttribute('name')), "{\n")
36
            ->indent()
37
        ;
38

  
39
        $compiler
40
            ->subcompile($this->getNode('body'))
41
            ->outdent()
42
            ->write("}\n\n")
43
        ;
44
    }
45
}
46 0

  
branches/2.8.x/wb/include/Twig/Node/ForLoop.php
1
<?php
2

  
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2011 Fabien Potencier
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

  
12
/**
13
 * Internal node used by the for node.
14
 *
15
 * @package    twig
16
 * @author     Fabien Potencier <fabien@symfony.com>
17
 */
18
class Twig_Node_ForLoop extends Twig_Node
19
{
20
    public function __construct($lineno, $tag = null)
21
    {
22
        parent::__construct(array(), array('with_loop' => false, 'ifexpr' => false, 'else' => false), $lineno, $tag);
23
    }
24

  
25
    /**
26
     * Compiles the node to PHP.
27
     *
28
     * @param Twig_Compiler A Twig_Compiler instance
29
     */
30
    public function compile(Twig_Compiler $compiler)
31
    {
32
        if ($this->getAttribute('else')) {
33
            $compiler->write("\$context['_iterated'] = true;\n");
34
        }
35

  
36
        if ($this->getAttribute('with_loop')) {
37
            $compiler
38
                ->write("++\$context['loop']['index0'];\n")
39
                ->write("++\$context['loop']['index'];\n")
40
                ->write("\$context['loop']['first'] = false;\n")
41
            ;
42

  
43
            if (!$this->getAttribute('ifexpr')) {
44
                $compiler
45
                    ->write("if (isset(\$context['loop']['length'])) {\n")
46
                    ->indent()
47
                    ->write("--\$context['loop']['revindex0'];\n")
48
                    ->write("--\$context['loop']['revindex'];\n")
49
                    ->write("\$context['loop']['last'] = 0 === \$context['loop']['revindex0'];\n")
50
                    ->outdent()
51
                    ->write("}\n")
52
                ;
53
            }
54
        }
55
    }
56
}
57 0

  
branches/2.8.x/wb/include/Twig/Node/Sandbox.php
1
<?php
2

  
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2010 Fabien Potencier
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

  
12
/**
13
 * Represents a sandbox node.
14
 *
15
 * @package    twig
16
 * @author     Fabien Potencier <fabien@symfony.com>
17
 */
18
class Twig_Node_Sandbox extends Twig_Node
19
{
20
    public function __construct(Twig_NodeInterface $body, $lineno, $tag = null)
21
    {
22
        parent::__construct(array('body' => $body), array(), $lineno, $tag);
23
    }
24

  
25
    /**
26
     * Compiles the node to PHP.
27
     *
28
     * @param Twig_Compiler A Twig_Compiler instance
29
     */
30
    public function compile(Twig_Compiler $compiler)
31
    {
32
        $compiler
33
            ->addDebugInfo($this)
34
            ->write("\$sandbox = \$this->env->getExtension('sandbox');\n")
35
            ->write("if (!\$alreadySandboxed = \$sandbox->isSandboxed()) {\n")
36
            ->indent()
37
            ->write("\$sandbox->enableSandbox();\n")
38
            ->outdent()
39
            ->write("}\n")
40
            ->subcompile($this->getNode('body'))
41
            ->write("if (!\$alreadySandboxed) {\n")
42
            ->indent()
43
            ->write("\$sandbox->disableSandbox();\n")
44
            ->outdent()
45
            ->write("}\n")
46
        ;
47
    }
48
}
49 0

  
branches/2.8.x/wb/include/Twig/Node/AutoEscape.php
1
<?php
2

  
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2009 Fabien Potencier
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

  
12
/**
13
 * Represents an autoescape node.
14
 *
15
 * The value is the escaping strategy (can be html, js, ...)
16
 *
17
 * The true value is equivalent to html.
18
 *
19
 * If autoescaping is disabled, then the value is false.
20
 *
21
 * @package    twig
22
 * @author     Fabien Potencier <fabien@symfony.com>
23
 */
24
class Twig_Node_AutoEscape extends Twig_Node
25
{
26
    public function __construct($value, Twig_NodeInterface $body, $lineno, $tag = 'autoescape')
27
    {
28
        parent::__construct(array('body' => $body), array('value' => $value), $lineno, $tag);
29
    }
30

  
31
    /**
32
     * Compiles the node to PHP.
33
     *
34
     * @param Twig_Compiler A Twig_Compiler instance
35
     */
36
    public function compile(Twig_Compiler $compiler)
37
    {
38
        $compiler->subcompile($this->getNode('body'));
39
    }
40
}
41 0

  
branches/2.8.x/wb/include/Twig/Node/Flush.php
1
<?php
2

  
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2011 Fabien Potencier
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

  
12
/**
13
 * Represents a flush node.
14
 *
15
 * @package    twig
16
 * @author     Fabien Potencier <fabien@symfony.com>
17
 */
18
class Twig_Node_Flush extends Twig_Node
19
{
20
    public function __construct($lineno, $tag)
21
    {
22
        parent::__construct(array(), array(), $lineno, $tag);
23
    }
24

  
25
    /**
26
     * Compiles the node to PHP.
27
     *
28
     * @param Twig_Compiler A Twig_Compiler instance
29
     */
30
    public function compile(Twig_Compiler $compiler)
31
    {
32
        $compiler
33
            ->addDebugInfo($this)
34
            ->write("flush();\n")
35
        ;
36
    }
37
}
38 0

  
branches/2.8.x/wb/include/Twig/Node/Text.php
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

  
13
/**
14
 * Represents a text node.
15
 *
16
 * @package    twig
17
 * @author     Fabien Potencier <fabien@symfony.com>
18
 */
19
class Twig_Node_Text extends Twig_Node implements Twig_NodeOutputInterface
20
{
21
    public function __construct($data, $lineno)
22
    {
23
        parent::__construct(array(), array('data' => $data), $lineno);
24
    }
25

  
26
    /**
27
     * Compiles the node to PHP.
28
     *
29
     * @param Twig_Compiler A Twig_Compiler instance
30
     */
31
    public function compile(Twig_Compiler $compiler)
32
    {
33
        $compiler
34
            ->addDebugInfo($this)
35
            ->write('echo ')
36
            ->string($this->getAttribute('data'))
37
            ->raw(";\n")
38
        ;
39
    }
40
}
41 0

  
branches/2.8.x/wb/include/Twig/Node/SetTemp.php
1
<?php
2

  
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2011 Fabien Potencier
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

  
12
class Twig_Node_SetTemp extends Twig_Node
13
{
14
    public function __construct($name, $lineno)
15
    {
16
        parent::__construct(array(), array('name' => $name), $lineno);
17
    }
18

  
19
    public function compile(Twig_Compiler $compiler)
20
    {
21
        $name = $this->getAttribute('name');
22
        $compiler
23
            ->addDebugInfo($this)
24
            ->write('if (isset($context[')
25
            ->string($name)
26
            ->raw('])) { $_')
27
            ->raw($name)
28
            ->raw('_ = $context[')
29
            ->repr($name)
30
            ->raw(']; } else { $_')
31
            ->raw($name)
32
            ->raw("_ = null; }\n")
33
        ;
34
    }
35
}
36 0

  
branches/2.8.x/wb/include/Twig/Node/SandboxedModule.php
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

  
13
/**
14
 * Represents a module node.
15
 *
16
 * @package    twig
17
 * @author     Fabien Potencier <fabien@symfony.com>
18
 */
19
class Twig_Node_SandboxedModule extends Twig_Node_Module
20
{
21
    protected $usedFilters;
22
    protected $usedTags;
23
    protected $usedFunctions;
24

  
25
    public function __construct(Twig_Node_Module $node, array $usedFilters, array $usedTags, array $usedFunctions)
26
    {
27
        parent::__construct($node->getNode('body'), $node->getNode('parent'), $node->getNode('blocks'), $node->getNode('macros'), $node->getNode('traits'), $node->getAttribute('embedded_templates'), $node->getAttribute('filename'), $node->getLine(), $node->getNodeTag());
28

  
29
        $this->setAttribute('index', $node->getAttribute('index'));
30

  
31
        $this->usedFilters = $usedFilters;
32
        $this->usedTags = $usedTags;
33
        $this->usedFunctions = $usedFunctions;
34
    }
35

  
36
    protected function compileDisplayBody(Twig_Compiler $compiler)
37
    {
38
        $compiler->write("\$this->checkSecurity();\n");
39

  
40
        parent::compileDisplayBody($compiler);
41
    }
42

  
43
    protected function compileDisplayFooter(Twig_Compiler $compiler)
44
    {
45
        parent::compileDisplayFooter($compiler);
46

  
47
        $compiler
48
            ->write("protected function checkSecurity()\n", "{\n")
49
            ->indent()
50
            ->write("\$this->env->getExtension('sandbox')->checkSecurity(\n")
51
            ->indent()
52
            ->write(!$this->usedTags ? "array(),\n" : "array('".implode('\', \'', $this->usedTags)."'),\n")
53
            ->write(!$this->usedFilters ? "array(),\n" : "array('".implode('\', \'', $this->usedFilters)."'),\n")
54
            ->write(!$this->usedFunctions ? "array()\n" : "array('".implode('\', \'', $this->usedFunctions)."')\n")
55
            ->outdent()
56
            ->write(");\n")
57
            ->outdent()
58
            ->write("}\n\n")
59
        ;
60
    }
61
}
62 0

  
branches/2.8.x/wb/include/Twig/Node/Do.php
1
<?php
2

  
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2011 Fabien Potencier
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

  
12
/**
13
 * Represents a do node.
14
 *
15
 * @package    twig
16
 * @author     Fabien Potencier <fabien@symfony.com>
17
 */
18
class Twig_Node_Do extends Twig_Node
19
{
20
    public function __construct(Twig_Node_Expression $expr, $lineno, $tag = null)
21
    {
22
        parent::__construct(array('expr' => $expr), array(), $lineno, $tag);
23
    }
24

  
25
    /**
26
     * Compiles the node to PHP.
27
     *
28
     * @param Twig_Compiler A Twig_Compiler instance
29
     */
30
    public function compile(Twig_Compiler $compiler)
31
    {
32
        $compiler
33
            ->addDebugInfo($this)
34
            ->write('')
35
            ->subcompile($this->getNode('expr'))
36
            ->raw(";\n")
37
        ;
38
    }
39
}
40 0

  
branches/2.8.x/wb/include/Twig/Node/Macro.php
1
<?php
2

  
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2009 Fabien Potencier
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

  
12
/**
13
 * Represents a macro node.
14
 *
15
 * @package    twig
16
 * @author     Fabien Potencier <fabien@symfony.com>
17
 */
18
class Twig_Node_Macro extends Twig_Node
19
{
20
    public function __construct($name, Twig_NodeInterface $body, Twig_NodeInterface $arguments, $lineno, $tag = null)
21
    {
22
        parent::__construct(array('body' => $body, 'arguments' => $arguments), array('name' => $name), $lineno, $tag);
23
    }
24

  
25
    /**
26
     * Compiles the node to PHP.
27
     *
28
     * @param Twig_Compiler A Twig_Compiler instance
29
     */
30
    public function compile(Twig_Compiler $compiler)
31
    {
32
        $arguments = array();
33
        foreach ($this->getNode('arguments') as $argument) {
34
            $arguments[] = '$_'.$argument->getAttribute('name').' = null';
35
        }
36

  
37
        $compiler
38
            ->addDebugInfo($this)
39
            ->write(sprintf("public function get%s(%s)\n", $this->getAttribute('name'), implode(', ', $arguments)), "{\n")
40
            ->indent()
41
        ;
42

  
43
        if (!count($this->getNode('arguments'))) {
44
            $compiler->write("\$context = \$this->env->getGlobals();\n\n");
45
        } else {
46
            $compiler
47
                ->write("\$context = \$this->env->mergeGlobals(array(\n")
48
                ->indent()
49
            ;
50

  
51
            foreach ($this->getNode('arguments') as $argument) {
52
                $compiler
53
                    ->write('')
54
                    ->string($argument->getAttribute('name'))
55
                    ->raw(' => $_'.$argument->getAttribute('name'))
56
                    ->raw(",\n")
57
                ;
58
            }
59

  
60
            $compiler
61
                ->outdent()
62
                ->write("));\n\n")
63
            ;
64
        }
65

  
66
        $compiler
67
            ->write("\$blocks = array();\n\n")
68
            ->write("ob_start();\n")
69
            ->write("try {\n")
70
            ->indent()
71
            ->subcompile($this->getNode('body'))
72
            ->outdent()
73
            ->write("} catch (Exception \$e) {\n")
74
            ->indent()
75
            ->write("ob_end_clean();\n\n")
76
            ->write("throw \$e;\n")
77
            ->outdent()
78
            ->write("}\n\n")
79
            ->write("return ('' === \$tmp = ob_get_clean()) ? '' : new Twig_Markup(\$tmp, \$this->env->getCharset());\n")
80
            ->outdent()
81
            ->write("}\n\n")
82
        ;
83
    }
84
}
85 0

  
branches/2.8.x/wb/include/Twig/Node/Set.php
1
<?php
2

  
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2010 Fabien Potencier
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

  
12
/**
13
 * Represents a set node.
14
 *
15
 * @package    twig
16
 * @author     Fabien Potencier <fabien@symfony.com>
17
 */
18
class Twig_Node_Set extends Twig_Node
19
{
20
    public function __construct($capture, Twig_NodeInterface $names, Twig_NodeInterface $values, $lineno, $tag = null)
21
    {
22
        parent::__construct(array('names' => $names, 'values' => $values), array('capture' => $capture, 'safe' => false), $lineno, $tag);
23

  
24
        /*
25
         * Optimizes the node when capture is used for a large block of text.
26
         *
27
         * {% set foo %}foo{% endset %} is compiled to $context['foo'] = new Twig_Markup("foo");
28
         */
29
        if ($this->getAttribute('capture')) {
30
            $this->setAttribute('safe', true);
31

  
32
            $values = $this->getNode('values');
33
            if ($values instanceof Twig_Node_Text) {
34
                $this->setNode('values', new Twig_Node_Expression_Constant($values->getAttribute('data'), $values->getLine()));
35
                $this->setAttribute('capture', false);
36
            }
37
        }
38
    }
39

  
40
    /**
41
     * Compiles the node to PHP.
42
     *
43
     * @param Twig_Compiler A Twig_Compiler instance
44
     */
45
    public function compile(Twig_Compiler $compiler)
46
    {
47
        $compiler->addDebugInfo($this);
48

  
49
        if (count($this->getNode('names')) > 1) {
50
            $compiler->write('list(');
51
            foreach ($this->getNode('names') as $idx => $node) {
52
                if ($idx) {
53
                    $compiler->raw(', ');
54
                }
55

  
56
                $compiler->subcompile($node);
57
            }
58
            $compiler->raw(')');
59
        } else {
60
            if ($this->getAttribute('capture')) {
61
                $compiler
62
                    ->write("ob_start();\n")
63
                    ->subcompile($this->getNode('values'))
64
                ;
65
            }
66

  
67
            $compiler->subcompile($this->getNode('names'), false);
68

  
69
            if ($this->getAttribute('capture')) {
70
                $compiler->raw(" = ('' === \$tmp = ob_get_clean()) ? '' : new Twig_Markup(\$tmp, \$this->env->getCharset())");
71
            }
72
        }
73

  
74
        if (!$this->getAttribute('capture')) {
75
            $compiler->raw(' = ');
76

  
77
            if (count($this->getNode('names')) > 1) {
78
                $compiler->write('array(');
79
                foreach ($this->getNode('values') as $idx => $value) {
80
                    if ($idx) {
81
                        $compiler->raw(', ');
82
                    }
83

  
84
                    $compiler->subcompile($value);
85
                }
86
                $compiler->raw(')');
87
            } else {
88
                if ($this->getAttribute('safe')) {
89
                    $compiler
90
                        ->raw("('' === \$tmp = ")
91
                        ->subcompile($this->getNode('values'))
92
                        ->raw(") ? '' : new Twig_Markup(\$tmp, \$this->env->getCharset())")
93
                    ;
94
                } else {
95
                    $compiler->subcompile($this->getNode('values'));
96
                }
97
            }
98
        }
99

  
100
        $compiler->raw(";\n");
101
    }
102
}
103 0

  
branches/2.8.x/wb/include/Twig/Node/Spaceless.php
1
<?php
2

  
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2010 Fabien Potencier
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

  
12
/**
13
 * Represents a spaceless node.
14
 *
15
 * It removes spaces between HTML tags.
16
 *
17
 * @package    twig
18
 * @author     Fabien Potencier <fabien@symfony.com>
19
 */
20
class Twig_Node_Spaceless extends Twig_Node
21
{
22
    public function __construct(Twig_NodeInterface $body, $lineno, $tag = 'spaceless')
23
    {
24
        parent::__construct(array('body' => $body), array(), $lineno, $tag);
25
    }
26

  
27
    /**
28
     * Compiles the node to PHP.
29
     *
30
     * @param Twig_Compiler A Twig_Compiler instance
31
     */
32
    public function compile(Twig_Compiler $compiler)
33
    {
34
        $compiler
35
            ->addDebugInfo($this)
36
            ->write("ob_start();\n")
37
            ->subcompile($this->getNode('body'))
38
            ->write("echo trim(preg_replace('/>\s+</', '><', ob_get_clean()));\n")
39
        ;
40
    }
41
}
42 0

  
branches/2.8.x/wb/include/Twig/Node/Body.php
1
<?php
2

  
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2011 Fabien Potencier
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

  
12
/**
13
 * Represents a body node.
14
 *
15
 * @package    twig
16
 * @author     Fabien Potencier <fabien@symfony.com>
17
 */
18
class Twig_Node_Body extends Twig_Node
19
{
20
}
21 0

  
branches/2.8.x/wb/include/Twig/Node/If.php
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

  
13
/**
14
 * Represents an if node.
15
 *
16
 * @package    twig
17
 * @author     Fabien Potencier <fabien@symfony.com>
18
 */
19
class Twig_Node_If extends Twig_Node
20
{
21
    public function __construct(Twig_NodeInterface $tests, Twig_NodeInterface $else = null, $lineno, $tag = null)
22
    {
23
        parent::__construct(array('tests' => $tests, 'else' => $else), array(), $lineno, $tag);
24
    }
25

  
26
    /**
27
     * Compiles the node to PHP.
28
     *
29
     * @param Twig_Compiler A Twig_Compiler instance
30
     */
31
    public function compile(Twig_Compiler $compiler)
32
    {
33
        $compiler->addDebugInfo($this);
34
        for ($i = 0; $i < count($this->getNode('tests')); $i += 2) {
35
            if ($i > 0) {
36
                $compiler
37
                    ->outdent()
38
                    ->write("} elseif (")
39
                ;
40
            } else {
41
                $compiler
42
                    ->write('if (')
43
                ;
44
            }
45

  
46
            $compiler
47
                ->subcompile($this->getNode('tests')->getNode($i))
48
                ->raw(") {\n")
49
                ->indent()
50
                ->subcompile($this->getNode('tests')->getNode($i + 1))
51
            ;
52
        }
53

  
54
        if ($this->hasNode('else') && null !== $this->getNode('else')) {
55
            $compiler
56
                ->outdent()
57
                ->write("} else {\n")
58
                ->indent()
59
                ->subcompile($this->getNode('else'))
60
            ;
61
        }
62

  
63
        $compiler
64
            ->outdent()
65
            ->write("}\n");
66
    }
67
}
68 0

  
branches/2.8.x/wb/include/Twig/Node/Expression/Conditional.php
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_Conditional extends Twig_Node_Expression
13
{
14
    public function __construct(Twig_Node_Expression $expr1, Twig_Node_Expression $expr2, Twig_Node_Expression $expr3, $lineno)
15
    {
16
        parent::__construct(array('expr1' => $expr1, 'expr2' => $expr2, 'expr3' => $expr3), array(), $lineno);
17
    }
18

  
19
    public function compile(Twig_Compiler $compiler)
20
    {
21
        $compiler
22
            ->raw('((')
23
            ->subcompile($this->getNode('expr1'))
24
            ->raw(') ? (')
25
            ->subcompile($this->getNode('expr2'))
26
            ->raw(') : (')
27
            ->subcompile($this->getNode('expr3'))
28
            ->raw('))')
29
        ;
30
    }
31
}
32 0

  
branches/2.8.x/wb/include/Twig/Node/Expression/AssignName.php
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
 */
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff