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 | $compiler |
---|
33 | ->addDebugInfo($this) |
---|
34 | ->write(sprintf("public function get%s(", $this->getAttribute('name'))) |
---|
35 | ; |
---|
36 | |
---|
37 | $count = count($this->getNode('arguments')); |
---|
38 | $pos = 0; |
---|
39 | foreach ($this->getNode('arguments') as $name => $default) { |
---|
40 | $compiler |
---|
41 | ->raw('$_'.$name.' = ') |
---|
42 | ->subcompile($default) |
---|
43 | ; |
---|
44 | |
---|
45 | if (++$pos < $count) { |
---|
46 | $compiler->raw(', '); |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | $compiler |
---|
51 | ->raw(")\n") |
---|
52 | ->write("{\n") |
---|
53 | ->indent() |
---|
54 | ; |
---|
55 | |
---|
56 | if (!count($this->getNode('arguments'))) { |
---|
57 | $compiler->write("\$context = \$this->env->getGlobals();\n\n"); |
---|
58 | } else { |
---|
59 | $compiler |
---|
60 | ->write("\$context = \$this->env->mergeGlobals(array(\n") |
---|
61 | ->indent() |
---|
62 | ; |
---|
63 | |
---|
64 | foreach ($this->getNode('arguments') as $name => $default) { |
---|
65 | $compiler |
---|
66 | ->write('') |
---|
67 | ->string($name) |
---|
68 | ->raw(' => $_'.$name) |
---|
69 | ->raw(",\n") |
---|
70 | ; |
---|
71 | } |
---|
72 | |
---|
73 | $compiler |
---|
74 | ->outdent() |
---|
75 | ->write("));\n\n") |
---|
76 | ; |
---|
77 | } |
---|
78 | |
---|
79 | $compiler |
---|
80 | ->write("\$blocks = array();\n\n") |
---|
81 | ->write("ob_start();\n") |
---|
82 | ->write("try {\n") |
---|
83 | ->indent() |
---|
84 | ->subcompile($this->getNode('body')) |
---|
85 | ->outdent() |
---|
86 | ->write("} catch (Exception \$e) {\n") |
---|
87 | ->indent() |
---|
88 | ->write("ob_end_clean();\n\n") |
---|
89 | ->write("throw \$e;\n") |
---|
90 | ->outdent() |
---|
91 | ->write("}\n\n") |
---|
92 | ->write("return ('' === \$tmp = ob_get_clean()) ? '' : new Twig_Markup(\$tmp, \$this->env->getCharset());\n") |
---|
93 | ->outdent() |
---|
94 | ->write("}\n\n") |
---|
95 | ; |
---|
96 | } |
---|
97 | } |
---|