|
0
|
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 |
->write("\$context = array_merge(\$this->env->getGlobals(), array(\n") |
|
|
42 |
->indent() |
|
|
43 |
; |
|
|
44 |
|
|
|
45 |
foreach ($this->getNode('arguments') as $argument) { |
|
|
46 |
$compiler |
|
|
47 |
->write('') |
|
|
48 |
->string($argument->getAttribute('name')) |
|
|
49 |
->raw(' => $'.$argument->getAttribute('name')) |
|
|
50 |
->raw(",\n") |
|
|
51 |
; |
|
|
52 |
} |
|
|
53 |
|
|
|
54 |
$compiler |
|
|
55 |
->outdent() |
|
|
56 |
->write("));\n\n") |
|
|
57 |
->write("ob_start();\n") |
|
|
58 |
->write("try {\n") |
|
|
59 |
->indent() |
|
|
60 |
->subcompile($this->getNode('body')) |
|
|
61 |
->outdent() |
|
|
62 |
->write("} catch(Exception \$e) {\n") |
|
|
63 |
->indent() |
|
|
64 |
->write("ob_end_clean();\n\n") |
|
|
65 |
->write("throw \$e;\n") |
|
|
66 |
->outdent() |
|
|
67 |
->write("}\n\n") |
|
|
68 |
->write("return ob_get_clean();\n") |
|
|
69 |
->outdent() |
|
|
70 |
->write("}\n\n") |
|
|
71 |
; |
|
|
72 |
} |
|
|
73 |
} |