public function compile(Twig_Compiler $compiler) { $compiler->addDebugInfo($this)->write(sprintf('public function macro_%s(', $this->getAttribute('name'))); $count = count($this->getNode('arguments')); $pos = 0; foreach ($this->getNode('arguments') as $name => $default) { $compiler->raw('$__' . $name . '__ = ')->subcompile($default); if (++$pos < $count) { $compiler->raw(', '); } } if (PHP_VERSION_ID >= 50600) { if ($count) { $compiler->raw(', '); } $compiler->raw('...$__varargs__'); } $compiler->raw(")\n")->write("{\n")->indent(); $compiler->write("\$context = \$this->env->mergeGlobals(array(\n")->indent(); foreach ($this->getNode('arguments') as $name => $default) { $compiler->addIndentation()->string($name)->raw(' => $__' . $name . '__')->raw(",\n"); } $compiler->addIndentation()->string(self::VARARGS_NAME)->raw(' => '); if (PHP_VERSION_ID >= 50600) { $compiler->raw("\$__varargs__,\n"); } else { $compiler->raw('func_num_args() > ')->repr($count)->raw(' ? array_slice(func_get_args(), ')->repr($count)->raw(") : array(),\n"); } $compiler->outdent()->write("));\n\n")->write("\$blocks = array();\n\n")->write("ob_start();\n")->write("try {\n")->indent()->subcompile($this->getNode('body'))->raw("\n")->write("return ('' === \$tmp = ob_get_contents()) ? '' : new Twig_Markup(\$tmp, \$this->env->getCharset());\n")->outdent()->write("} finally {\n")->indent()->write("ob_end_clean();\n")->outdent()->write("}\n")->outdent()->write("}\n\n"); }
/** * {@inheritdoc} */ public function compile(\Twig_Compiler $compiler) { $compiler->write("if (\$this->env->isDebug()) {\n")->indent(); if (!$this->hasNode('values')) { // remove embedded templates (macros) from the context $compiler->write(sprintf('$%svars = array();' . "\n", $this->varPrefix))->write(sprintf('foreach ($context as $%1$skey => $%1$sval) {' . "\n", $this->varPrefix))->indent()->write(sprintf('if (!$%sval instanceof \\Twig_Template) {' . "\n", $this->varPrefix))->indent()->write(sprintf('$%1$svars[$%1$skey] = $%1$sval;' . "\n", $this->varPrefix))->outdent()->write("}\n")->outdent()->write("}\n")->addDebugInfo($this)->write(sprintf('\\Symfony\\Component\\VarDumper\\VarDumper::dump($%svars);' . "\n", $this->varPrefix)); } elseif (($values = $this->getNode('values')) && 1 === $values->count()) { $compiler->addDebugInfo($this)->write('\\Symfony\\Component\\VarDumper\\VarDumper::dump(')->subcompile($values->getNode(0))->raw(");\n"); } else { $compiler->addDebugInfo($this)->write('\\Symfony\\Component\\VarDumper\\VarDumper::dump(array(' . "\n")->indent(); foreach ($values as $node) { $compiler->addIndentation(); if ($node->hasAttribute('name')) { $compiler->string($node->getAttribute('name'))->raw(' => '); } $compiler->subcompile($node)->raw(",\n"); } $compiler->outdent()->write("));\n"); } $compiler->outdent()->write("}\n"); }
protected function compileWithArguments(\Twig_Compiler $compiler, $expressionNode, array $arguments) { $compiler->raw("\n"); $compiler->indent(); $compiler->addIndentation(); $compiler->raw("function() use(&\$context) {\n"); $compiler->indent(); // copy of arguments and __ from context foreach ($arguments as $arg) { $compiler->addIndentation(); $compiler->raw("if (isset(\$context['{$arg}'])) \$outer{$arg} = \$context['{$arg}'];\n"); } $compiler->addIndentation(); $compiler->raw("if (isset(\$context['__'])) \$outer__ = \$context['__'];\n"); // adding closure's arguments to context $compiler->addIndentation(); $compiler->raw("\$context['__'] = func_get_args();\n"); foreach ($arguments as $i => $arg) { $compiler->addIndentation(); $compiler->raw("if (func_num_args()>{$i}) \$context['{$arg}'] = func_get_arg({$i});\n"); $compiler->addIndentation(); $compiler->raw("else unset(\$context['{$arg}']);\n"); } // getting call result $compiler->addIndentation(); $compiler->raw("\$result = "); $compiler->subcompile($this->getNode($expressionNode)); $compiler->raw(";\n"); // recreating original context foreach ($arguments as $arg) { $compiler->addIndentation(); $compiler->raw("if (isset(\$outer{$arg})) \$context['{$arg}'] = \$outer{$arg} ;\n"); $compiler->addIndentation(); $compiler->raw("else unset(\$context['{$arg}']);\n"); } $compiler->addIndentation(); $compiler->raw("if (isset(\$outer__)) \$context['__'] = \$outer__ ;\n"); $compiler->addIndentation(); $compiler->raw("else unset(\$context['__']);\n"); // return statement $compiler->addIndentation(); $compiler->raw("return \$result;\n"); $compiler->outdent(); $compiler->addIndentation(); $compiler->raw("}\n"); $compiler->outdent(); $compiler->addIndentation(); }