diff options
Diffstat (limited to 'inc/Twig/Node/Set.php')
-rw-r--r-- | inc/Twig/Node/Set.php | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/inc/Twig/Node/Set.php b/inc/Twig/Node/Set.php new file mode 100644 index 00000000..4c9c16ce --- /dev/null +++ b/inc/Twig/Node/Set.php | |||
@@ -0,0 +1,101 @@ | |||
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 | * @author Fabien Potencier <fabien@symfony.com> | ||
16 | */ | ||
17 | class Twig_Node_Set extends Twig_Node | ||
18 | { | ||
19 | public function __construct($capture, Twig_NodeInterface $names, Twig_NodeInterface $values, $lineno, $tag = null) | ||
20 | { | ||
21 | parent::__construct(array('names' => $names, 'values' => $values), array('capture' => $capture, 'safe' => false), $lineno, $tag); | ||
22 | |||
23 | /* | ||
24 | * Optimizes the node when capture is used for a large block of text. | ||
25 | * | ||
26 | * {% set foo %}foo{% endset %} is compiled to $context['foo'] = new Twig_Markup("foo"); | ||
27 | */ | ||
28 | if ($this->getAttribute('capture')) { | ||
29 | $this->setAttribute('safe', true); | ||
30 | |||
31 | $values = $this->getNode('values'); | ||
32 | if ($values instanceof Twig_Node_Text) { | ||
33 | $this->setNode('values', new Twig_Node_Expression_Constant($values->getAttribute('data'), $values->getLine())); | ||
34 | $this->setAttribute('capture', false); | ||
35 | } | ||
36 | } | ||
37 | } | ||
38 | |||
39 | /** | ||
40 | * Compiles the node to PHP. | ||
41 | * | ||
42 | * @param Twig_Compiler A Twig_Compiler instance | ||
43 | */ | ||
44 | public function compile(Twig_Compiler $compiler) | ||
45 | { | ||
46 | $compiler->addDebugInfo($this); | ||
47 | |||
48 | if (count($this->getNode('names')) > 1) { | ||
49 | $compiler->write('list('); | ||
50 | foreach ($this->getNode('names') as $idx => $node) { | ||
51 | if ($idx) { | ||
52 | $compiler->raw(', '); | ||
53 | } | ||
54 | |||
55 | $compiler->subcompile($node); | ||
56 | } | ||
57 | $compiler->raw(')'); | ||
58 | } else { | ||
59 | if ($this->getAttribute('capture')) { | ||
60 | $compiler | ||
61 | ->write("ob_start();\n") | ||
62 | ->subcompile($this->getNode('values')) | ||
63 | ; | ||
64 | } | ||
65 | |||
66 | $compiler->subcompile($this->getNode('names'), false); | ||
67 | |||
68 | if ($this->getAttribute('capture')) { | ||
69 | $compiler->raw(" = ('' === \$tmp = ob_get_clean()) ? '' : new Twig_Markup(\$tmp, \$this->env->getCharset())"); | ||
70 | } | ||
71 | } | ||
72 | |||
73 | if (!$this->getAttribute('capture')) { | ||
74 | $compiler->raw(' = '); | ||
75 | |||
76 | if (count($this->getNode('names')) > 1) { | ||
77 | $compiler->write('array('); | ||
78 | foreach ($this->getNode('values') as $idx => $value) { | ||
79 | if ($idx) { | ||
80 | $compiler->raw(', '); | ||
81 | } | ||
82 | |||
83 | $compiler->subcompile($value); | ||
84 | } | ||
85 | $compiler->raw(')'); | ||
86 | } else { | ||
87 | if ($this->getAttribute('safe')) { | ||
88 | $compiler | ||
89 | ->raw("('' === \$tmp = ") | ||
90 | ->subcompile($this->getNode('values')) | ||
91 | ->raw(") ? '' : new Twig_Markup(\$tmp, \$this->env->getCharset())") | ||
92 | ; | ||
93 | } else { | ||
94 | $compiler->subcompile($this->getNode('values')); | ||
95 | } | ||
96 | } | ||
97 | } | ||
98 | |||
99 | $compiler->raw(";\n"); | ||
100 | } | ||
101 | } | ||