fix: #4029 when use_yield is true CaptureNode use iterator_to_array preserveKeys argument to false

This commit is contained in:
Gildas de Cadoudal
2024-04-18 09:45:09 +02:00
parent 7d2b814e6c
commit f7121a23bc
4 changed files with 19 additions and 3 deletions
+3 -1
View File
@@ -44,9 +44,11 @@ class CaptureNode extends Node
->indent()
->subcompile($this->getNode('body'))
->outdent()
->write("})() ?? new \EmptyIterator())")
->write("})() ?? new \EmptyIterator()")
;
if ($useYield) {
$compiler->raw(', false))');
} else {
$compiler->raw(')');
}
if (!$this->getAttribute('raw')) {
@@ -0,0 +1,14 @@
--TEST--
#4029 When use_yield is true, CaptureNode fall in iterator_to_array pitfall regarding index overwrite
--TEMPLATE--
{%- set tmp -%}
{%- block foo 'foo' -%}
{%- block bar 'bar' -%}
{%- endset -%}
{{ tmp }}
--DATA--
return []
--CONFIG--
return ['use_yield' => true]
--EXPECT--
foobar
+1 -1
View File
@@ -60,7 +60,7 @@ 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())), \$this->env->getCharset());
})() ?? new \EmptyIterator(), false)), \$this->env->getCharset());
}
EOF
, new Environment(new ArrayLoader()),
+1 -1
View File
@@ -57,7 +57,7 @@ EOF
// line 1
\$context["foo"] = ('' === \$tmp = implode('', iterator_to_array((function () use (&\$context, \$macros, \$blocks) {
yield "foo";
})() ?? new \EmptyIterator()))) ? '' : new Markup(\$tmp, \$this->env->getCharset());
})() ?? new \EmptyIterator(), false))) ? '' : new Markup(\$tmp, \$this->env->getCharset());
EOF
, new Environment(new ArrayLoader()),
];