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