Upgrade Twig library

This commit is contained in:
Michael Foster 2013-08-01 15:20:12 -04:00
parent 22f3a95e0e
commit 0fe5528574
133 changed files with 5080 additions and 1386 deletions

View file

@ -12,8 +12,7 @@
/**
* Represents a macro node.
*
* @package twig
* @author Fabien Potencier <fabien@symfony.com>
* @author Fabien Potencier <fabien@symfony.com>
*/
class Twig_Node_Macro extends Twig_Node
{
@ -29,43 +28,67 @@ class Twig_Node_Macro extends Twig_Node
*/
public function compile(Twig_Compiler $compiler)
{
$arguments = array();
foreach ($this->getNode('arguments') as $argument) {
$arguments[] = '$'.$argument->getAttribute('name').' = null';
$compiler
->addDebugInfo($this)
->write(sprintf("public function get%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(', ');
}
}
$compiler
->addDebugInfo($this)
->write(sprintf("public function get%s(%s)\n", $this->getAttribute('name'), implode(', ', $arguments)), "{\n")
->indent()
->write("\$context = array_merge(\$this->env->getGlobals(), array(\n")
->raw(")\n")
->write("{\n")
->indent()
;
foreach ($this->getNode('arguments') as $argument) {
if (!count($this->getNode('arguments'))) {
$compiler->write("\$context = \$this->env->getGlobals();\n\n");
} else {
$compiler
->write('')
->string($argument->getAttribute('name'))
->raw(' => $'.$argument->getAttribute('name'))
->raw(",\n")
->write("\$context = \$this->env->mergeGlobals(array(\n")
->indent()
;
foreach ($this->getNode('arguments') as $name => $default) {
$compiler
->write('')
->string($name)
->raw(' => $_'.$name)
->raw(",\n")
;
}
$compiler
->outdent()
->write("));\n\n")
;
}
$compiler
->outdent()
->write("));\n\n")
->write("\$blocks = array();\n\n")
->write("ob_start();\n")
->write("try {\n")
->indent()
->subcompile($this->getNode('body'))
->outdent()
->write("} catch(Exception \$e) {\n")
->write("} catch (Exception \$e) {\n")
->indent()
->write("ob_end_clean();\n\n")
->write("throw \$e;\n")
->outdent()
->write("}\n\n")
->write("return ob_get_clean();\n")
->write("return ('' === \$tmp = ob_get_clean()) ? '' : new Twig_Markup(\$tmp, \$this->env->getCharset());\n")
->outdent()
->write("}\n\n")
;