Dotclear

source: inc/libs/twig/Node/Set.php @ 991:e42f791e0975

Revision 991:e42f791e0975, 3.2 KB checked in by Dsls <dsls@…>, 13 years ago (diff)

New twig branch. 1st step : add twig 1.11.1

Line 
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 */
18class 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}
Note: See TracBrowser for help on using the repository browser.

Sites map