1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Translator; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Files\FileHelper; |
8
|
|
|
use Yiisoft\Files\PathMatcher\PathMatcher; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Extractor messages |
12
|
|
|
*/ |
13
|
|
|
final class TranslationExtractor |
14
|
|
|
{ |
15
|
|
|
private string $translator = '->translate'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array<string|array{0: int, 1: string, 2: int}> |
19
|
|
|
*/ |
20
|
|
|
private array $translatorTokens = []; |
21
|
|
|
private int $sizeOfTranslator = 0; |
22
|
|
|
|
23
|
|
|
private array $skippedLines = []; |
24
|
|
|
private array $skippedLinesOfFile = []; |
25
|
|
|
|
26
|
|
|
private string $defaultCategory = ''; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string[] |
30
|
|
|
*/ |
31
|
|
|
private array $only = ['**.php']; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string[] |
35
|
|
|
*/ |
36
|
|
|
private array $except = [ |
37
|
|
|
'.svn', |
38
|
|
|
'.git', |
39
|
|
|
'.gitignore', |
40
|
|
|
'.gitkeep', |
41
|
|
|
'.hgignore', |
42
|
|
|
'.hgkeep', |
43
|
|
|
'/messages', |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
private static array $commaSpare = [ |
47
|
|
|
')' => '(', |
48
|
|
|
']' => '[', |
49
|
|
|
'}' => '{', |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $path |
54
|
|
|
* @param string[]|null $only |
55
|
|
|
* @param string[]|null $except |
56
|
|
|
* |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
9 |
|
public function extract(string $path, ?array $only = null, ?array $except = null): array |
60
|
|
|
{ |
61
|
9 |
|
if (!is_dir($path)) { |
62
|
1 |
|
throw new \RuntimeException(sprintf('Directory "%s" does not exist.', $path)); |
63
|
|
|
} |
64
|
|
|
|
65
|
8 |
|
$translatorTokens = token_get_all('<?php ' . $this->translator); |
66
|
8 |
|
array_shift($translatorTokens); |
67
|
8 |
|
$this->translatorTokens = $translatorTokens; |
68
|
8 |
|
$this->sizeOfTranslator = count($this->translatorTokens); |
69
|
|
|
|
70
|
|
|
|
71
|
8 |
|
if ($this->sizeOfTranslator < 2) { |
72
|
1 |
|
throw new \RuntimeException('Translator tokens cannot be shorttest 2 tokens.'); |
73
|
|
|
} |
74
|
|
|
|
75
|
7 |
|
return $this->getMessageFromPath($path, $only === null ? $this->only : $only, $except === null ? $this->except : $except); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $path |
80
|
|
|
* @param string[] $only |
81
|
|
|
* @param string[] $except |
82
|
|
|
* |
83
|
|
|
* @psalm-return array<array-key|string, mixed|non-empty-list<string>> |
84
|
|
|
*/ |
85
|
7 |
|
private function getMessageFromPath(string $path, array $only, array $except): array |
86
|
|
|
{ |
87
|
7 |
|
$messages = []; |
88
|
|
|
|
89
|
7 |
|
$files = FileHelper::findFiles($path, [ |
90
|
7 |
|
'filter' => (new pathMatcher())->only(...$only)->except(...$except), |
91
|
|
|
'recursive' => true, |
92
|
|
|
]); |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @var string[] $files |
96
|
|
|
*/ |
97
|
7 |
|
foreach ($files as $file) { |
98
|
7 |
|
$messages = array_merge_recursive($messages, $this->extractMessagesFromFile($file)); |
99
|
|
|
} |
100
|
|
|
|
101
|
7 |
|
return $messages; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $fileName |
106
|
|
|
* |
107
|
|
|
* @psalm-return array<array-key|string, mixed|non-empty-list<string>> |
108
|
|
|
*/ |
109
|
7 |
|
private function extractMessagesFromFile(string $fileName): array |
110
|
|
|
{ |
111
|
7 |
|
$fileContent = file_get_contents($fileName); |
112
|
7 |
|
$tokens = token_get_all($fileContent); |
113
|
|
|
|
114
|
7 |
|
$this->skippedLinesOfFile = []; |
115
|
7 |
|
$messages = $this->extractMessagesFromTokens($tokens); |
116
|
|
|
|
117
|
7 |
|
if (!empty($this->skippedLinesOfFile)) { |
118
|
4 |
|
$this->skippedLines[$fileName] = $this->skippedLinesOfFile; |
119
|
|
|
} |
120
|
|
|
|
121
|
7 |
|
return $messages; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @psalm-param array<string|array{0: int, 1: string, 2: int}> $tokens |
126
|
|
|
* |
127
|
|
|
* @psalm-return array<array-key|string, mixed|non-empty-list<string>> |
128
|
|
|
*/ |
129
|
7 |
|
private function extractMessagesFromTokens(array $tokens): array |
130
|
|
|
{ |
131
|
7 |
|
$messages = $buffer = []; |
132
|
7 |
|
$matchedTokensCount = $pendingParenthesisCount = 0; |
133
|
7 |
|
$isStartedTranslator = false; |
134
|
|
|
|
135
|
7 |
|
foreach ($tokens as $tokenIndex => $token) { |
136
|
7 |
|
if (in_array($token[0], [T_WHITESPACE, T_COMMENT])) { |
137
|
7 |
|
continue; |
138
|
|
|
} |
139
|
|
|
|
140
|
7 |
|
if ($isStartedTranslator) { |
141
|
7 |
|
if ($this->tokensEqual($token, ')')) { |
142
|
7 |
|
if ($pendingParenthesisCount === 0) { |
143
|
7 |
|
$messages = array_merge_recursive($messages, $this->extractParametersFromTokens($buffer)); |
144
|
7 |
|
$isStartedTranslator = false; |
145
|
7 |
|
$pendingParenthesisCount = 0; |
146
|
7 |
|
$buffer = []; |
147
|
7 |
|
continue; |
148
|
|
|
} |
149
|
7 |
|
$pendingParenthesisCount--; |
150
|
7 |
|
} elseif ($this->tokensEqual($token, '(')) { |
151
|
7 |
|
$pendingParenthesisCount++; |
152
|
|
|
} |
153
|
7 |
|
$buffer[] = $token; |
154
|
|
|
} else { |
155
|
7 |
|
if ($matchedTokensCount === $this->sizeOfTranslator) { |
156
|
7 |
|
if ($this->tokensEqual($token, '(')) { |
157
|
7 |
|
$isStartedTranslator = true; |
158
|
7 |
|
continue; |
159
|
|
|
} |
160
|
7 |
|
$matchedTokensCount = 0; |
161
|
|
|
} |
162
|
|
|
|
163
|
7 |
|
if ($this->tokensEqual($token, $this->translatorTokens[$matchedTokensCount])) { |
164
|
7 |
|
$matchedTokensCount++; |
165
|
|
|
} else { |
166
|
7 |
|
$matchedTokensCount = 0; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
7 |
|
return $messages; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param array $tokens |
176
|
|
|
* @psalm-param array<string|array{0: int, 1: string, 2: int}> $tokens |
177
|
|
|
* |
178
|
|
|
* @return array |
179
|
|
|
* @psalm-return array<array-key|string, mixed|non-empty-list<string>> |
180
|
|
|
*/ |
181
|
7 |
|
private function extractParametersFromTokens(array $tokens): array |
182
|
|
|
{ |
183
|
7 |
|
$parameters = $this->splitTokensAsParams($tokens); |
184
|
|
|
|
185
|
7 |
|
if (!isset($parameters['id'])) { |
186
|
4 |
|
$this->skippedLinesOfFile[] = $tokens; |
187
|
4 |
|
return []; |
188
|
|
|
} |
189
|
|
|
|
190
|
5 |
|
$messages = [$parameters['category'] ?? $this->defaultCategory => [$parameters['id']]]; |
191
|
|
|
|
192
|
|
|
// Get translation messages from parameters |
193
|
5 |
|
if (isset($parameters['parameters'])) { |
194
|
4 |
|
$messages = array_merge_recursive($messages, $this->extractMessagesFromTokens($parameters['parameters'])); |
195
|
|
|
} |
196
|
|
|
|
197
|
5 |
|
return $messages; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @psalm-param array<string|array{0: int, 1: string, 2: int}> $tokens |
202
|
|
|
* |
203
|
|
|
* @psalm-return array{category?: null|string, id?: null|string, parameters?: null|list<array{0: int, 1: string, 2: int}|string>} |
204
|
|
|
*/ |
205
|
7 |
|
private function splitTokensAsParams(array $tokens): array |
206
|
|
|
{ |
207
|
7 |
|
$parameters = []; |
208
|
7 |
|
$parameterIndex = 0; |
209
|
7 |
|
$commaStack = []; |
210
|
|
|
|
211
|
7 |
|
foreach ($tokens as $token) { |
212
|
7 |
|
if (empty($commaStack) && $token === ',') { |
213
|
6 |
|
$parameterIndex++; |
214
|
6 |
|
continue; |
215
|
|
|
} |
216
|
7 |
|
if (is_string($token)) { |
217
|
6 |
|
if (in_array($token, self::$commaSpare)) { |
218
|
6 |
|
$commaStack[] = $token; |
219
|
6 |
|
} elseif (isset(self::$commaSpare[$token])&& array_pop($commaStack) !== self::$commaSpare[$token]) { |
220
|
|
|
return []; |
221
|
|
|
} |
222
|
|
|
} |
223
|
7 |
|
$parameters[$parameterIndex][] = $token; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return [ |
227
|
7 |
|
'id' => $this->getMessageStringFromTokens($parameters[0] ?? []), |
228
|
7 |
|
'parameters' => $parameters[1] ?? null, |
229
|
7 |
|
'category' => $this->getMessageStringFromTokens($parameters[2] ?? []), |
230
|
|
|
]; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @psalm-param array<string|array{0: int, 1: string, 2: int}> $tokens |
235
|
|
|
* |
236
|
|
|
* @return string|null |
237
|
|
|
*/ |
238
|
7 |
|
private function getMessageStringFromTokens(array $tokens): ?string |
239
|
|
|
{ |
240
|
7 |
|
if (empty($tokens) || $tokens[0][0] !== T_CONSTANT_ENCAPSED_STRING) { |
241
|
6 |
|
return null; |
242
|
|
|
} |
243
|
|
|
|
244
|
7 |
|
$fullMessage = substr($tokens[0][1], 1, -1); |
245
|
|
|
|
246
|
7 |
|
$i = 1; |
247
|
7 |
|
$countTokens = count($tokens); |
248
|
7 |
|
while ($i < $countTokens && $tokens[$i] === '.') { |
249
|
6 |
|
if ($tokens[$i + 1][0] === T_CONSTANT_ENCAPSED_STRING) { |
250
|
6 |
|
$fullMessage .= substr($tokens[$i + 1][1], 1, -1); |
251
|
4 |
|
} elseif (in_array($tokens[$i + 1][0], [T_LNUMBER, T_DNUMBER])) { |
252
|
4 |
|
$fullMessage .= $tokens[$i + 1][1]; |
253
|
|
|
} else { |
254
|
3 |
|
return null; |
255
|
|
|
} |
256
|
|
|
|
257
|
6 |
|
$i += 2; |
258
|
|
|
} |
259
|
|
|
|
260
|
5 |
|
return stripcslashes($fullMessage); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Finds out if two PHP tokens are equal. |
265
|
|
|
* |
266
|
|
|
* @param array{0: int, 1: string, 2: int}|string $a |
267
|
|
|
* @param array{0: int, 1: string, 2: int}|string $b |
268
|
|
|
* |
269
|
|
|
* @return bool |
270
|
|
|
*/ |
271
|
7 |
|
private function tokensEqual($a, $b): bool |
272
|
|
|
{ |
273
|
7 |
|
if (is_string($a)) { |
274
|
7 |
|
return $a === $b; |
275
|
|
|
} |
276
|
|
|
|
277
|
7 |
|
return $a[0] === $b[0] && $a[1] == $b[1]; |
278
|
|
|
} |
279
|
|
|
|
280
|
7 |
|
public function hasSkippedLines(): bool |
281
|
|
|
{ |
282
|
7 |
|
return !empty($this->skippedLines); |
283
|
|
|
} |
284
|
|
|
|
285
|
4 |
|
public function getSkippedLines(): array |
286
|
|
|
{ |
287
|
4 |
|
return $this->skippedLines; |
288
|
|
|
} |
289
|
|
|
|
290
|
2 |
|
public function getDefaultCategory(): string |
291
|
|
|
{ |
292
|
2 |
|
return $this->defaultCategory; |
293
|
|
|
} |
294
|
|
|
|
295
|
6 |
|
public function setDefaultCategory(string $defaultCategory): void |
296
|
|
|
{ |
297
|
6 |
|
$this->defaultCategory = $defaultCategory; |
298
|
6 |
|
} |
299
|
|
|
|
300
|
2 |
|
public function withTranslator(string $translator): self |
301
|
|
|
{ |
302
|
2 |
|
$new = clone $this; |
303
|
2 |
|
$new->translator = $translator; |
304
|
2 |
|
return $new; |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|