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\Extractor\Visitor\Twig; |
13
|
|
|
|
14
|
|
|
use Symfony\Bridge\Twig\Node\TransNode; |
15
|
|
|
use Translation\Extractor\Model\Error; |
16
|
|
|
use Translation\Extractor\Model\SourceCollection; |
17
|
|
|
use Translation\Extractor\Model\SourceLocation; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The Worker tha actually extract the translations. |
21
|
|
|
* |
22
|
|
|
* @author Tobias Nyholm <[email protected]> |
23
|
|
|
* @author Fabien Potencier <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
final class Worker |
26
|
|
|
{ |
27
|
|
|
const UNDEFINED_DOMAIN = 'messages'; |
28
|
|
|
|
29
|
|
|
private $stack = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param \Twig_Node|\Twig_NodeInterface $node |
33
|
|
|
* @param SourceCollection $collection |
34
|
|
|
* @param callable $getAbsoluteFilePath |
35
|
|
|
* |
36
|
|
|
* @return \Twig_Node|\Twig_NodeInterface |
37
|
|
|
*/ |
38
|
6 |
|
public function work($node, SourceCollection $collection, callable $getAbsoluteFilePath) |
39
|
|
|
{ |
40
|
6 |
|
$this->stack[] = $node; |
41
|
6 |
|
if ($node instanceof \Twig_Node_Expression_Filter && $node->getNode('node') instanceof \Twig_Node_Expression_Constant) { |
42
|
4 |
|
$domain = null; |
43
|
4 |
|
if ('trans' === $node->getNode('filter')->getAttribute('value')) { |
44
|
4 |
|
$domain = $this->getReadDomainFromArguments($node->getNode('arguments'), 1); |
45
|
|
|
} elseif ('transchoice' === $node->getNode('filter')->getAttribute('value')) { |
46
|
|
|
$domain = $this->getReadDomainFromArguments($node->getNode('arguments'), 2); |
47
|
|
|
} |
48
|
|
|
|
49
|
4 |
|
if ($domain) { |
|
|
|
|
50
|
|
|
try { |
51
|
4 |
|
$context = $this->extractContextFromJoinedFilters(); |
52
|
2 |
|
} catch (\LogicException $e) { |
53
|
2 |
|
$collection->addError(new Error($e->getMessage(), $getAbsoluteFilePath(), $node->getTemplateLine())); |
54
|
|
|
} |
55
|
4 |
|
$context['domain'] = $domain; |
56
|
4 |
|
$collection->addLocation( |
57
|
4 |
|
new SourceLocation( |
58
|
4 |
|
$node->getNode('node')->getAttribute('value'), |
59
|
4 |
|
$getAbsoluteFilePath(), |
60
|
4 |
|
$node->getTemplateLine(), |
61
|
4 |
|
$context |
62
|
|
|
) |
63
|
|
|
); |
64
|
|
|
} |
65
|
6 |
|
} elseif ($node instanceof TransNode) { |
66
|
|
|
// extract trans nodes |
67
|
3 |
|
$domain = self::UNDEFINED_DOMAIN; |
68
|
3 |
|
if ($node->hasNode('domain') && null !== $node->getNode('domain')) { |
69
|
2 |
|
$domain = $this->getReadDomainFromNode($node->getNode('domain')); |
70
|
|
|
} |
71
|
|
|
|
72
|
3 |
|
$collection->addLocation(new SourceLocation( |
73
|
3 |
|
$node->getNode('body')->getAttribute('data'), |
74
|
3 |
|
$getAbsoluteFilePath(), |
75
|
3 |
|
$node->getTemplateLine(), |
76
|
3 |
|
['domain' => $domain] |
77
|
|
|
)); |
78
|
|
|
} |
79
|
|
|
|
80
|
6 |
|
return $node; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
4 |
|
private function extractContextFromJoinedFilters() |
87
|
|
|
{ |
88
|
4 |
|
$context = []; |
89
|
4 |
|
for ($i = count($this->stack) - 2; $i >= 0; $i -= 1) { |
90
|
4 |
|
if (!$this->stack[$i] instanceof \Twig_Node_Expression_Filter) { |
91
|
3 |
|
break; |
92
|
|
|
} |
93
|
4 |
|
$name = $this->stack[$i]->getNode('filter')->getAttribute('value'); |
94
|
4 |
|
if ('trans' === $name) { |
95
|
|
|
break; |
96
|
4 |
|
} elseif ('desc' === $name) { |
97
|
3 |
|
$arguments = $this->stack[$i]->getNode('arguments'); |
98
|
3 |
|
if (!$arguments->hasNode(0)) { |
99
|
2 |
|
throw new \LogicException(sprintf('The "%s" filter requires exactly one argument, the description text.', $name)); |
100
|
|
|
} |
101
|
3 |
|
$text = $arguments->getNode(0); |
102
|
3 |
|
if (!$text instanceof \Twig_Node_Expression_Constant) { |
103
|
2 |
|
throw new \LogicException(sprintf('The first argument of the "%s" filter must be a constant expression, such as a string.', $name)); |
104
|
|
|
} |
105
|
2 |
|
$context['desc'] = $text->getAttribute('value'); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
3 |
|
return $context; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param \Twig_Node $arguments |
114
|
|
|
* @param int $index |
115
|
|
|
* |
116
|
|
|
* @return string|null |
117
|
|
|
*/ |
118
|
4 |
|
private function getReadDomainFromArguments(\Twig_Node $arguments, $index) |
119
|
|
|
{ |
120
|
4 |
View Code Duplication |
if ($arguments->hasNode('domain')) { |
|
|
|
|
121
|
|
|
$argument = $arguments->getNode('domain'); |
122
|
4 |
|
} elseif ($arguments->hasNode($index)) { |
123
|
|
|
$argument = $arguments->getNode($index); |
124
|
|
|
} else { |
125
|
4 |
|
return self::UNDEFINED_DOMAIN; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $this->getReadDomainFromNode($argument); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param \Twig_Node $node |
133
|
|
|
* |
134
|
|
|
* @return string|null |
135
|
|
|
*/ |
136
|
2 |
|
private function getReadDomainFromNode(\Twig_Node $node) |
137
|
|
|
{ |
138
|
2 |
|
if ($node instanceof \Twig_Node_Expression_Constant) { |
139
|
2 |
|
return $node->getAttribute('value'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return self::UNDEFINED_DOMAIN; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: