From 071bbdc67a9637a947fc1a4370d429dacb37b89a Mon Sep 17 00:00:00 2001 From: Hugo Alliaume Date: Wed, 13 Mar 2019 06:47:16 +0100 Subject: [PATCH 1/3] chore(twig): replace class aliases by class namespaces --- Service/Importer.php | 11 ++--- Tests/Unit/Twig/BaseTwigTestCase.php | 11 +++-- Twig/EditInPlaceExtension.php | 5 ++- Twig/Node/Transchoice.php | 10 +++-- Twig/TranslationExtension.php | 8 ++-- Twig/Visitor/DefaultApplyingNodeVisitor.php | 46 ++++++++++++--------- Twig/Visitor/NormalizingNodeVisitor.php | 32 ++++++++------ Twig/Visitor/RemovingNodeVisitor.php | 25 ++++++----- 8 files changed, 89 insertions(+), 59 deletions(-) diff --git a/Service/Importer.php b/Service/Importer.php index 29119b89..76da2352 100644 --- a/Service/Importer.php +++ b/Service/Importer.php @@ -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. @@ -40,7 +41,7 @@ final class Importer private $config; /** - * @var \Twig_Environment + * @var Environment */ private $twig; @@ -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; diff --git a/Tests/Unit/Twig/BaseTwigTestCase.php b/Tests/Unit/Twig/BaseTwigTestCase.php index 25f67fa6..95877b27 100644 --- a/Tests/Unit/Twig/BaseTwigTestCase.php +++ b/Tests/Unit/Twig/BaseTwigTestCase.php @@ -12,11 +12,14 @@ 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\Loader\ArrayLoader; +use Twig\Source; /** * @author Johannes M. Schmitt @@ -30,10 +33,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, null)))->getNode('body'); } } diff --git a/Twig/EditInPlaceExtension.php b/Twig/EditInPlaceExtension.php index 14eb9923..a37c6edd 100644 --- a/Twig/EditInPlaceExtension.php +++ b/Twig/EditInPlaceExtension.php @@ -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 @@ -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']]), ]; } diff --git a/Twig/Node/Transchoice.php b/Twig/Node/Transchoice.php index 654688f3..734b1ab3 100644 --- a/Twig/Node/Transchoice.php +++ b/Twig/Node/Transchoice.php @@ -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( diff --git a/Twig/TranslationExtension.php b/Twig/TranslationExtension.php index 02c556a2..497dcb4a 100644 --- a/Twig/TranslationExtension.php +++ b/Twig/TranslationExtension.php @@ -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 * @author Tobias Nyholm */ -final class TranslationExtension extends \Twig_Extension +final class TranslationExtension extends AbstractExtension { /** * @var TranslatorInterface @@ -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']), ]; } diff --git a/Twig/Visitor/DefaultApplyingNodeVisitor.php b/Twig/Visitor/DefaultApplyingNodeVisitor.php index d097e016..c64a3eab 100644 --- a/Twig/Visitor/DefaultApplyingNodeVisitor.php +++ b/Twig/Visitor/DefaultApplyingNodeVisitor.php @@ -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 @@ -21,7 +29,7 @@ * * @author Johannes M. Schmitt */ -final class DefaultApplyingNodeVisitor extends \Twig_BaseNodeVisitor +final class DefaultApplyingNodeVisitor extends AbstractNodeVisitor { /** * @var bool @@ -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.')); } @@ -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) { @@ -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() @@ -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; } diff --git a/Twig/Visitor/NormalizingNodeVisitor.php b/Twig/Visitor/NormalizingNodeVisitor.php index 9b600bd4..ed10080b 100644 --- a/Twig/Visitor/NormalizingNodeVisitor.php +++ b/Twig/Visitor/NormalizingNodeVisitor.php @@ -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. @@ -19,31 +25,31 @@ * * @author Johannes M. Schmitt */ -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; diff --git a/Twig/Visitor/RemovingNodeVisitor.php b/Twig/Visitor/RemovingNodeVisitor.php index ff69a3f3..7de5c425 100644 --- a/Twig/Visitor/RemovingNodeVisitor.php +++ b/Twig/Visitor/RemovingNodeVisitor.php @@ -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 */ -final class RemovingNodeVisitor extends \Twig_BaseNodeVisitor +final class RemovingNodeVisitor extends AbstractNodeVisitor { /** * @var bool @@ -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) { @@ -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; } From 20c41c078c93d43cdf5c537da5e70ac950a02c05 Mon Sep 17 00:00:00 2001 From: bocharsky-bw Date: Tue, 25 Jun 2019 13:51:33 +0300 Subject: [PATCH 2/3] Tweak Twig constraints to make sure we have namespaces --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 586f7b64..8ee8221a 100644 --- a/composer.json +++ b/composer.json @@ -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", From e1d84fe1e2e80574f47ec7df468a6a9bfe6fa625 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sat, 20 Jul 2019 12:56:42 +0200 Subject: [PATCH 3/3] Bugfix --- Tests/Unit/Twig/BaseTwigTestCase.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Tests/Unit/Twig/BaseTwigTestCase.php b/Tests/Unit/Twig/BaseTwigTestCase.php index 95877b27..c2c8793a 100644 --- a/Tests/Unit/Twig/BaseTwigTestCase.php +++ b/Tests/Unit/Twig/BaseTwigTestCase.php @@ -18,7 +18,6 @@ use Translation\Bundle\Twig\TranslationExtension; use Twig\Loader\ArrayLoader; use Twig\Environment; -use Twig\Loader\ArrayLoader; use Twig\Source; /** @@ -37,6 +36,6 @@ final protected function parse($file, $debug = false) $env->addExtension(new SymfonyTranslationExtension($translator = new IdentityTranslator(new MessageSelector()))); $env->addExtension(new TranslationExtension($translator, $debug)); - return $env->parse($env->tokenize(new Source($content, null)))->getNode('body'); + return $env->parse($env->tokenize(new Source($content, '')))->getNode('body'); } }