1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PHP Translation package. |
5
|
|
|
* |
6
|
|
|
* (c) PHP Translation team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Translation\Bundle\Twig\Visitor; |
13
|
|
|
|
14
|
|
|
use Translation\Bundle\Twig\Node\Transchoice; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Applies the value of the "desc" filter if the "trans" filter has no |
18
|
|
|
* translations. |
19
|
|
|
* |
20
|
|
|
* This is only active in your development environment. |
21
|
|
|
* |
22
|
|
|
* @author Johannes M. Schmitt <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class DefaultApplyingNodeVisitor extends \Twig_BaseNodeVisitor |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
private $enabled = true; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param $bool |
33
|
|
|
*/ |
34
|
|
|
public function setEnabled($bool) |
35
|
|
|
{ |
36
|
|
|
$this->enabled = (bool) $bool; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param \Twig_Node $node |
41
|
|
|
* @param \Twig_Environment $env |
42
|
|
|
* |
43
|
|
|
* @return \Twig_Node |
44
|
|
|
*/ |
45
|
6 |
|
public function doEnterNode(\Twig_Node $node, \Twig_Environment $env) |
46
|
|
|
{ |
47
|
6 |
|
if (!$this->enabled) { |
48
|
|
|
return $node; |
49
|
|
|
} |
50
|
|
|
|
51
|
6 |
|
if ($node instanceof \Twig_Node_Expression_Filter && 'desc' === $node->getNode('filter')->getAttribute('value')) { |
52
|
1 |
|
$transNode = $node->getNode('node'); |
53
|
1 |
|
while ($transNode instanceof \Twig_Node_Expression_Filter |
54
|
1 |
|
&& 'trans' !== $transNode->getNode('filter')->getAttribute('value') |
55
|
1 |
|
&& 'transchoice' !== $transNode->getNode('filter')->getAttribute('value')) { |
56
|
|
|
$transNode = $transNode->getNode('node'); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
if (!$transNode instanceof \Twig_Node_Expression_Filter) { |
60
|
|
|
throw new \RuntimeException(sprintf('The "desc" filter must be applied after a "trans", or "transchoice" filter.')); |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
$wrappingNode = $node->getNode('node'); |
64
|
1 |
|
$testNode = clone $wrappingNode; |
65
|
1 |
|
$defaultNode = $node->getNode('arguments')->getNode(0); |
66
|
|
|
|
67
|
|
|
// if the |transchoice filter is used, delegate the call to the TranslationExtension |
68
|
|
|
// so that we can catch a possible exception when the default translation has not yet |
69
|
|
|
// been extracted |
70
|
1 |
|
if ('transchoice' === $transNode->getNode('filter')->getAttribute('value')) { |
71
|
|
|
$transchoiceArguments = new \Twig_Node_Expression_Array([], $transNode->getTemplateLine()); |
72
|
|
|
$transchoiceArguments->addElement($wrappingNode->getNode('node')); |
|
|
|
|
73
|
|
|
$transchoiceArguments->addElement($defaultNode); |
|
|
|
|
74
|
|
|
foreach ($wrappingNode->getNode('arguments') as $arg) { |
75
|
|
|
$transchoiceArguments->addElement($arg); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$transchoiceNode = new Transchoice($transchoiceArguments, $transNode->getTemplateLine()); |
79
|
|
|
$node->setNode('node', $transchoiceNode); |
80
|
|
|
|
81
|
|
|
return $node; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// if the |trans filter has replacements parameters |
85
|
|
|
// (e.g. |trans({'%foo%': 'bar'})) |
|
|
|
|
86
|
1 |
|
if ($wrappingNode->getNode('arguments')->hasNode(0)) { |
87
|
1 |
|
$lineno = $wrappingNode->getTemplateLine(); |
88
|
|
|
|
89
|
|
|
// remove the replacements from the test node |
90
|
1 |
|
$testNode->setNode('arguments', clone $testNode->getNode('arguments')); |
91
|
1 |
|
$testNode->getNode('arguments')->setNode(0, new \Twig_Node_Expression_Array([], $lineno)); |
92
|
|
|
|
93
|
|
|
// wrap the default node in a |replace filter |
94
|
1 |
|
$defaultNode = new \Twig_Node_Expression_Filter( |
95
|
1 |
|
clone $node->getNode('arguments')->getNode(0), |
96
|
1 |
|
new \Twig_Node_Expression_Constant('replace', $lineno), |
97
|
1 |
|
new \Twig_Node([ |
98
|
1 |
|
clone $wrappingNode->getNode('arguments')->getNode(0), |
99
|
|
|
]), |
100
|
1 |
|
$lineno |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
$condition = new \Twig_Node_Expression_Conditional( |
105
|
1 |
|
new \Twig_Node_Expression_Binary_Equal($testNode, $transNode->getNode('node'), $wrappingNode->getTemplateLine()), |
106
|
1 |
|
$defaultNode, |
|
|
|
|
107
|
1 |
|
clone $wrappingNode, |
|
|
|
|
108
|
1 |
|
$wrappingNode->getTemplateLine() |
109
|
|
|
); |
110
|
1 |
|
$node->setNode('node', $condition); |
111
|
|
|
} |
112
|
|
|
|
113
|
6 |
|
return $node; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param \Twig_Node $node |
118
|
|
|
* @param \Twig_Environment $env |
119
|
|
|
* |
120
|
|
|
* @return \Twig_Node |
121
|
|
|
*/ |
122
|
6 |
|
public function doLeaveNode(\Twig_Node $node, \Twig_Environment $env) |
123
|
|
|
{ |
124
|
6 |
|
return $node; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return int |
129
|
|
|
*/ |
130
|
6 |
|
public function getPriority() |
131
|
|
|
{ |
132
|
6 |
|
return -2; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.