Skip to content

Commit a485b2e

Browse files
KocalNyholm
authored andcommitted
chore(twig): replace class aliases by class namespaces (#290)
* chore(twig): replace class aliases by class namespaces * Tweak Twig constraints to make sure we have namespaces * Bugfix
1 parent 6e6b7b7 commit a485b2e

File tree

9 files changed

+89
-60
lines changed

9 files changed

+89
-60
lines changed

Service/Importer.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Translation\Extractor\Model\SourceCollection;
2222
use Translation\Extractor\Model\SourceLocation;
2323
use Translation\Bundle\Catalogue\Operation\ReplaceOperation;
24+
use Twig\Environment;
2425

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

4243
/**
43-
* @var \Twig_Environment
44+
* @var Environment
4445
*/
4546
private $twig;
4647

@@ -50,11 +51,11 @@ final class Importer
5051
private $defaultLocale;
5152

5253
/**
53-
* @param Extractor $extractor
54-
* @param \Twig_Environment $twig
55-
* @param string $defaultLocale
54+
* @param Extractor $extractor
55+
* @param Environment $twig
56+
* @param string $defaultLocale
5657
*/
57-
public function __construct(Extractor $extractor, \Twig_Environment $twig, $defaultLocale)
58+
public function __construct(Extractor $extractor, Environment $twig, $defaultLocale)
5859
{
5960
$this->extractor = $extractor;
6061
$this->twig = $twig;

Tests/Unit/Twig/BaseTwigTestCase.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
namespace Translation\Bundle\Tests\Unit\Twig;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Symfony\Component\Translation\MessageSelector;
16-
use Symfony\Component\Translation\IdentityTranslator;
1715
use Symfony\Bridge\Twig\Extension\TranslationExtension as SymfonyTranslationExtension;
16+
use Symfony\Component\Translation\IdentityTranslator;
17+
use Symfony\Component\Translation\MessageSelector;
1818
use Translation\Bundle\Twig\TranslationExtension;
1919
use Twig\Loader\ArrayLoader;
20+
use Twig\Environment;
21+
use Twig\Source;
2022

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

37-
return $env->parse($env->tokenize(new \Twig_Source($content, '')))->getNode('body');
39+
return $env->parse($env->tokenize(new Source($content, '')))->getNode('body');
3840
}
3941
}

Twig/EditInPlaceExtension.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\HttpFoundation\RequestStack;
1515
use Translation\Bundle\EditInPlace\ActivatorInterface;
16+
use Twig\TwigFilter;
1617

