Fix capturing output from extensions that still use echo

This commit is contained in:
Nicolas Grekas
2024-04-30 15:57:58 +02:00
parent e142e3bf78
commit 8c43456ac6
4 changed files with 10 additions and 6 deletions
+2 -1
View File
@@ -43,8 +43,9 @@ class CaptureNode extends Node
$compiler
->indent()
->subcompile($this->getNode('body'))
->write("return; yield '';\n")
->outdent()
->write("})() ?? new \EmptyIterator()")
->write('})()')
;
if ($useYield) {
$compiler->raw(', false))');
+2 -2
View File
@@ -414,7 +414,7 @@ class EnvironmentTest extends TestCase
*/
public function testLegacyEchoingNode()
{
$loader = new ArrayLoader(['echo_bar' => 'A{% set v %}B{% test %}C{% endset %}D{% test %}E{{ v }}F']);
$loader = new ArrayLoader(['echo_bar' => 'A{% set v %}B{% test %}C{% endset %}D{% test %}E{{ v }}F{% set w %}{% test %}{% endset %}G{{ w }}H']);
$twig = new Environment($loader);
$twig->addExtension(new EnvironmentTest_Extension());
@@ -430,7 +430,7 @@ EOF
);
}
$this->assertSame('ADbarEBbarCF', $twig->render('echo_bar'));
$this->assertSame('ADbarEBbarCFGbarH', $twig->render('echo_bar'));
}
protected function getMockLoader($templateName, $templateContent)
+2 -1
View File
@@ -60,7 +60,8 @@ public function macro_foo(\$__foo__ = null, \$__bar__ = "Foo", ...\$__varargs__)
return new Markup(implode('', iterator_to_array((function () use (\$context, \$macros, \$blocks) {
yield "foo";
})() ?? new \EmptyIterator(), false)), \$this->env->getCharset());
return; yield '';
})(), false)), \$this->env->getCharset());
}
EOF
, new Environment(new ArrayLoader()),
+4 -2
View File
@@ -57,7 +57,8 @@ EOF
// line 1
\$context["foo"] = ('' === \$tmp = implode('', iterator_to_array((function () use (&\$context, \$macros, \$blocks) {
yield "foo";
})() ?? new \EmptyIterator(), false))) ? '' : new Markup(\$tmp, \$this->env->getCharset());
return; yield '';
})(), false))) ? '' : new Markup(\$tmp, \$this->env->getCharset());
EOF
, new Environment(new ArrayLoader()),
];
@@ -66,7 +67,8 @@ EOF
// line 1
$context["foo"] = ('' === $tmp = \Twig\Extension\CoreExtension::captureOutput((function () use (&$context, $macros, $blocks) {
yield "foo";
})() ?? new \EmptyIterator())) ? '' : new Markup($tmp, $this->env->getCharset());
return; yield '';
})())) ? '' : new Markup($tmp, $this->env->getCharset());
EOF
, new Environment(new ArrayLoader()),
];