Skip to content

chore(twig): replace class aliases by class namespaces #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
11 changes: 6 additions & 5 deletions Service/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Translation\Extractor\Model\SourceCollection;
use Translation\Extractor\Model\SourceLocation;
use Translation\Bundle\Catalogue\Operation\ReplaceOperation;
use Twig\Environment;

/**
* Use extractors to import translations to message catalogues.
Expand All @@ -40,7 +41,7 @@ final class Importer
private $config;

/**
* @var \Twig_Environment
* @var Environment
*/
private $twig;

Expand All @@ -50,11 +51,11 @@ final class Importer
private $defaultLocale;

/**
* @param Extractor $extractor
* @param \Twig_Environment $twig
* @param string $defaultLocale
* @param Extractor $extractor
* @param Environment $twig
* @param string $defaultLocale
*/
public function __construct(Extractor $extractor, \Twig_Environment $twig, $defaultLocale)
public function __construct(Extractor $extractor, Environment $twig, $defaultLocale)
{
$this->extractor = $extractor;
$this->twig = $twig;
Expand Down
10 changes: 6 additions & 4 deletions Tests/Unit/Twig/BaseTwigTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
namespace Translation\Bundle\Tests\Unit\Twig;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\MessageSelector;
use Symfony\Component\Translation\IdentityTranslator;
use Symfony\Bridge\Twig\Extension\TranslationExtension as SymfonyTranslationExtension;
use Symfony\Component\Translation\IdentityTranslator;
use Symfony\Component\Translation\MessageSelector;
use Translation\Bundle\Twig\TranslationExtension;
use Twig\Loader\ArrayLoader;
use Twig\Environment;
use Twig\Source;

/**
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
Expand All @@ -30,10 +32,10 @@ final protected function parse($file, $debug = false)
$loader = class_exists(ArrayLoader::class)
? new ArrayLoader()
: new \Twig_Loader_Array([]);
$env = new \Twig_Environment($loader);
$env = new Environment($loader);
$env->addExtension(new SymfonyTranslationExtension($translator = new IdentityTranslator(new MessageSelector())));
$env->addExtension(new TranslationExtension($translator, $debug));

return $env->parse($env->tokenize(new \Twig_Source($content, '')))->getNode('body');
return $env->parse($env->tokenize(new Source($content, '')))->getNode('body');
}
}
5 changes: 3 additions & 2 deletions Twig/EditInPlaceExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\HttpFoundation\RequestStack;
use Translation\Bundle\EditInPlace\ActivatorInterface;
use Twig\TwigFilter;

/**
* Override the `trans` functions `is_safe` option to allow HTML output from the
Expand All @@ -38,8 +39,8 @@ final class EditInPlaceExtension extends \Symfony\Bridge\Twig\Extension\Translat
public function getFilters()
{
return [
new \Twig_SimpleFilter('trans', [$this, 'trans'], ['is_safe_callback' => [$this, 'isSafe']]),
new \Twig_SimpleFilter('transchoice', [$this, 'transchoice'], ['is_safe_callback' => [$this, 'isSafe']]),
new TwigFilter('trans', [$this, 'trans'], ['is_safe_callback' => [$this, 'isSafe']]),
new TwigFilter('transchoice', [$this, 'transchoice'], ['is_safe_callback' => [$this, 'isSafe']]),
];
}

Expand Down
10 changes: 7 additions & 3 deletions Twig/Node/Transchoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@

namespace Translation\Bundle\Twig\Node;

class Transchoice extends \Twig_Node_Expression
use Twig\Compiler;
use Twig\Node\Expression\AbstractExpression;
use Twig\Node\Expression\ArrayExpression;

class Transchoice extends AbstractExpression
{
public function __construct(\Twig_Node_Expression_Array $arguments, $lineno)
public function __construct(ArrayExpression $arguments, $lineno)
{
parent::__construct(['arguments' => $arguments], [], $lineno);
}

public function compile(\Twig_Compiler $compiler)
public function compile(Compiler $compiler)
{
$compiler->raw(
sprintf(
Expand Down
8 changes: 5 additions & 3 deletions Twig/TranslationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
use Translation\Bundle\Twig\Visitor\DefaultApplyingNodeVisitor;
use Translation\Bundle\Twig\Visitor\NormalizingNodeVisitor;
use Translation\Bundle\Twig\Visitor\RemovingNodeVisitor;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

/**
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class TranslationExtension extends \Twig_Extension
final class TranslationExtension extends AbstractExtension
{
/**
* @var TranslatorInterface
Expand Down Expand Up @@ -48,8 +50,8 @@ public function __construct(TranslatorInterface $translator, $debug = false)
public function getFilters()
{
return [
new \Twig_SimpleFilter('desc', [$this, 'desc']),
new \Twig_SimpleFilter('meaning', [$this, 'meaning']),
new TwigFilter('desc', [$this, 'desc']),
new TwigFilter('meaning', [$this, 'meaning']),
];
}

Expand Down
46 changes: 27 additions & 19 deletions Twig/Visitor/DefaultApplyingNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
namespace Translation\Bundle\Twig\Visitor;

use Translation\Bundle\Twig\Node\Transchoice;
use Twig\Environment;
use Twig\Node\Expression\ArrayExpression;
use Twig\Node\Expression\Binary\EqualBinary;
use Twig\Node\Expression\ConditionalExpression;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\FilterExpression;
use Twig\Node\Node;
use Twig\NodeVisitor\AbstractNodeVisitor;

/**
* Applies the value of the "desc" filter if the "trans" filter has no
Expand All @@ -21,7 +29,7 @@
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
final class DefaultApplyingNodeVisitor extends \Twig_BaseNodeVisitor
final class DefaultApplyingNodeVisitor extends AbstractNodeVisitor
{
/**
* @var bool
Expand All @@ -37,29 +45,29 @@ public function setEnabled($bool)
}

/**
* @param \Twig_Node $node
* @param \Twig_Environment $env
* @param Node $node
* @param Environment $env
*
* @return \Twig_Node
* @return Node
*/
public function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
public function doEnterNode(Node $node, Environment $env)
{
if (!$this->enabled) {
return $node;
}

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

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

if (!$transNode instanceof \Twig_Node_Expression_Filter) {
if (!$transNode instanceof FilterExpression) {
throw new \RuntimeException(sprintf('The "desc" filter must be applied after a "trans", or "transchoice" filter.'));
}

Expand All @@ -71,7 +79,7 @@ public function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
// so that we can catch a possible exception when the default translation has not yet
// been extracted
if ('transchoice' === $transNode->getNode('filter')->getAttribute('value')) {
$transchoiceArguments = new \Twig_Node_Expression_Array([], $transNode->getTemplateLine());
$transchoiceArguments = new ArrayExpression([], $transNode->getTemplateLine());
$transchoiceArguments->addElement($wrappingNode->getNode('node'));
$transchoiceArguments->addElement($defaultNode);
foreach ($wrappingNode->getNode('arguments') as $arg) {
Expand All @@ -91,21 +99,21 @@ public function doEnterNode(\Twig_Node $node, \Twig_Environment $env)

// remove the replacements from the test node
$testNode->setNode('arguments', clone $testNode->getNode('arguments'));
$testNode->getNode('arguments')->setNode(0, new \Twig_Node_Expression_Array([], $lineno));
$testNode->getNode('arguments')->setNode(0, new ArrayExpression([], $lineno));

// wrap the default node in a |replace filter
$defaultNode = new \Twig_Node_Expression_Filter(
$defaultNode = new FilterExpression(
clone $node->getNode('arguments')->getNode(0),
new \Twig_Node_Expression_Constant('replace', $lineno),
new \Twig_Node([
new ConstantExpression('replace', $lineno),
new Node([
clone $wrappingNode->getNode('arguments')->getNode(0),
]),
$lineno
);
}

$condition = new \Twig_Node_Expression_Conditional(
new \Twig_Node_Expression_Binary_Equal($testNode, $transNode->getNode('node'), $wrappingNode->getTemplateLine()),
$condition = new ConditionalExpression(
new EqualBinary($testNode, $transNode->getNode('node'), $wrappingNode->getTemplateLine()),
$defaultNode,
clone $wrappingNode,
$wrappingNode->getTemplateLine()
Expand All @@ -116,12 +124,12 @@ public function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
}

/**
* @param \Twig_Node $node
* @param \Twig_Environment $env
* @param Node $node
* @param Environment $env
*
* @return \Twig_Node
* @return Node
*/
public function doLeaveNode(\Twig_Node $node, \Twig_Environment $env)
public function doLeaveNode(Node $node, Environment $env)
{
return $node;
}
Expand Down
32 changes: 19 additions & 13 deletions Twig/Visitor/NormalizingNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@

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\AbstractNodeVisitor;

/**
* Performs equivalence transformations on the AST to ensure that
* subsequent visitors do not need to be aware of different syntaxes.
Expand All @@ -19,31 +25,31 @@
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
final class NormalizingNodeVisitor extends \Twig_BaseNodeVisitor
final class NormalizingNodeVisitor extends AbstractNodeVisitor
{
/**
* @param \Twig_Node $node
* @param \Twig_Environment $env
* @param Node $node
* @param Environment $env
*
* @return \Twig_Node
* @return Node
*/
protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
protected function doEnterNode(Node $node, Environment $env)
{
return $node;
}

/**
* @param \Twig_Node $node
* @param \Twig_Environment $env
* @param Node $node
* @param Environment $env
*
* @return \Twig_Node_Expression_Constant|\Twig_Node
* @return ConstantExpression|Node
*/
protected function doLeaveNode(\Twig_Node $node, \Twig_Environment $env)
protected function doLeaveNode(Node $node, Environment $env)
{
if ($node instanceof \Twig_Node_Expression_Binary_Concat
&& ($left = $node->getNode('left')) instanceof \Twig_Node_Expression_Constant
&& ($right = $node->getNode('right')) instanceof \Twig_Node_Expression_Constant) {
return new \Twig_Node_Expression_Constant($left->getAttribute('value').$right->getAttribute('value'), $left->getTemplateLine());
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;
Expand Down
25 changes: 15 additions & 10 deletions Twig/Visitor/RemovingNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@

namespace Translation\Bundle\Twig\Visitor;

use Twig\Environment;
use Twig\Node\Expression\FilterExpression;
use Twig\Node\Node;
use Twig\NodeVisitor\AbstractNodeVisitor;

/**
* Removes translation metadata filters from the AST.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
final class RemovingNodeVisitor extends \Twig_BaseNodeVisitor
final class RemovingNodeVisitor extends AbstractNodeVisitor
{
/**
* @var bool
Expand All @@ -32,14 +37,14 @@ public function setEnabled($bool)
}

/**
* @param \Twig_Node $node
* @param \Twig_Environment $env
* @param Node $node
* @param Environment $env
*
* @return \Twig_Node
* @return Node
*/
protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
protected function doEnterNode(Node $node, Environment $env)
{
if ($this->enabled && $node instanceof \Twig_Node_Expression_Filter) {
if ($this->enabled && $node instanceof FilterExpression) {
$name = $node->getNode('filter')->getAttribute('value');

if ('desc' === $name || 'meaning' === $name) {
Expand All @@ -51,12 +56,12 @@ protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
}

/**
* @param \Twig_Node $node
* @param \Twig_Environment $env
* @param Node $node
* @param Environment $env
*
* @return \Twig_Node
* @return Node
*/
protected function doLeaveNode(\Twig_Node $node, \Twig_Environment $env)
protected function doLeaveNode(Node $node, Environment $env)
{
return $node;
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"php-translation/symfony-storage": "^1.0",
"php-translation/extractor": "^1.6",
"nyholm/nsa": "^1.1",
"twig/twig": "<1.39 || ^2.0,<2.8"
"twig/twig": "^1.38,<1.39 || ^2.7,<2.8"
},
"require-dev": {
"symfony/phpunit-bridge": "^4.2",
Expand Down