|
11 | 11 |
|
12 | 12 | namespace Symfony\Bundle\FrameworkBundle\Translation; |
13 | 13 |
|
14 | | -use Symfony\Component\Finder\Finder; |
15 | | -use Symfony\Component\Translation\Extractor\AbstractFileExtractor; |
16 | | -use Symfony\Component\Translation\MessageCatalogue; |
17 | | -use Symfony\Component\Translation\Extractor\ExtractorInterface; |
| 14 | +use Symfony\Component\Translation\Extractor\PhpExtractor as NewPhpExtractor; |
| 15 | + |
| 16 | +@trigger_error(sprintf('The class "%s" is deprecated since version 3.4 and will be removed in 4.0. Use "%s" instead. ', PhpExtractor::class, NewPhpExtractor::class), E_USER_DEPRECATED); |
18 | 17 |
|
19 | 18 | /** |
20 | | - * PhpExtractor extracts translation messages from a PHP template. |
21 | | - * |
22 | | - * @author Michel Salib <[email protected]> |
| 19 | + * @deprecated since version 3.4 and will be removed in 4.0. Use Symfony\Component\Translation\Extractor\PhpExtractor instead |
23 | 20 | */ |
24 | | -class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface |
| 21 | +class PhpExtractor extends NewPhpExtractor |
25 | 22 | { |
26 | | - const MESSAGE_TOKEN = 300; |
27 | | - const METHOD_ARGUMENTS_TOKEN = 1000; |
28 | | - const DOMAIN_TOKEN = 1001; |
29 | | - |
30 | | - /** |
31 | | - * Prefix for new found message. |
32 | | - * |
33 | | - * @var string |
34 | | - */ |
35 | | - private $prefix = ''; |
36 | | - |
37 | | - /** |
38 | | - * The sequence that captures translation messages. |
39 | | - * |
40 | | - * @var array |
41 | | - */ |
42 | | - protected $sequences = array( |
43 | | - array( |
44 | | - '->', |
45 | | - 'trans', |
46 | | - '(', |
47 | | - self::MESSAGE_TOKEN, |
48 | | - ',', |
49 | | - self::METHOD_ARGUMENTS_TOKEN, |
50 | | - ',', |
51 | | - self::DOMAIN_TOKEN, |
52 | | - ), |
53 | | - array( |
54 | | - '->', |
55 | | - 'transChoice', |
56 | | - '(', |
57 | | - self::MESSAGE_TOKEN, |
58 | | - ',', |
59 | | - self::METHOD_ARGUMENTS_TOKEN, |
60 | | - ',', |
61 | | - self::METHOD_ARGUMENTS_TOKEN, |
62 | | - ',', |
63 | | - self::DOMAIN_TOKEN, |
64 | | - ), |
65 | | - array( |
66 | | - '->', |
67 | | - 'trans', |
68 | | - '(', |
69 | | - self::MESSAGE_TOKEN, |
70 | | - ), |
71 | | - array( |
72 | | - '->', |
73 | | - 'transChoice', |
74 | | - '(', |
75 | | - self::MESSAGE_TOKEN, |
76 | | - ), |
77 | | - ); |
78 | | - |
79 | | - /** |
80 | | - * {@inheritdoc} |
81 | | - */ |
82 | | - public function extract($resource, MessageCatalogue $catalog) |
83 | | - { |
84 | | - $files = $this->extractFiles($resource); |
85 | | - foreach ($files as $file) { |
86 | | - $this->parseTokens(token_get_all(file_get_contents($file)), $catalog); |
87 | | - |
88 | | - if (\PHP_VERSION_ID >= 70000) { |
89 | | - // PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098 |
90 | | - gc_mem_caches(); |
91 | | - } |
92 | | - } |
93 | | - } |
94 | | - |
95 | | - /** |
96 | | - * {@inheritdoc} |
97 | | - */ |
98 | | - public function setPrefix($prefix) |
99 | | - { |
100 | | - $this->prefix = $prefix; |
101 | | - } |
102 | | - |
103 | | - /** |
104 | | - * Normalizes a token. |
105 | | - * |
106 | | - * @param mixed $token |
107 | | - * |
108 | | - * @return string |
109 | | - */ |
110 | | - protected function normalizeToken($token) |
111 | | - { |
112 | | - if (isset($token[1]) && 'b"' !== $token) { |
113 | | - return $token[1]; |
114 | | - } |
115 | | - |
116 | | - return $token; |
117 | | - } |
118 | | - |
119 | | - /** |
120 | | - * Seeks to a non-whitespace token. |
121 | | - */ |
122 | | - private function seekToNextRelevantToken(\Iterator $tokenIterator) |
123 | | - { |
124 | | - for (; $tokenIterator->valid(); $tokenIterator->next()) { |
125 | | - $t = $tokenIterator->current(); |
126 | | - if (T_WHITESPACE !== $t[0]) { |
127 | | - break; |
128 | | - } |
129 | | - } |
130 | | - } |
131 | | - |
132 | | - private function skipMethodArgument(\Iterator $tokenIterator) |
133 | | - { |
134 | | - $openBraces = 0; |
135 | | - |
136 | | - for (; $tokenIterator->valid(); $tokenIterator->next()) { |
137 | | - $t = $tokenIterator->current(); |
138 | | - |
139 | | - if ('[' === $t[0] || '(' === $t[0]) { |
140 | | - ++$openBraces; |
141 | | - } |
142 | | - |
143 | | - if (']' === $t[0] || ')' === $t[0]) { |
144 | | - --$openBraces; |
145 | | - } |
146 | | - |
147 | | - if ((0 === $openBraces && ',' === $t[0]) || (-1 === $openBraces && ')' === $t[0])) { |
148 | | - break; |
149 | | - } |
150 | | - } |
151 | | - } |
152 | | - |
153 | | - /** |
154 | | - * Extracts the message from the iterator while the tokens |
155 | | - * match allowed message tokens. |
156 | | - */ |
157 | | - private function getValue(\Iterator $tokenIterator) |
158 | | - { |
159 | | - $message = ''; |
160 | | - $docToken = ''; |
161 | | - |
162 | | - for (; $tokenIterator->valid(); $tokenIterator->next()) { |
163 | | - $t = $tokenIterator->current(); |
164 | | - if (!isset($t[1])) { |
165 | | - break; |
166 | | - } |
167 | | - |
168 | | - switch ($t[0]) { |
169 | | - case T_START_HEREDOC: |
170 | | - $docToken = $t[1]; |
171 | | - break; |
172 | | - case T_ENCAPSED_AND_WHITESPACE: |
173 | | - case T_CONSTANT_ENCAPSED_STRING: |
174 | | - $message .= $t[1]; |
175 | | - break; |
176 | | - case T_END_HEREDOC: |
177 | | - return PhpStringTokenParser::parseDocString($docToken, $message); |
178 | | - default: |
179 | | - break 2; |
180 | | - } |
181 | | - } |
182 | | - |
183 | | - if ($message) { |
184 | | - $message = PhpStringTokenParser::parse($message); |
185 | | - } |
186 | | - |
187 | | - return $message; |
188 | | - } |
189 | | - |
190 | | - /** |
191 | | - * Extracts trans message from PHP tokens. |
192 | | - * |
193 | | - * @param array $tokens |
194 | | - * @param MessageCatalogue $catalog |
195 | | - */ |
196 | | - protected function parseTokens($tokens, MessageCatalogue $catalog) |
197 | | - { |
198 | | - $tokenIterator = new \ArrayIterator($tokens); |
199 | | - |
200 | | - for ($key = 0; $key < $tokenIterator->count(); ++$key) { |
201 | | - foreach ($this->sequences as $sequence) { |
202 | | - $message = ''; |
203 | | - $domain = 'messages'; |
204 | | - $tokenIterator->seek($key); |
205 | | - |
206 | | - foreach ($sequence as $sequenceKey => $item) { |
207 | | - $this->seekToNextRelevantToken($tokenIterator); |
208 | | - |
209 | | - if ($this->normalizeToken($tokenIterator->current()) === $item) { |
210 | | - $tokenIterator->next(); |
211 | | - continue; |
212 | | - } elseif (self::MESSAGE_TOKEN === $item) { |
213 | | - $message = $this->getValue($tokenIterator); |
214 | | - |
215 | | - if (count($sequence) === ($sequenceKey + 1)) { |
216 | | - break; |
217 | | - } |
218 | | - } elseif (self::METHOD_ARGUMENTS_TOKEN === $item) { |
219 | | - $this->skipMethodArgument($tokenIterator); |
220 | | - } elseif (self::DOMAIN_TOKEN === $item) { |
221 | | - $domain = $this->getValue($tokenIterator); |
222 | | - |
223 | | - break; |
224 | | - } else { |
225 | | - break; |
226 | | - } |
227 | | - } |
228 | | - |
229 | | - if ($message) { |
230 | | - $catalog->set($message, $this->prefix.$message, $domain); |
231 | | - break; |
232 | | - } |
233 | | - } |
234 | | - } |
235 | | - } |
236 | | - |
237 | | - /** |
238 | | - * @param string $file |
239 | | - * |
240 | | - * @return bool |
241 | | - * |
242 | | - * @throws \InvalidArgumentException |
243 | | - */ |
244 | | - protected function canBeExtracted($file) |
245 | | - { |
246 | | - return $this->isFile($file) && 'php' === pathinfo($file, PATHINFO_EXTENSION); |
247 | | - } |
248 | | - |
249 | | - /** |
250 | | - * @param string|array $directory |
251 | | - * |
252 | | - * @return array |
253 | | - */ |
254 | | - protected function extractFromDirectory($directory) |
255 | | - { |
256 | | - $finder = new Finder(); |
257 | | - |
258 | | - return $finder->files()->name('*.php')->in($directory); |
259 | | - } |
260 | 23 | } |
0 commit comments