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