From f7121a23bcce8d1b9aec1e5304373dcecbe3ff7e Mon Sep 17 00:00:00 2001 From: Gildas de Cadoudal Date: Thu, 18 Apr 2024 09:45:09 +0200 Subject: [PATCH] fix: #4029 when use_yield is true CaptureNode use iterator_to_array preserveKeys argument to false --- src/Node/CaptureNode.php | 4 +++- .../regression/4029-iterator_to_array.test | 14 ++++++++++++++ tests/Node/MacroTest.php | 2 +- tests/Node/SetTest.php | 2 +- 4 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 tests/Fixtures/regression/4029-iterator_to_array.test diff --git a/src/Node/CaptureNode.php b/src/Node/CaptureNode.php index 561e1ea53..7c187727a 100644 --- a/src/Node/CaptureNode.php +++ b/src/Node/CaptureNode.php @@ -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')) { diff --git a/tests/Fixtures/regression/4029-iterator_to_array.test b/tests/Fixtures/regression/4029-iterator_to_array.test new file mode 100644 index 000000000..99afd892f --- /dev/null +++ b/tests/Fixtures/regression/4029-iterator_to_array.test @@ -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 \ No newline at end of file diff --git a/tests/Node/MacroTest.php b/tests/Node/MacroTest.php index 09d7ee6ca..88ce9b299 100644 --- a/tests/Node/MacroTest.php +++ b/tests/Node/MacroTest.php @@ -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()), diff --git a/tests/Node/SetTest.php b/tests/Node/SetTest.php index 57f66cb9d..ed6ee9ebe 100644 --- a/tests/Node/SetTest.php +++ b/tests/Node/SetTest.php @@ -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()), ];