-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AuxiliaryNode: supports traversion + PHP expression
- Loading branch information
Showing
4 changed files
with
108 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Latte (https://latte.nette.org) | ||
* Copyright (c) 2008 David Grudl (https://davidgrudl.com) | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Latte\Compiler\Nodes\Php\Expression; | ||
|
||
use Latte\Compiler\Node; | ||
use Latte\Compiler\Nodes\Php\ExpressionNode; | ||
use Latte\Compiler\PrintContext; | ||
|
||
|
||
class AuxiliaryNode extends ExpressionNode | ||
{ | ||
/** @var (?Node)[] */ | ||
public array $nodes; | ||
|
||
|
||
public function __construct( | ||
public /*readonly*/ \Closure $print, | ||
?Node ...$nodes, | ||
) { | ||
$this->nodes = $nodes; | ||
} | ||
|
||
|
||
public function print(PrintContext $context): string | ||
{ | ||
return ($this->print)($context, ...$this->nodes); | ||
} | ||
|
||
|
||
public function &getIterator(): \Generator | ||
{ | ||
foreach ($this->nodes as &$node) { | ||
if ($node) { | ||
yield $node; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Latte\Compiler\Node; | ||
use Latte\Compiler\Nodes; | ||
use Latte\Compiler\Nodes\Php\Expression; | ||
use Latte\Compiler\Nodes\Php\Scalar\StringNode; | ||
use Latte\Compiler\NodeTraverser; | ||
use Latte\Compiler\PrintContext; | ||
use Tester\Assert; | ||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
|
||
$node = new Nodes\AuxiliaryNode( | ||
fn(PrintContext $context, $a, $b, $c) => $context->format('%node %node %node', $a, $b, $c), | ||
new StringNode('a'), | ||
null, | ||
new StringNode('b'), | ||
); | ||
|
||
$node = (new NodeTraverser)->traverse( | ||
$node, | ||
fn(Node $node) => $node instanceof StringNode ? new StringNode('new') : $node | ||
); | ||
Assert::same("'new' 'new'", $node->print(new PrintContext)); | ||
|
||
|
||
$node = new Expression\AuxiliaryNode( | ||
fn(PrintContext $context, $a, $b, $c) => $context->format('%node %node %node', $a, $b, $c), | ||
new StringNode('a'), | ||
null, | ||
new StringNode('b'), | ||
); | ||
|
||
$node = (new NodeTraverser)->traverse( | ||
$node, | ||
fn(Node $node) => $node instanceof StringNode ? new StringNode('new') : $node | ||
); | ||
Assert::same("'new' 'new'", $node->print(new PrintContext)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters