-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathNormalizingNodeVisitor.php
50 lines (43 loc) · 1.41 KB
/
NormalizingNodeVisitor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Translation\Bundle\Twig\Visitor;
use Twig\Environment;
use Twig\Node\Expression\Binary\ConcatBinary;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Node;
use Twig\NodeVisitor\NodeVisitorInterface;
/**
* Performs equivalence transformations on the AST to ensure that
* subsequent visitors do not need to be aware of different syntaxes.
*
* E.g. "foo" ~ "bar" ~ "baz" would become "foobarbaz"
*
* @author Johannes M. Schmitt <[email protected]>
*/
final class NormalizingNodeVisitor implements NodeVisitorInterface
{
public function enterNode(Node $node, Environment $env): Node
{
return $node;
}
public function leaveNode(Node $node, Environment $env): ConstantExpression|Node
{
if ($node instanceof ConcatBinary
&& ($left = $node->getNode('left')) instanceof ConstantExpression
&& ($right = $node->getNode('right')) instanceof ConstantExpression) {
return new ConstantExpression($left->getAttribute('value').$right->getAttribute('value'), $left->getTemplateLine());
}
return $node;
}
public function getPriority(): int
{
return -3;
}
}