1718
/**
1819
* 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
3839
public function getFilters()
3940
{
4041
return [
41-
new \Twig_SimpleFilter('trans', [$this, 'trans'], ['is_safe_callback' => [$this, 'isSafe']]),
42-
new \Twig_SimpleFilter('transchoice', [$this, 'transchoice'], ['is_safe_callback' => [$this, 'isSafe']]),
42+
new TwigFilter('trans', [$this, 'trans'], ['is_safe_callback' => [$this, 'isSafe']]),
43+
new TwigFilter('transchoice', [$this, 'transchoice'], ['is_safe_callback' => [$this, 'isSafe']]),
4344
];
4445
}
4546

Twig/Node/Transchoice.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@
1111

1212
namespace Translation\Bundle\Twig\Node;
1313

14-
class Transchoice extends \Twig_Node_Expression
14+
use Twig\Compiler;
15+
use Twig\Node\Expression\AbstractExpression;
16+
use Twig\Node\Expression\ArrayExpression;
17+
18+
class Transchoice extends AbstractExpression
1519
{
16-
public function __construct(\Twig_Node_Expression_Array $arguments, $lineno)
20+
public function __construct(ArrayExpression $arguments, $lineno)
1721
{
1822
parent::__construct(['arguments' => $arguments], [], $lineno);
1923
}
2024

21-
public function compile(\Twig_Compiler $compiler)
25+
public function compile(Compiler $compiler)
2226
{
2327
$compiler->raw(
2428
sprintf(

Twig/TranslationExtension.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
use Translation\Bundle\Twig\Visitor\DefaultApplyingNodeVisitor;
1616
use Translation\Bundle\Twig\Visitor\NormalizingNodeVisitor;
1717
use Translation\Bundle\Twig\Visitor\RemovingNodeVisitor;
18+
use Twig\Extension\AbstractExtension;
19+
use Twig\TwigFilter;
1820

1921
/**
2022
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
2123
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
2224
*/
23-
final class TranslationExtension extends \Twig_Extension
25+
final class TranslationExtension extends AbstractExtension
2426
{
2527
/**
2628
* @var TranslatorInterface
@@ -48,8 +50,8 @@ public function __construct(TranslatorInterface $translator, $debug = false)
4850
public function getFilters()
4951
{
5052
return [
51-
new \Twig_SimpleFilter('desc', [$this, 'desc']),
52-
new \Twig_SimpleFilter('meaning', [$this, 'meaning']),
53+
new TwigFilter('desc', [$this, 'desc']),
54+
new TwigFilter('meaning', [$this, 'meaning']),
5355
];
5456
}
5557

Twig/Visitor/DefaultApplyingNodeVisitor.php

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212
namespace Translation\Bundle\Twig\Visitor;
1313

1414
use Translation\Bundle\Twig\Node\Transchoice;
15+
use Twig\Environment;
16+
use Twig\Node\Expression\ArrayExpression;
17+
use Twig\Node\Expression\Binary\EqualBinary;
18+
use Twig\Node\Expression\ConditionalExpression;
19+
use Twig\Node\Expression\ConstantExpression;
20+
use Twig\Node\Expression\FilterExpression;
21+
use Twig\Node\Node;
22+
use Twig\NodeVisitor\AbstractNodeVisitor;
1523

1624
/**
1725
* Applies the value of the "desc" filter if the "trans" filter has no
@@ -21,7 +29,7 @@
2129
*
2230
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
2331
*/
24-
final class DefaultApplyingNodeVisitor extends \Twig_BaseNodeVisitor
32+
final class DefaultApplyingNodeVisitor extends AbstractNodeVisitor
2533
{
2634
/**
2735
* @var bool
@@ -37,29 +45,29 @@ public function setEnabled($bool)
3745
}
3846

3947
/**
40-
* @param \Twig_Node $node
41-
* @param \Twig_Environment $env
48+
* @param Node $node
49+
* @param Environment $env
4250
*
43-
* @return \Twig_Node
51+
* @return Node
4452
*/
45-
public function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
53+
public function doEnterNode(Node $node, Environment $env)
4654
{
4755
if (!$this->enabled) {
4856
return $node;
4957
}
5058

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

5563
$transNode = $node->getNode('node');
56-
while ($transNode instanceof \Twig_Node_Expression_Filter
64+
while ($transNode instanceof FilterExpression
5765
&& 'trans' !== $transNode->getNode('filter')->getAttribute('value')
5866
&& 'transchoice' !== $transNode->getNode('filter')->getAttribute('value')) {
5967
$transNode = $transNode->getNode('node');
6068
}
6169

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

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

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

96104
// wrap the default node in a |replace filter
97-
$defaultNode = new \Twig_Node_Expression_Filter(
105+
$defaultNode = new FilterExpression(
98106
clone $node->getNode('arguments')->getNode(0),
99-
new \Twig_Node_Expression_Constant('replace', $lineno),
100-
new \Twig_Node([
107+
new ConstantExpression('replace', $lineno),
108+
new Node([
101109
clone $wrappingNode->getNode('arguments')->getNode(0),
102110
]),
103111
$lineno
104112
);
105113
}
106114

107-
$condition = new \Twig_Node_Expression_Conditional(
108-
new \Twig_Node_Expression_Binary_Equal($testNode, $transNode->getNode('node'), $wrappingNode->getTemplateLine()),
115+
$condition = new ConditionalExpression(
116+
new EqualBinary($testNode, $transNode->getNode('node'), $wrappingNode->getTemplateLine()),
109117
$defaultNode,
110118
clone $wrappingNode,
111119
$wrappingNode->getTemplateLine()
@@ -116,12 +124,12 @@ public function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
116124
}
117125

118126
/**
119-
* @param \Twig_Node $node
120-
* @param \Twig_Environment $env
127+
* @param Node $node
128+
* @param Environment $env
121129
*
122-
* @return \Twig_Node
130+
* @return Node
123131
*/
124-
public function doLeaveNode(\Twig_Node $node, \Twig_Environment $env)
132+
public function doLeaveNode(Node $node, Environment $env)
125133
{
126134
return $node;
127135
}

Twig/Visitor/NormalizingNodeVisitor.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111

1212
namespace Translation\Bundle\Twig\Visitor;
1313

14+
use Twig\Environment;
15+
use Twig\Node\Expression\Binary\ConcatBinary;
16+
use Twig\Node\Expression\ConstantExpression;
17+
use Twig\Node\Node;
18+
use Twig\NodeVisitor\AbstractNodeVisitor;
19+
1420
/**
1521
* Performs equivalence transformations on the AST to ensure that
1622
* subsequent visitors do not need to be aware of different syntaxes.
@@ -19,31 +25,31 @@
1925
*
2026
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
2127
*/
22-
final class NormalizingNodeVisitor extends \Twig_BaseNodeVisitor
28+
final class NormalizingNodeVisitor extends AbstractNodeVisitor
2329
{
2430
/**
25-
* @param \Twig_Node $node
26-
* @param \Twig_Environment $env
31+
* @param Node $node
32+
* @param Environment $env
2733
*
28-
* @return \Twig_Node
34+
* @return Node
2935
*/
30-
protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
36+
protected function doEnterNode(Node $node, Environment $env)
3137
{
3238
return $node;
3339
}
3440

3541
/**
36-
* @param \Twig_Node $node
37-
* @param \Twig_Environment $env
42+
* @param Node $node
43+
* @param Environment $env
3844
*
39-
* @return \Twig_Node_Expression_Constant|\Twig_Node
45+
* @return ConstantExpression|Node
4046
*/
41-
protected function doLeaveNode(\Twig_Node $node, \Twig_Environment $env)
47+
protected function doLeaveNode(Node $node, Environment $env)
4248
{
43-
if ($node instanceof \Twig_Node_Expression_Binary_Concat
44-
&& ($left = $node->getNode('left')) instanceof \Twig_Node_Expression_Constant
45-
&& ($right = $node->getNode('right')) instanceof \Twig_Node_Expression_Constant) {
46-
return new \Twig_Node_Expression_Constant($left->getAttribute('value').$right->getAttribute('value'), $left->getTemplateLine());
49+
if ($node instanceof ConcatBinary
50+
&& ($left = $node->getNode('left')) instanceof ConstantExpression
51+
&& ($right = $node->getNode('right')) instanceof ConstantExpression) {
52+
return new ConstantExpression($left->getAttribute('value').$right->getAttribute('value'), $left->getTemplateLine());
4753
}
4854

4955
return $node;

Twig/Visitor/RemovingNodeVisitor.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@
1111

1212
namespace Translation\Bundle\Twig\Visitor;
1313

14+
use Twig\Environment;
15+
use Twig\Node\Expression\FilterExpression;
16+
use Twig\Node\Node;
17+
use Twig\NodeVisitor\AbstractNodeVisitor;
18+
1419
/**
1520
* Removes translation metadata filters from the AST.
1621
*
1722
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
1823
*/
19-
final class RemovingNodeVisitor extends \Twig_BaseNodeVisitor
24+
final class RemovingNodeVisitor extends AbstractNodeVisitor
2025
{
2126
/**
2227
* @var bool
@@ -32,14 +37,14 @@ public function setEnabled($bool)
3237
}
3338

3439
/**
35-
* @param \Twig_Node $node
36-
* @param \Twig_Environment $env
40+
* @param Node $node
41+
* @param Environment $env
3742
*
38-
* @return \Twig_Node
43+
* @return Node
3944
*/
40-
protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
45+
protected function doEnterNode(Node $node, Environment $env)
4146
{
42-
if ($this->enabled && $node instanceof \Twig_Node_Expression_Filter) {
47+
if ($this->enabled && $node instanceof FilterExpression) {
4348
$name = $node->getNode('filter')->getAttribute('value');
4449

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

5358
/**
54-
* @param \Twig_Node $node
55-
* @param \Twig_Environment $env
59+
* @param Node $node
60+
* @param Environment $env
5661
*
57-
* @return \Twig_Node
62+
* @return Node
5863
*/
59-
protected function doLeaveNode(\Twig_Node $node, \Twig_Environment $env)
64+
protected function doLeaveNode(Node $node, Environment $env)
6065
{
6166
return $node;
6267
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"php-translation/symfony-storage": "^1.0",
2323
"php-translation/extractor": "^1.6",
2424
"nyholm/nsa": "^1.1",
25-
"twig/twig": "<1.39 || ^2.0,<2.8"
25+
"twig/twig": "^1.38,<1.39 || ^2.7,<2.8"
2626
},
2727
"require-dev": {
2828
"symfony/phpunit-bridge": "^4.2",

0 commit comments

Comments
 (0)