4 * This file is part of Twig.
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
13 * Represents a macro node.
15 * @author Fabien Potencier <fabien@symfony.com>
17 class Twig_Node_Macro extends Twig_Node
19 const VARARGS_NAME = 'varargs';
21 public function __construct($name, Twig_NodeInterface $body, Twig_NodeInterface $arguments, $lineno, $tag = null)
23 foreach ($arguments as $argumentName => $argument) {
24 if (self::VARARGS_NAME === $argumentName) {
25 throw new Twig_Error_Syntax(sprintf('The argument "%s" in macro "%s" cannot be defined because the variable "%s" is reserved for arbitrary arguments.', self::VARARGS_NAME, $name, self::VARARGS_NAME), $argument->getTemplateLine());
29 parent::__construct(array('body' => $body, 'arguments' => $arguments), array('name' => $name), $lineno, $tag);
32 public function compile(Twig_Compiler $compiler)
36 ->write(sprintf('public function get%s(', $this->getAttribute('name')))
39 $count = count($this->getNode('arguments'));
41 foreach ($this->getNode('arguments') as $name => $default) {
43 ->raw('$__'.$name.'__ = ')
44 ->subcompile($default)
47 if (++$pos < $count) {
52 if (PHP_VERSION_ID >= 50600) {
57 $compiler->raw('...$__varargs__');
67 ->write("\$context = \$this->env->mergeGlobals(array(\n")
71 foreach ($this->getNode('arguments') as $name => $default) {
75 ->raw(' => $__'.$name.'__')
82 ->string(self::VARARGS_NAME)
86 if (PHP_VERSION_ID >= 50600) {
87 $compiler->raw("\$__varargs__,\n");
90 ->raw('func_num_args() > ')
92 ->raw(' ? array_slice(func_get_args(), ')
94 ->raw(") : array(),\n")
101 ->write("\$blocks = array();\n\n")
102 ->write("ob_start();\n")
105 ->subcompile($this->getNode('body'))
107 ->write("} catch (Exception \$e) {\n")
109 ->write("ob_end_clean();\n\n")
110 ->write("throw \$e;\n")
112 ->write("} catch (Throwable \$e) {\n")
114 ->write("ob_end_clean();\n\n")
115 ->write("throw \$e;\n")
118 ->write("return ('' === \$tmp = ob_get_clean()) ? '' : new Twig_Markup(\$tmp, \$this->env->getCharset());\n")
125 class_alias('Twig_Node_Macro', 'Twig\Node\MacroNode', false);