mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-12 18:36:53 +00:00
Make some small internal tweaks to loops
This commit is contained in:
@@ -65,7 +65,7 @@ class ForNode extends Node
|
|||||||
|
|
||||||
if ($this->hasNode('else')) {
|
if ($this->hasNode('else')) {
|
||||||
$compiler
|
$compiler
|
||||||
->write("if (!\${$loopName}->iterated()) {\n")
|
->write("if (0 === \${$loopName}->getIndex0()) {\n")
|
||||||
->indent()
|
->indent()
|
||||||
->subcompile($this->getNode('else'))
|
->subcompile($this->getNode('else'))
|
||||||
->outdent()
|
->outdent()
|
||||||
|
|||||||
@@ -22,8 +22,10 @@ final class LoopContext
|
|||||||
{
|
{
|
||||||
private mixed $lastChanged;
|
private mixed $lastChanged;
|
||||||
|
|
||||||
public function __construct(private LoopIterator $loop, private $parent)
|
public function __construct(
|
||||||
{
|
private LoopIterator $loop,
|
||||||
|
private $parent,
|
||||||
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getParent(): mixed
|
public function getParent(): mixed
|
||||||
@@ -63,7 +65,7 @@ final class LoopContext
|
|||||||
|
|
||||||
public function isLast(): bool
|
public function isLast(): bool
|
||||||
{
|
{
|
||||||
return $this->loop->isLast();
|
return !$this->loop->getNext()['valid'];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function hasChanged(mixed $value): bool
|
public function hasChanged(mixed $value): bool
|
||||||
|
|||||||
@@ -90,11 +90,6 @@ final class LoopIterator implements \Iterator
|
|||||||
return $this->current['valid'];
|
return $this->current['valid'];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function iterated(): bool
|
|
||||||
{
|
|
||||||
return 0 !== $this->index0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getIndex0(): int
|
public function getIndex0(): int
|
||||||
{
|
{
|
||||||
return $this->index0;
|
return $this->index0;
|
||||||
@@ -113,11 +108,6 @@ final class LoopIterator implements \Iterator
|
|||||||
return $this->length = count($this->seq);
|
return $this->length = count($this->seq);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isLast(): bool
|
|
||||||
{
|
|
||||||
return !$this->getNext()['valid'];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array{valid: bool, key: mixed, value: mixed}
|
* @return array{valid: bool, key: mixed, value: mixed}
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -127,7 +127,7 @@ EOF
|
|||||||
foreach (\$__internal_compile_0 as \$context["k"] => \$context["v"]) {
|
foreach (\$__internal_compile_0 as \$context["k"] => \$context["v"]) {
|
||||||
yield {$this->getVariableGetter('foo')};
|
yield {$this->getVariableGetter('foo')};
|
||||||
}
|
}
|
||||||
if (!\$__internal_compile_0->iterated()) {
|
if (0 === \$__internal_compile_0->getIndex0()) {
|
||||||
yield {$this->getVariableGetter('foo')};
|
yield {$this->getVariableGetter('foo')};
|
||||||
}
|
}
|
||||||
\$_parent = \$context['_parent'];
|
\$_parent = \$context['_parent'];
|
||||||
|
|||||||
Reference in New Issue
Block a user