Skip to content

Commit

Permalink
fix: #4029 when use_yield is true CaptureNode use iterator_to_array p…
Browse files Browse the repository at this point in the history
…reserveKeys argument to false
  • Loading branch information
TLG-Gildas committed Apr 18, 2024
1 parent 7d2b814 commit f7121a2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/Node/CaptureNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ public function compile(Compiler $compiler): void
->indent()
->subcompile($this->getNode('body'))
->outdent()
->write("})() ?? new \EmptyIterator())")
->write("})() ?? new \EmptyIterator()")
;
if ($useYield) {
$compiler->raw(', false))');
} else {
$compiler->raw(')');
}
if (!$this->getAttribute('raw')) {
Expand Down
14 changes: 14 additions & 0 deletions tests/Fixtures/regression/4029-iterator_to_array.test
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion tests/Node/MacroTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
2 changes: 1 addition & 1 deletion tests/Node/SetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function getTests()
// 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()),
];
Expand Down

0 comments on commit f7121a2

Please sign in to comment.