Skip to content

Commit

Permalink
Node: getIterator() must exists (BC break)
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 27, 2022
1 parent e4e401f commit 2ee343c
Show file tree
Hide file tree
Showing 19 changed files with 117 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Latte/Compiler/Nodes/AuxiliaryNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ public function print(PrintContext $context): string
{
return ($this->callable)($context);
}


public function &getIterator(): \Generator
{
false && yield;
}
}
6 changes: 6 additions & 0 deletions src/Latte/Compiler/Nodes/NopNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@ public function print(PrintContext $context): string
{
return '';
}


public function &getIterator(): \Generator
{
false && yield;
}
}
6 changes: 6 additions & 0 deletions src/Latte/Compiler/Nodes/Php/IdentifierNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,10 @@ public function print(PrintContext $context): string
{
return $this->name;
}


public function &getIterator(): \Generator
{
false && yield;
}
}
6 changes: 6 additions & 0 deletions src/Latte/Compiler/Nodes/Php/NameNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,10 @@ public function toCodeString(): string
};
return $prefix . implode('\\', $this->parts);
}


public function &getIterator(): \Generator
{
false && yield;
}
}
4 changes: 4 additions & 0 deletions src/Latte/Compiler/Nodes/Php/ScalarNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@

abstract class ScalarNode extends ExpressionNode
{
public function &getIterator(): \Generator
{
false && yield;
}
}
6 changes: 6 additions & 0 deletions src/Latte/Compiler/Nodes/Php/SuperiorTypeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@ public function print(PrintContext $context): string
{
throw new \LogicException('Cannot directly print SuperiorTypeNode');
}


public function &getIterator(): \Generator
{
false && yield;
}
}
6 changes: 6 additions & 0 deletions src/Latte/Compiler/Nodes/TextNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ public function isWhitespace(): bool
{
return trim($this->content) === '';
}


public function &getIterator(): \Generator
{
false && yield;
}
}
15 changes: 15 additions & 0 deletions src/Latte/Compiler/TemplateParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ public function parseLatteStatement(): ?Node
throw new \LogicException("Incorrect behavior of {{$startTag->name}} parser, unexpected returned value (on line {$startTag->position->line})");
}

$this->checkNodeCompatibility($node);

if ($this->location === self::LocationHead && $startTag->outputMode !== $startTag::OutputNone) {
$this->location = self::LocationText;
}
Expand Down Expand Up @@ -445,4 +447,17 @@ public function isTagAllowed(string $name): bool
{
return !$this->policy || $this->policy->isTagAllowed($name);
}


public function checkNodeCompatibility(Node $node): void
{
static $prev = [\Nette\Bridges\ApplicationLatte\Nodes\NNonceNode::class => true];
if (
!isset($prev[$node::class])
&& (new \ReflectionMethod($node, 'getIterator'))->getDeclaringClass()->getName() === Node::class
) {
trigger_error('Class ' . $node::class . ' should contain method getIterator(), see https://bit.ly/latte-666');
}
$prev[$node::class] = true;
}
}
2 changes: 2 additions & 0 deletions src/Latte/Compiler/TemplateParserHtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ private function openNAttrNodes(array $toOpen): array

} elseif ($res instanceof Node) {
$this->parser->ensureIsConsumed($tag);
$this->parser->checkNodeCompatibility($res);
$res->position = $tag->position;
$tag->replaceNAttribute($res);
$this->parser->popTag();
Expand All @@ -477,6 +478,7 @@ private function finishNAttrNodes(AreaNode $node, array $toClose): AreaNode
while ([$gen, $tag] = array_pop($toClose)) {
$gen->send([$node, null]);
$node = $gen->getReturn();
$this->parser->checkNodeCompatibility($node);
$node->position = $tag->position;
$this->parser->popTag();
$this->parser->ensureIsConsumed($tag);
Expand Down
6 changes: 6 additions & 0 deletions src/Latte/Essential/Nodes/ContentTypeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,10 @@ public function print(PrintContext $context): string
)
: '';
}


public function &getIterator(): \Generator
{
false && yield;
}
}
6 changes: 6 additions & 0 deletions src/Latte/Essential/Nodes/RawPhpNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,10 @@ public function print(PrintContext $context): string
$this->code,
);
}


public function &getIterator(): \Generator
{
false && yield;
}
}
6 changes: 6 additions & 0 deletions src/Latte/Essential/Nodes/RollbackNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ public function print(PrintContext $context): string
{
return 'throw new Latte\Essential\RollbackException;';
}


public function &getIterator(): \Generator
{
false && yield;
}
}
6 changes: 6 additions & 0 deletions src/Latte/Essential/Nodes/TemplatePrintNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@ public function print(PrintContext $context): string
{
return '(new Latte\Essential\Blueprint)->printClass($this, ' . PhpHelpers::dump($this->template) . '); exit;';
}


public function &getIterator(): \Generator
{
false && yield;
}
}
6 changes: 6 additions & 0 deletions src/Latte/Essential/Nodes/TemplateTypeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,10 @@ public function print(PrintContext $context): string
{
return '';
}


public function &getIterator(): \Generator
{
false && yield;
}
}
6 changes: 6 additions & 0 deletions src/Latte/Essential/Nodes/TraceNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ public function print(PrintContext $context): string
$this->position,
);
}


public function &getIterator(): \Generator
{
false && yield;
}
}
6 changes: 6 additions & 0 deletions src/Latte/Essential/Nodes/VarPrintNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,10 @@ public function print(PrintContext $context): string
: 'array_diff_key(get_defined_vars(), $this->getParameters())';
return "(new Latte\\Essential\\Blueprint)->printVars($vars); exit;";
}


public function &getIterator(): \Generator
{
false && yield;
}
}
6 changes: 6 additions & 0 deletions src/Latte/Essential/Nodes/VarTypeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,10 @@ public function print(PrintContext $context): string
{
return '';
}


public function &getIterator(): \Generator
{
false && yield;
}
}
6 changes: 6 additions & 0 deletions tests/common/TemplateParser.nodes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ class FooNode extends Latte\Compiler\Nodes\AreaNode
{
return '';
}


public function &getIterator(): Generator
{
false && yield;
}
}


Expand Down
6 changes: 6 additions & 0 deletions tests/common/nodehelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ public function print(PrintContext $context): string
{
return '';
}


public function &getIterator(): Generator
{
false && yield;
}
}


Expand Down

0 comments on commit 2ee343c

Please sign in to comment.