Skip to content

Commit

Permalink
AuxiliaryNode: supports traversion + PHP expression
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 27, 2023
1 parent fab3848 commit 10ca5a3
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 6 deletions.
17 changes: 14 additions & 3 deletions src/Latte/Compiler/Nodes/AuxiliaryNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,36 @@

namespace Latte\Compiler\Nodes;

use Latte\Compiler\Node;
use Latte\Compiler\PrintContext;


class AuxiliaryNode extends AreaNode
{
/** @var (?Node)[] */
public array $nodes;


public function __construct(
public /*readonly*/ \Closure $callable,
public /*readonly*/ \Closure $print,
?Node ...$nodes,
) {
$this->nodes = $nodes;
}


public function print(PrintContext $context): string
{
return ($this->callable)($context);
return ($this->print)($context, ...$this->nodes);
}


public function &getIterator(): \Generator
{
false && yield;
foreach ($this->nodes as &$node) {
if ($node) {
yield $node;
}
}
}
}
45 changes: 45 additions & 0 deletions src/Latte/Compiler/Nodes/Php/Expression/AuxiliaryNode.php
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;
}
}
}
}
41 changes: 41 additions & 0 deletions tests/common/AuxiliaryNode.phpt
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));
11 changes: 8 additions & 3 deletions tests/common/TemplateParser.nodes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ Assert::match(<<<'XX'
| | | content: null
| | | nAttributes: array (0)
| | | tagNode: Latte\Compiler\Nodes\AuxiliaryNode
| | | | callable: Closure($context)
| | | | nodes: array (0)
| | | | print: Closure($context)
| | | | position: null
| | | captureTagName: false
| | | endTagVar: unset
Expand All @@ -176,6 +177,7 @@ Assert::match(<<<'XX'
| position: 1:1 (offset 0)
contentType: 'html'
position: null

XX, parse("<br attr1 \nattr2=val\n attr3=\n'val'>"));


Expand Down Expand Up @@ -231,7 +233,8 @@ Assert::match(<<<'XX'
| | | content: null
| | | nAttributes: array (0)
| | | tagNode: Latte\Compiler\Nodes\AuxiliaryNode
| | | | callable: Closure($context)
| | | | nodes: array (0)
| | | | print: Closure($context)
| | | | position: null
| | | captureTagName: false
| | | endTagVar: unset
Expand All @@ -244,6 +247,7 @@ Assert::match(<<<'XX'
| position: 1:1 (offset 0)
contentType: 'html'
position: null

XX, parse("<br {foo}attr4='val'{/foo} attr5={foo}b{/foo} attr6=c{foo/}d>"));


Expand Down Expand Up @@ -286,7 +290,8 @@ Assert::match(<<<'XX'
| | | | position: 1:4 (offset 3)
| | | nAttributes: array (0)
| | | tagNode: Latte\Compiler\Nodes\AuxiliaryNode
| | | | callable: Closure($context)
| | | | nodes: array (0)
| | | | print: Closure($context)
| | | | position: null
| | | captureTagName: false
| | | endTagVar: unset
Expand Down

0 comments on commit 10ca5a3

Please sign in to comment.