1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Translator\Extractor; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Extractor messages |
9
|
|
|
*/ |
10
|
|
|
final class ContentParser |
11
|
|
|
{ |
12
|
|
|
private string $translator = '->translate'; |
13
|
|
|
|
14
|
|
|
/** @var array<string|array{0: int, 1: string, 2: int}> */ |
15
|
|
|
private array $translatorTokens = []; |
16
|
|
|
|
17
|
|
|
private int $sizeOfTranslator = 0; |
18
|
|
|
|
19
|
|
|
private string $defaultCategory = ''; |
20
|
|
|
|
21
|
|
|
private static array $commaSpare = [ |
22
|
|
|
')' => '(', |
23
|
|
|
']' => '[', |
24
|
|
|
'}' => '{', |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @psalm-var array<array<string|array{0: int, 1: string, 2: int}>> |
29
|
|
|
*/ |
30
|
|
|
private array $skippedLines = []; |
31
|
|
|
|
32
|
12 |
|
public function __construct(?string $defaultCategory = null, ?string $translator = null) |
33
|
|
|
{ |
34
|
12 |
|
$this->defaultCategory = $defaultCategory ?? $this->defaultCategory; |
35
|
12 |
|
$this->setTranslator($translator === null ? $this->translator : $translator); |
36
|
11 |
|
} |
37
|
|
|
|
38
|
12 |
|
private function setTranslator(string $translator): void |
39
|
|
|
{ |
40
|
12 |
|
$this->translator = $translator; |
41
|
12 |
|
$translatorTokens = token_get_all('<?php ' . $this->translator); |
42
|
12 |
|
array_shift($translatorTokens); |
43
|
12 |
|
$this->translatorTokens = $translatorTokens; |
44
|
12 |
|
$this->sizeOfTranslator = count($this->translatorTokens); |
45
|
|
|
|
46
|
12 |
|
if ($this->sizeOfTranslator < 2) { |
47
|
1 |
|
throw new \RuntimeException('Translator tokens cannot be shorttest 2 tokens.'); |
48
|
|
|
} |
49
|
11 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $content |
53
|
|
|
* |
54
|
|
|
* @psalm-return array<array-key|string, mixed|non-empty-list<string>> |
55
|
|
|
* |
56
|
|
|
* @return array[] |
57
|
|
|
*/ |
58
|
10 |
|
public function extract(string $content): array |
59
|
|
|
{ |
60
|
10 |
|
$this->skippedLines = []; |
61
|
10 |
|
$tokens = token_get_all($content); |
62
|
|
|
|
63
|
10 |
|
return $this->extractMessagesFromTokens($tokens); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @psalm-param array<string|array{0: int, 1: string, 2: int}> $tokens |
68
|
|
|
* |
69
|
|
|
* @param array $tokens |
70
|
|
|
* |
71
|
|
|
* @psalm-return array<array-key|string, mixed|non-empty-list<string>> |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
10 |
|
private function extractMessagesFromTokens(array $tokens): array |
76
|
|
|
{ |
77
|
10 |
|
$messages = $buffer = []; |
78
|
10 |
|
$matchedTokensCount = $pendingParenthesisCount = 0; |
79
|
10 |
|
$isStartedTranslator = false; |
80
|
|
|
|
81
|
10 |
|
foreach ($tokens as $tokenIndex => $token) { |
82
|
10 |
|
if (in_array($token[0], [T_WHITESPACE, T_COMMENT])) { |
83
|
10 |
|
continue; |
84
|
|
|
} |
85
|
|
|
|
86
|
10 |
|
if ($isStartedTranslator) { |
87
|
10 |
|
if ($this->tokensEqual($token, ')')) { |
88
|
10 |
|
if ($pendingParenthesisCount === 0) { |
89
|
10 |
|
$messages = array_merge_recursive($messages, $this->extractParametersFromTokens($buffer)); |
90
|
10 |
|
$isStartedTranslator = false; |
91
|
10 |
|
$pendingParenthesisCount = 0; |
92
|
10 |
|
$buffer = []; |
93
|
10 |
|
continue; |
94
|
|
|
} |
95
|
10 |
|
$pendingParenthesisCount--; |
96
|
10 |
|
} elseif ($this->tokensEqual($token, '(')) { |
97
|
10 |
|
$pendingParenthesisCount++; |
98
|
|
|
} |
99
|
10 |
|
$buffer[] = $token; |
100
|
|
|
} else { |
101
|
10 |
|
if ($matchedTokensCount === $this->sizeOfTranslator) { |
102
|
10 |
|
if ($this->tokensEqual($token, '(')) { |
103
|
10 |
|
$isStartedTranslator = true; |
104
|
10 |
|
continue; |
105
|
|
|
} |
106
|
10 |
|
$matchedTokensCount = 0; |
107
|
|
|
} |
108
|
|
|
|
109
|
10 |
|
if ($this->tokensEqual($token, $this->translatorTokens[$matchedTokensCount])) { |
110
|
10 |
|
$matchedTokensCount++; |
111
|
|
|
} else { |
112
|
10 |
|
$matchedTokensCount = 0; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
10 |
|
return $messages; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param array $tokens |
122
|
|
|
* @psalm-param array<string|array{0: int, 1: string, 2: int}> $tokens |
123
|
|
|
* |
124
|
|
|
* @return array |
125
|
|
|
* @psalm-return array<array-key|string, mixed|non-empty-list<string>> |
126
|
|
|
*/ |
127
|
10 |
|
private function extractParametersFromTokens(array $tokens): array |
128
|
|
|
{ |
129
|
10 |
|
$parameters = $this->splitTokensAsParams($tokens); |
130
|
|
|
|
131
|
10 |
|
if (empty($parameters) || !isset($parameters['id'])) { |
132
|
6 |
|
$this->skippedLines[] = $tokens; |
133
|
6 |
|
return []; |
134
|
|
|
} |
135
|
|
|
|
136
|
7 |
|
$messages = [$parameters['category'] ?? $this->defaultCategory => [$parameters['id']]]; |
137
|
|
|
|
138
|
|
|
// Get translation messages from parameters |
139
|
7 |
|
if (isset($parameters['parameters'])) { |
140
|
5 |
|
$messages = array_merge_recursive($messages, $this->extractMessagesFromTokens($parameters['parameters'])); |
141
|
|
|
} |
142
|
|
|
|
143
|
7 |
|
return $messages; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @psalm-param array<string|array{0: int, 1: string, 2: int}> $tokens |
148
|
|
|
* |
149
|
|
|
* @psalm-return array{category?: null|string, id?: null|string, parameters?: null|list<array{0: int, 1: string, 2: int}|string>} |
150
|
|
|
*/ |
151
|
10 |
|
private function splitTokensAsParams(array $tokens): array |
152
|
|
|
{ |
153
|
10 |
|
$parameters = []; |
154
|
10 |
|
$parameterIndex = 0; |
155
|
10 |
|
$commaStack = []; |
156
|
|
|
|
157
|
10 |
|
foreach ($tokens as $token) { |
158
|
10 |
|
if (empty($commaStack) && $token === ',') { |
159
|
8 |
|
$parameterIndex++; |
160
|
8 |
|
continue; |
161
|
|
|
} |
162
|
10 |
|
if (is_string($token)) { |
163
|
8 |
|
if (in_array($token, self::$commaSpare)) { |
164
|
8 |
|
$commaStack[] = $token; |
165
|
8 |
|
} elseif (isset(self::$commaSpare[$token]) && array_pop($commaStack) !== self::$commaSpare[$token]) { |
166
|
|
|
return []; |
167
|
|
|
} |
168
|
|
|
} |
169
|
10 |
|
$parameters[$parameterIndex][] = $token; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return [ |
173
|
10 |
|
'id' => $this->getMessageStringFromTokens($parameters[0] ?? []), |
174
|
10 |
|
'parameters' => $parameters[1] ?? null, |
175
|
10 |
|
'category' => $this->getMessageStringFromTokens($parameters[2] ?? []), |
176
|
|
|
]; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @psalm-param array<string|array{0: int, 1: string, 2: int}> $tokens |
181
|
|
|
* |
182
|
|
|
* @return string|null |
183
|
|
|
*/ |
184
|
10 |
|
private function getMessageStringFromTokens(array $tokens): ?string |
185
|
|
|
{ |
186
|
10 |
|
if (empty($tokens) || $tokens[0][0] !== T_CONSTANT_ENCAPSED_STRING) { |
187
|
9 |
|
return null; |
188
|
|
|
} |
189
|
|
|
|
190
|
10 |
|
$fullMessage = substr($tokens[0][1], 1, -1); |
191
|
|
|
|
192
|
10 |
|
$i = 1; |
193
|
10 |
|
$countTokens = count($tokens); |
194
|
10 |
|
while ($i < $countTokens && $tokens[$i] === '.') { |
195
|
8 |
|
if ($tokens[$i + 1][0] === T_CONSTANT_ENCAPSED_STRING) { |
196
|
8 |
|
$fullMessage .= substr($tokens[$i + 1][1], 1, -1); |
197
|
6 |
|
} elseif (in_array($tokens[$i + 1][0], [T_LNUMBER, T_DNUMBER])) { |
198
|
6 |
|
$fullMessage .= $tokens[$i + 1][1]; |
199
|
|
|
} else { |
200
|
4 |
|
return null; |
201
|
|
|
} |
202
|
|
|
|
203
|
8 |
|
$i += 2; |
204
|
|
|
} |
205
|
|
|
|
206
|
7 |
|
return stripcslashes($fullMessage); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Finds out if two PHP tokens are equal. |
211
|
|
|
* |
212
|
|
|
* @param array{0: int, 1: string, 2: int}|string $a |
213
|
|
|
* @param array{0: int, 1: string, 2: int}|string $b |
214
|
|
|
* |
215
|
|
|
* @return bool |
216
|
|
|
*/ |
217
|
10 |
|
private function tokensEqual($a, $b): bool |
218
|
|
|
{ |
219
|
10 |
|
if (is_string($a)) { |
220
|
10 |
|
return $a === $b; |
221
|
|
|
} |
222
|
|
|
|
223
|
10 |
|
return $a[0] === $b[0] && $a[1] == $b[1]; |
224
|
|
|
} |
225
|
|
|
|
226
|
1 |
|
public function getDefaultCategory(): string |
227
|
|
|
{ |
228
|
1 |
|
return $this->defaultCategory; |
229
|
|
|
} |
230
|
|
|
|
231
|
2 |
|
public function setDefaultCategory(string $defaultCategory): void |
232
|
|
|
{ |
233
|
2 |
|
$this->defaultCategory = $defaultCategory; |
234
|
2 |
|
} |
235
|
|
|
|
236
|
10 |
|
public function hasSkippedLines(): bool |
237
|
|
|
{ |
238
|
10 |
|
return !empty($this->skippedLines); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @psalm-return array<array<string|array{0: int, 1: string, 2: int}>> |
243
|
|
|
*/ |
244
|
7 |
|
public function getSkippedLines(): array |
245
|
|
|
{ |
246
|
7 |
|
return $this->skippedLines; |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|