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\Php; |
13
|
|
|
|
14
|
|
|
use PhpParser\Node; |
15
|
|
|
use PhpParser\NodeVisitor; |
16
|
|
|
use Translation\Extractor\Model\SourceLocation; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Extract translations from classes implementing |
20
|
|
|
* Translation\Extractor\Model\SourceLocation\TranslationSourceLocationContainer. |
21
|
|
|
* |
22
|
|
|
* @author Tobias Nyholm <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class SourceLocationContainerVisitor extends BasePHPVisitor implements NodeVisitor |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $namespace = ''; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $useStatements = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
2 |
|
public function beforeTraverse(array $nodes): ?Node |
40
|
|
|
{ |
41
|
2 |
|
return null; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
2 |
|
public function enterNode(Node $node): ?Node |
48
|
|
|
{ |
49
|
2 |
|
if ($node instanceof Node\Stmt\Namespace_) { |
50
|
2 |
|
if (isset($node->name)) { |
51
|
|
|
// Save namespace of this class for later. |
52
|
2 |
|
$this->namespace = implode('\\', $node->name->parts); |
53
|
|
|
} |
54
|
2 |
|
$this->useStatements = []; |
55
|
|
|
|
56
|
2 |
|
return null; |
57
|
|
|
} |
58
|
|
|
|
59
|
2 |
|
if ($node instanceof Node\Stmt\UseUse) { |
60
|
2 |
|
$key = isset($node->alias) ? $node->alias : $node->name->parts[\count($node->name->parts) - 1]; |
61
|
2 |
|
$this->useStatements[(string) $key] = implode('\\', $node->name->parts); |
62
|
|
|
|
63
|
2 |
|
return null; |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
if (!$node instanceof Node\Stmt\Class_) { |
67
|
2 |
|
return null; |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
|
$isContainer = false; |
71
|
2 |
|
foreach ($node->implements as $interface) { |
72
|
2 |
|
$name = implode('\\', $interface->parts); |
73
|
2 |
|
if (isset($this->useStatements[$name])) { |
74
|
2 |
|
$name = $this->useStatements[$name]; |
75
|
|
|
} |
76
|
|
|
|
77
|
2 |
|
if ('Translation\Extractor\TranslationSourceLocationContainer' === $name) { |
78
|
2 |
|
$isContainer = true; |
79
|
|
|
|
80
|
2 |
|
break; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
2 |
|
if (!$isContainer) { |
85
|
1 |
|
return null; |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
$sourceLocations = \call_user_func([$this->namespace.'\\'.$node->name, 'getTranslationSourceLocations']); |
89
|
|
|
|
90
|
2 |
|
foreach ($sourceLocations as $sourceLocation) { |
91
|
2 |
|
if (!$sourceLocation instanceof SourceLocation) { |
92
|
|
|
throw new \RuntimeException(sprintf('%s::getTranslationSourceLocations() was expected to return an array of SourceLocations, but got an array which contains an item of type %s.', $this->namespace.'\\'.$node->name, \gettype($sourceLocation))); |
93
|
|
|
} |
94
|
|
|
|
95
|
2 |
|
$this->collection->addLocation($sourceLocation); |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
return null; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
2 |
|
public function leaveNode(Node $node): ?Node |
105
|
|
|
{ |
106
|
2 |
|
return null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
2 |
|
public function afterTraverse(array $nodes): ?Node |
113
|
|
|
{ |
114
|
2 |
|
return null; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|