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\SourceCollection; |
16
|
|
|
use Translation\Extractor\Model\SourceLocation; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The Worker tha actually extract the translations. |
20
|
|
|
* |
21
|
|
|
* @author Tobias Nyholm <[email protected]> |
22
|
|
|
* @author Fabien Potencier <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class Worker |
25
|
|
|
{ |
26
|
|
|
const UNDEFINED_DOMAIN = 'messages'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param \Twig_Node|\Twig_NodeInterface $node |
30
|
|
|
* @param SourceCollection $collection |
31
|
|
|
* @param callable $getAbsoluteFilePath |
32
|
|
|
* |
33
|
|
|
* @return \Twig_Node|\Twig_NodeInterface |
34
|
|
|
*/ |
35
|
4 |
|
public function work($node, SourceCollection $collection, callable $getAbsoluteFilePath) |
36
|
|
|
{ |
37
|
|
|
if ( |
38
|
4 |
|
$node instanceof \Twig_Node_Expression_Filter && |
39
|
4 |
|
'trans' === $node->getNode('filter')->getAttribute('value') && |
40
|
2 |
|
$node->getNode('node') instanceof \Twig_Node_Expression_Constant |
41
|
4 |
|
) { |
42
|
|
|
// extract constant nodes with a trans filter |
43
|
2 |
|
$collection->addLocation(new SourceLocation( |
44
|
2 |
|
$node->getNode('node')->getAttribute('value'), |
45
|
2 |
|
$getAbsoluteFilePath(), |
46
|
2 |
|
$node->getTemplateLine(), |
47
|
2 |
|
['domain' => $this->getReadDomainFromArguments($node->getNode('arguments'), 1)] |
48
|
2 |
|
)); |
49
|
2 |
|
} elseif ( |
50
|
4 |
|
$node instanceof \Twig_Node_Expression_Filter && |
51
|
4 |
|
'transchoice' === $node->getNode('filter')->getAttribute('value') && |
52
|
|
|
$node->getNode('node') instanceof \Twig_Node_Expression_Constant |
53
|
4 |
|
) { |
54
|
|
|
// extract constant nodes with a trans filter |
55
|
|
|
$collection->addLocation(new SourceLocation( |
56
|
|
|
$node->getNode('node')->getAttribute('value'), |
57
|
|
|
$getAbsoluteFilePath(), |
58
|
|
|
$node->getTemplateLine(), |
59
|
|
|
['domain' => $this->getReadDomainFromArguments($node->getNode('arguments'), 2)] |
60
|
|
|
)); |
61
|
4 |
|
} elseif ($node instanceof TransNode) { |
62
|
|
|
// extract trans nodes |
63
|
3 |
|
$collection->addLocation(new SourceLocation( |
64
|
3 |
|
$node->getNode('body')->getAttribute('data'), |
65
|
3 |
|
$getAbsoluteFilePath(), |
66
|
3 |
|
$node->getTemplateLine(), |
67
|
3 |
|
['domain' => $node->hasNode('domain') ? $this->getReadDomainFromNode($node->getNode('domain')) : self::UNDEFINED_DOMAIN] |
68
|
3 |
|
)); |
69
|
3 |
|
} |
70
|
|
|
|
71
|
4 |
|
return $node; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param \Twig_Node $arguments |
76
|
|
|
* @param int $index |
77
|
|
|
* |
78
|
|
|
* @return string|null |
79
|
|
|
*/ |
80
|
2 |
|
private function getReadDomainFromArguments(\Twig_Node $arguments, $index) |
81
|
|
|
{ |
82
|
2 |
View Code Duplication |
if ($arguments->hasNode('domain')) { |
|
|
|
|
83
|
|
|
$argument = $arguments->getNode('domain'); |
84
|
2 |
|
} elseif ($arguments->hasNode($index)) { |
85
|
|
|
$argument = $arguments->getNode($index); |
86
|
|
|
} else { |
87
|
2 |
|
return self::UNDEFINED_DOMAIN; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this->getReadDomainFromNode($argument); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param \Twig_Node $node |
95
|
|
|
* |
96
|
|
|
* @return string|null |
97
|
|
|
*/ |
98
|
2 |
|
private function getReadDomainFromNode(\Twig_Node $node) |
99
|
|
|
{ |
100
|
2 |
|
if ($node instanceof \Twig_Node_Expression_Constant) { |
101
|
2 |
|
return $node->getAttribute('value'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return self::UNDEFINED_DOMAIN; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.