mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-20 14:36:57 +00:00
Do not use same var names in loop function
These variables are passed in when the loop function is invoked when yielding. Giving them the same internal var names complicates things for no good reason.
This commit is contained in:
@@ -43,25 +43,24 @@ class ForNode extends Node
|
|||||||
{
|
{
|
||||||
$iteratorVar = $compiler->getVarName();
|
$iteratorVar = $compiler->getVarName();
|
||||||
$functionVar = $compiler->getVarName();
|
$functionVar = $compiler->getVarName();
|
||||||
$parentVar = $compiler->getVarName();
|
|
||||||
|
|
||||||
$compiler
|
$compiler
|
||||||
->addDebugInfo($this)
|
->addDebugInfo($this)
|
||||||
->write("\$$iteratorVar = new \Twig\Runtime\LoopIterator(")
|
->write("\$$iteratorVar = new \Twig\Runtime\LoopIterator(")
|
||||||
->subcompile($this->getNode('seq'))
|
->subcompile($this->getNode('seq'))
|
||||||
->raw(");\n")
|
->raw(");\n")
|
||||||
->write("\$$functionVar = function (\$$iteratorVar, &\$context, \$blocks, \$$functionVar, \$depth) {\n")
|
->write("\$$functionVar = function (\$iterator, &\$context, \$blocks, \$recurseFunc, \$depth) {\n")
|
||||||
->indent()
|
->indent()
|
||||||
->write("\$macros = \$this->macros;\n")
|
->write("\$macros = \$this->macros;\n")
|
||||||
->write("\$$parentVar = \$context;\n")
|
->write("\$parent = \$context;\n")
|
||||||
;
|
;
|
||||||
|
|
||||||
if ($this->getAttribute('with_loop')) {
|
if ($this->getAttribute('with_loop')) {
|
||||||
$compiler->write("\$context['loop'] = new \Twig\Runtime\LoopContext(\$$iteratorVar, \$$parentVar, \$blocks, \$$functionVar, \$depth);\n");
|
$compiler->write("\$context['loop'] = new \Twig\Runtime\LoopContext(\$iterator, \$parent, \$blocks, \$recurseFunc, \$depth);\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
$compiler
|
$compiler
|
||||||
->write("foreach (\$$iteratorVar as ")
|
->write("foreach (\$iterator as ")
|
||||||
->subcompile($this->getNode('key_target'))
|
->subcompile($this->getNode('key_target'))
|
||||||
->raw(' => ')
|
->raw(' => ')
|
||||||
->subcompile($this->getNode('value_target'))
|
->subcompile($this->getNode('value_target'))
|
||||||
@@ -74,7 +73,7 @@ class ForNode extends Node
|
|||||||
|
|
||||||
if ($this->hasNode('else')) {
|
if ($this->hasNode('else')) {
|
||||||
$compiler
|
$compiler
|
||||||
->write("if (0 === \${$iteratorVar}->getIndex0()) {\n")
|
->write("if (0 === \$iterator->getIndex0()) {\n")
|
||||||
->indent()
|
->indent()
|
||||||
->subcompile($this->getNode('else'))
|
->subcompile($this->getNode('else'))
|
||||||
->outdent()
|
->outdent()
|
||||||
@@ -90,7 +89,7 @@ class ForNode extends Node
|
|||||||
$compiler->raw(");\n");
|
$compiler->raw(");\n");
|
||||||
|
|
||||||
// keep the values set in the inner context for variables defined in the outer context
|
// keep the values set in the inner context for variables defined in the outer context
|
||||||
$compiler->write("\$context = array_intersect_key(\$context, \$$parentVar) + \$$parentVar;\n");
|
$compiler->write("\$context = array_intersect_key(\$context, \$parent) + \$parent;\n");
|
||||||
|
|
||||||
$compiler
|
$compiler
|
||||||
->write("yield from [];\n")
|
->write("yield from [];\n")
|
||||||
|
|||||||
+25
-25
@@ -68,14 +68,14 @@ class ForTest extends NodeTestCase
|
|||||||
$tests[] = [$node, <<<EOF
|
$tests[] = [$node, <<<EOF
|
||||||
// line 1
|
// line 1
|
||||||
\$__internal_compile_0 = new \Twig\Runtime\LoopIterator({$itemsGetter});
|
\$__internal_compile_0 = new \Twig\Runtime\LoopIterator({$itemsGetter});
|
||||||
\$__internal_compile_1 = function (\$__internal_compile_0, &\$context, \$blocks, \$__internal_compile_1, \$depth) {
|
\$__internal_compile_1 = function (\$iterator, &\$context, \$blocks, \$recurseFunc, \$depth) {
|
||||||
\$macros = \$this->macros;
|
\$macros = \$this->macros;
|
||||||
\$__internal_compile_2 = \$context;
|
\$parent = \$context;
|
||||||
foreach (\$__internal_compile_0 as \$context["key"] => \$context["item"]) {
|
foreach (\$iterator as \$context["key"] => \$context["item"]) {
|
||||||
yield {$fooGetter};
|
yield {$fooGetter};
|
||||||
}
|
}
|
||||||
unset(\$context['key'], \$context['item']);
|
unset(\$context['key'], \$context['item']);
|
||||||
\$context = array_intersect_key(\$context, \$__internal_compile_2) + \$__internal_compile_2;
|
\$context = array_intersect_key(\$context, \$parent) + \$parent;
|
||||||
yield from [];
|
yield from [];
|
||||||
};
|
};
|
||||||
yield from \$__internal_compile_1(\$__internal_compile_0, \$context, \$blocks, \$__internal_compile_1, 0);
|
yield from \$__internal_compile_1(\$__internal_compile_0, \$context, \$blocks, \$__internal_compile_1, 0);
|
||||||
@@ -93,15 +93,15 @@ EOF
|
|||||||
$tests[] = [$node, <<<EOF
|
$tests[] = [$node, <<<EOF
|
||||||
// line 1
|
// line 1
|
||||||
\$__internal_compile_0 = new \Twig\Runtime\LoopIterator({$valuesGetter});
|
\$__internal_compile_0 = new \Twig\Runtime\LoopIterator({$valuesGetter});
|
||||||
\$__internal_compile_1 = function (\$__internal_compile_0, &\$context, \$blocks, \$__internal_compile_1, \$depth) {
|
\$__internal_compile_1 = function (\$iterator, &\$context, \$blocks, \$recurseFunc, \$depth) {
|
||||||
\$macros = \$this->macros;
|
\$macros = \$this->macros;
|
||||||
\$__internal_compile_2 = \$context;
|
\$parent = \$context;
|
||||||
\$context['loop'] = new \Twig\Runtime\LoopContext(\$__internal_compile_0, \$__internal_compile_2, \$blocks, \$__internal_compile_1, \$depth);
|
\$context['loop'] = new \Twig\Runtime\LoopContext(\$iterator, \$parent, \$blocks, \$recurseFunc, \$depth);
|
||||||
foreach (\$__internal_compile_0 as \$context["k"] => \$context["v"]) {
|
foreach (\$iterator as \$context["k"] => \$context["v"]) {
|
||||||
yield {$fooGetter};
|
yield {$fooGetter};
|
||||||
}
|
}
|
||||||
unset(\$context['k'], \$context['v'], \$context['loop']);
|
unset(\$context['k'], \$context['v'], \$context['loop']);
|
||||||
\$context = array_intersect_key(\$context, \$__internal_compile_2) + \$__internal_compile_2;
|
\$context = array_intersect_key(\$context, \$parent) + \$parent;
|
||||||
yield from [];
|
yield from [];
|
||||||
};
|
};
|
||||||
yield from \$__internal_compile_1(\$__internal_compile_0, \$context, \$blocks, \$__internal_compile_1, 0);
|
yield from \$__internal_compile_1(\$__internal_compile_0, \$context, \$blocks, \$__internal_compile_1, 0);
|
||||||
@@ -119,15 +119,15 @@ EOF
|
|||||||
$tests[] = [$node, <<<EOF
|
$tests[] = [$node, <<<EOF
|
||||||
// line 1
|
// line 1
|
||||||
\$__internal_compile_0 = new \Twig\Runtime\LoopIterator({$valuesGetter});
|
\$__internal_compile_0 = new \Twig\Runtime\LoopIterator({$valuesGetter});
|
||||||
\$__internal_compile_1 = function (\$__internal_compile_0, &\$context, \$blocks, \$__internal_compile_1, \$depth) {
|
\$__internal_compile_1 = function (\$iterator, &\$context, \$blocks, \$recurseFunc, \$depth) {
|
||||||
\$macros = \$this->macros;
|
\$macros = \$this->macros;
|
||||||
\$__internal_compile_2 = \$context;
|
\$parent = \$context;
|
||||||
\$context['loop'] = new \Twig\Runtime\LoopContext(\$__internal_compile_0, \$__internal_compile_2, \$blocks, \$__internal_compile_1, \$depth);
|
\$context['loop'] = new \Twig\Runtime\LoopContext(\$iterator, \$parent, \$blocks, \$recurseFunc, \$depth);
|
||||||
foreach (\$__internal_compile_0 as \$context["k"] => \$context["v"]) {
|
foreach (\$iterator as \$context["k"] => \$context["v"]) {
|
||||||
yield {$fooGetter};
|
yield {$fooGetter};
|
||||||
}
|
}
|
||||||
unset(\$context['k'], \$context['v'], \$context['loop']);
|
unset(\$context['k'], \$context['v'], \$context['loop']);
|
||||||
\$context = array_intersect_key(\$context, \$__internal_compile_2) + \$__internal_compile_2;
|
\$context = array_intersect_key(\$context, \$parent) + \$parent;
|
||||||
yield from [];
|
yield from [];
|
||||||
};
|
};
|
||||||
yield from \$__internal_compile_1(\$__internal_compile_0, \$context, \$blocks, \$__internal_compile_1, 0);
|
yield from \$__internal_compile_1(\$__internal_compile_0, \$context, \$blocks, \$__internal_compile_1, 0);
|
||||||
@@ -146,20 +146,20 @@ EOF
|
|||||||
$tests[] = [$node, <<<EOF
|
$tests[] = [$node, <<<EOF
|
||||||
// line 1
|
// line 1
|
||||||
\$__internal_compile_0 = new \Twig\Runtime\LoopIterator({$valuesGetter});
|
\$__internal_compile_0 = new \Twig\Runtime\LoopIterator({$valuesGetter});
|
||||||
\$__internal_compile_1 = function (\$__internal_compile_0, &\$context, \$blocks, \$__internal_compile_1, \$depth) {
|
\$__internal_compile_1 = function (\$iterator, &\$context, \$blocks, \$recurseFunc, \$depth) {
|
||||||
\$macros = \$this->macros;
|
\$macros = \$this->macros;
|
||||||
\$__internal_compile_2 = \$context;
|
\$parent = \$context;
|
||||||
\$context['loop'] = new \Twig\Runtime\LoopContext(\$__internal_compile_0, \$__internal_compile_2, \$blocks, \$__internal_compile_1, \$depth);
|
\$context['loop'] = new \Twig\Runtime\LoopContext(\$iterator, \$parent, \$blocks, \$recurseFunc, \$depth);
|
||||||
foreach (\$__internal_compile_0 as \$context["k"] => \$context["v"]) {
|
foreach (\$iterator as \$context["k"] => \$context["v"]) {
|
||||||
if (true) {
|
if (true) {
|
||||||
yield {$fooGetter};
|
yield {$fooGetter};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (0 === \$__internal_compile_0->getIndex0()) {
|
if (0 === \$iterator->getIndex0()) {
|
||||||
yield {$fooGetter};
|
yield {$fooGetter};
|
||||||
}
|
}
|
||||||
unset(\$context['k'], \$context['v'], \$context['loop']);
|
unset(\$context['k'], \$context['v'], \$context['loop']);
|
||||||
\$context = array_intersect_key(\$context, \$__internal_compile_2) + \$__internal_compile_2;
|
\$context = array_intersect_key(\$context, \$parent) + \$parent;
|
||||||
yield from [];
|
yield from [];
|
||||||
};
|
};
|
||||||
yield from \$__internal_compile_1(\$__internal_compile_0, \$context, \$blocks, \$__internal_compile_1, 0);
|
yield from \$__internal_compile_1(\$__internal_compile_0, \$context, \$blocks, \$__internal_compile_1, 0);
|
||||||
@@ -173,15 +173,15 @@ EOF
|
|||||||
$tests[] = [$node, <<<EOF
|
$tests[] = [$node, <<<EOF
|
||||||
// line 1
|
// line 1
|
||||||
\$__internal_compile_0 = new \Twig\Runtime\LoopIterator({$itemsGetter});
|
\$__internal_compile_0 = new \Twig\Runtime\LoopIterator({$itemsGetter});
|
||||||
\$__internal_compile_1 = function (\$__internal_compile_0, &\$context, \$blocks, \$__internal_compile_1, \$depth) {
|
\$__internal_compile_1 = function (\$iterator, &\$context, \$blocks, \$recurseFunc, \$depth) {
|
||||||
\$macros = \$this->macros;
|
\$macros = \$this->macros;
|
||||||
\$__internal_compile_2 = \$context;
|
\$parent = \$context;
|
||||||
\$context['loop'] = new \Twig\Runtime\LoopContext(\$__internal_compile_0, \$__internal_compile_2, \$blocks, \$__internal_compile_1, \$depth);
|
\$context['loop'] = new \Twig\Runtime\LoopContext(\$iterator, \$parent, \$blocks, \$recurseFunc, \$depth);
|
||||||
foreach (\$__internal_compile_0 as \$context["_key"] => \$context["item"]) {
|
foreach (\$iterator as \$context["_key"] => \$context["item"]) {
|
||||||
yield from CoreExtension::getAttribute(\$this->env, \$this->source, \$context["loop"], "__invoke", arguments: [CoreExtension::getAttribute(\$this->env, \$this->source, \$context["item"], "children", arguments: [], lineno: 1)], type: "method", lineno: 1);
|
yield from CoreExtension::getAttribute(\$this->env, \$this->source, \$context["loop"], "__invoke", arguments: [CoreExtension::getAttribute(\$this->env, \$this->source, \$context["item"], "children", arguments: [], lineno: 1)], type: "method", lineno: 1);
|
||||||
}
|
}
|
||||||
unset(\$context['_key'], \$context['item'], \$context['loop']);
|
unset(\$context['_key'], \$context['item'], \$context['loop']);
|
||||||
\$context = array_intersect_key(\$context, \$__internal_compile_2) + \$__internal_compile_2;
|
\$context = array_intersect_key(\$context, \$parent) + \$parent;
|
||||||
yield from [];
|
yield from [];
|
||||||
};
|
};
|
||||||
yield from \$__internal_compile_1(\$__internal_compile_0, \$context, \$blocks, \$__internal_compile_1, 0);
|
yield from \$__internal_compile_1(\$__internal_compile_0, \$context, \$blocks, \$__internal_compile_1, 0);
|
||||||
|
|||||||
Reference in New Issue
Block a user