Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Twig/Visitor/AbstractNodeVisitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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\Node;
use Twig\NodeVisitor\NodeVisitorInterface;

abstract class AbstractNodeVisitor implements NodeVisitorInterface
{

abstract protected function doEnterNode(Node $node, Environment $env): Node;

abstract protected function doLeaveNode(Node $node, Environment $env): Node;

public function enterNode(Node $node, Environment $env): Node
{
return $this->doEnterNode($node, $env);
}

public function leaveNode(Node $node, Environment $env): ?Node
{
return $this->doLeaveNode($node, $env);
}

protected function getValueFromNode(Node $node): ?string
{
if (Environment::VERSION_ID >= 31200) {
return $node->getAttribute('twig_callable')->getName();
} else {
return $node->getNode('filter')->getAttribute('value');
}
}
}
19 changes: 14 additions & 5 deletions Twig/Visitor/DefaultApplyingNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Twig\Node\Expression\Ternary\ConditionalTernary;
use Twig\Node\Node;
use Twig\Node\Nodes;
use Twig\NodeVisitor\AbstractNodeVisitor;
use Twig\TwigFilter;

/**
Expand Down Expand Up @@ -50,14 +49,14 @@ public function doEnterNode(Node $node, Environment $env): Node
return $node;
}

if (!($node instanceof FilterExpression && 'desc' === $node->getNode('filter')->getAttribute('value'))) {
if (!($node instanceof FilterExpression && 'desc' === $this->getValueFromNode($node))) {
return $node;
}

$transNode = $node->getNode('node');
while ($transNode instanceof FilterExpression
&& 'trans' !== $transNode->getNode('filter')->getAttribute('value')
&& 'transchoice' !== $transNode->getNode('filter')->getAttribute('value')) {
&& 'trans' !== $this->getValueFromNode($transNode)
&& 'transchoice' !== $this->getValueFromNode($transNode)) {
$transNode = $transNode->getNode('node');
}

Expand All @@ -72,7 +71,7 @@ public function doEnterNode(Node $node, Environment $env): Node
// if the |transchoice filter is used, delegate the call to the TranslationExtension
// so that we can catch a possible exception when the default translation has not yet
// been extracted
if ('transchoice' === $transNode->getNode('filter')->getAttribute('value')) {
if ('transchoice' === $this->getValueFromNode($transNode)) {
$transchoiceArguments = new ArrayExpression([], $transNode->getTemplateLine());
$transchoiceArguments->addElement($wrappingNode->getNode('node'));
$transchoiceArguments->addElement($defaultNode);
Expand Down Expand Up @@ -150,4 +149,14 @@ public function getPriority(): int
{
return -2;
}

public function enterNode(Node $node, Environment $env): Node
{
return $this->doEnterNode($node, $env);
}

public function leaveNode(Node $node, Environment $env): ?Node
{
return $this->doLeaveNode($node, $env);
}
}
1 change: 0 additions & 1 deletion Twig/Visitor/NormalizingNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Twig\Node\Expression\Binary\ConcatBinary;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Node;
use Twig\NodeVisitor\AbstractNodeVisitor;

/**
* Performs equivalence transformations on the AST to ensure that
Expand Down
3 changes: 1 addition & 2 deletions Twig/Visitor/RemovingNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Twig\Environment;
use Twig\Node\Expression\FilterExpression;
use Twig\Node\Node;
use Twig\NodeVisitor\AbstractNodeVisitor;

/**
* Removes translation metadata filters from the AST.
Expand All @@ -36,7 +35,7 @@ public function setEnabled(bool $bool): void
protected function doEnterNode(Node $node, Environment $env): Node
{
if ($this->enabled && $node instanceof FilterExpression) {
$name = $node->getNode('filter')->getAttribute('value');
$name = $this->getValueFromNode($node);

if ('desc' === $name || 'meaning' === $name) {
return $this->enterNode($node->getNode('node'), $env);
Expand Down
Loading