Merge branch '3.x' into 4.x

* 3.x:
  Replace `return; yield` with `yield from []`
  Fix testExtensionsAreNotInitializedWhenRenderingACompiledTemplate
  Fix MacroTest
This commit is contained in:
Fabien Potencier
2024-09-03 13:55:51 +02:00
8 changed files with 47 additions and 22 deletions
+1 -1
View File
@@ -40,7 +40,7 @@ class BlockNode extends Node
$compiler
->subcompile($this->getNode('body'))
->write("return; yield '';\n") // needed when body doesn't yield anything
->write("yield from [];\n")
->outdent()
->write("}\n\n")
;
+1 -1
View File
@@ -36,7 +36,7 @@ class CaptureNode extends Node
->raw("(function () use (&\$context, \$macros, \$blocks) {\n")
->indent()
->subcompile($this->getNode('body'))
->write("return; yield '';\n")
->write("yield from [];\n")
->outdent()
->write('})(), false))')
;
+1 -1
View File
@@ -346,7 +346,7 @@ final class ModuleNode extends Node
$compiler->subcompile($this->getNode('display_end'));
if (!$this->hasNode('parent')) {
$compiler->write("return; yield '';\n"); // ensure at least one yield call even for templates with no output
$compiler->write("yield from [];\n");
}
$compiler
+15 -8
View File
@@ -177,19 +177,26 @@ class EnvironmentTest extends TestCase
// force compilation
$twig = new Environment($loader = new ArrayLoader(['index' => '{{ foo }}']), $options);
$twig->addExtension($extension = new class extends AbstractExtension {
public bool $throw = false;
public function getFilters(): array
{
if ($this->throw) {
throw new \RuntimeException('Extension are not supposed to be initialized.');
}
return parent::getFilters();
}
});
$key = $cache->generateKey('index', $twig->getTemplateClass('index'));
$cache->write($key, $twig->compileSource(new Source('{{ foo }}', 'index')));
// check that extensions won't be initialized when rendering a template that is already in the cache
$twig = $this
->getMockBuilder(Environment::class)
->setConstructorArgs([$loader, $options])
->setMethods(['initExtensions'])
->getMock()
;
$twig->expects($this->never())->method('initExtensions');
$twig = new Environment($loader, $options);
$extension->throw = true;
$twig->addExtension($extension);
// render template
$output = $twig->render('index', ['foo' => 'bar']);
+1 -1
View File
@@ -41,7 +41,7 @@ public function block_foo(array \$context, array \$blocks = []): iterable
{
\$macros = \$this->macros;
yield "foo";
return; yield '';
yield from [];
}
EOF
, new Environment(new ArrayLoader()),
+26 -8
View File
@@ -36,8 +36,6 @@ class MacroTest extends NodeTestCase
public function getTests()
{
$tests = [];
$arguments = new Node([
'foo' => new ConstantExpression(null, 1),
'bar' => new ConstantExpression('Foo', 1),
@@ -46,7 +44,7 @@ class MacroTest extends NodeTestCase
$body = new BodyNode([new TextNode('foo', 1)]);
$node = new MacroNode('foo', $body, $arguments, 1);
$text[] = [$node, <<<EOF
yield 'with use_yield = true' => [$node, <<<EOF
// line 1
public function macro_foo(\$__foo__ = null, \$__bar__ = "Foo", ...\$__varargs__)
{
@@ -59,15 +57,35 @@ public function macro_foo(\$__foo__ = null, \$__bar__ = "Foo", ...\$__varargs__)
\$blocks = [];
return new Markup(implode('', iterator_to_array((function () use (\$context, \$macros, \$blocks) {
return ('' === \$tmp = implode('', iterator_to_array((function () use (&\$context, \$macros, \$blocks) {
yield "foo";
return; yield '';
})(), false)), \$this->env->getCharset());
yield from [];
})(), false))) ? '' : new Markup(\$tmp, \$this->env->getCharset());
}
EOF
, new Environment(new ArrayLoader()),
, new Environment(new ArrayLoader(), ['use_yield' => true]),
];
return $tests;
yield 'with use_yield = false' => [$node, <<<EOF
// line 1
public function macro_foo(\$__foo__ = null, \$__bar__ = "Foo", ...\$__varargs__)
{
\$macros = \$this->macros;
\$context = \$this->env->mergeGlobals([
"foo" => \$__foo__,
"bar" => \$__bar__,
"varargs" => \$__varargs__,
]);
\$blocks = [];
return ('' === \$tmp = \\Twig\\Extension\\CoreExtension::captureOutput((function () use (&\$context, \$macros, \$blocks) {
yield "foo";
yield from [];
})())) ? '' : new Markup(\$tmp, \$this->env->getCharset());
}
EOF
, new Environment(new ArrayLoader(), ['use_yield' => false]),
];
}
}
+1 -1
View File
@@ -103,7 +103,7 @@ class __TwigTemplate_%x extends Template
\$macros = \$this->macros;
// line 1
yield "foo";
return; yield '';
yield from [];
}
/**
+1 -1
View File
@@ -56,7 +56,7 @@ EOF
// line 1
\$context["foo"] = ('' === \$tmp = implode('', iterator_to_array((function () use (&\$context, \$macros, \$blocks) {
yield "foo";
return; yield '';
yield from [];
})(), false))) ? '' : new Markup(\$tmp, \$this->env->getCharset());
EOF
, new Environment(new ArrayLoader()),