Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Symfony\Component\Translation\Extractor;
13
 
14
trigger_deprecation('symfony/translation', '6.2', '"%s" is deprecated, use "%s" instead.', PhpExtractor::class, PhpAstExtractor::class);
15
 
16
use Symfony\Component\Finder\Finder;
17
use Symfony\Component\Translation\MessageCatalogue;
18
 
19
/**
20
 * PhpExtractor extracts translation messages from a PHP template.
21
 *
22
 * @author Michel Salib <michelsalib@hotmail.com>
23
 *
24
 * @deprecated since Symfony 6.2, use the PhpAstExtractor instead
25
 */
26
class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
27
{
28
    public const MESSAGE_TOKEN = 300;
29
    public const METHOD_ARGUMENTS_TOKEN = 1000;
30
    public const DOMAIN_TOKEN = 1001;
31
 
32
    /**
33
     * Prefix for new found message.
34
     */
35
    private string $prefix = '';
36
 
37
    /**
38
     * The sequence that captures translation messages.
39
     */
40
    protected $sequences = [
41
        [
42
            '->',
43
            'trans',
44
            '(',
45
            self::MESSAGE_TOKEN,
46
            ',',
47
            self::METHOD_ARGUMENTS_TOKEN,
48
            ',',
49
            self::DOMAIN_TOKEN,
50
        ],
51
        [
52
            '->',
53
            'trans',
54
            '(',
55
            self::MESSAGE_TOKEN,
56
        ],
57
        [
58
            'new',
59
            'TranslatableMessage',
60
            '(',
61
            self::MESSAGE_TOKEN,
62
            ',',
63
            self::METHOD_ARGUMENTS_TOKEN,
64
            ',',
65
            self::DOMAIN_TOKEN,
66
        ],
67
        [
68
            'new',
69
            'TranslatableMessage',
70
            '(',
71
            self::MESSAGE_TOKEN,
72
        ],
73
        [
74
            'new',
75
            '\\',
76
            'Symfony',
77
            '\\',
78
            'Component',
79
            '\\',
80
            'Translation',
81
            '\\',
82
            'TranslatableMessage',
83
            '(',
84
            self::MESSAGE_TOKEN,
85
            ',',
86
            self::METHOD_ARGUMENTS_TOKEN,
87
            ',',
88
            self::DOMAIN_TOKEN,
89
        ],
90
        [
91
            'new',
92
            '\Symfony\Component\Translation\TranslatableMessage',
93
            '(',
94
            self::MESSAGE_TOKEN,
95
            ',',
96
            self::METHOD_ARGUMENTS_TOKEN,
97
            ',',
98
            self::DOMAIN_TOKEN,
99
        ],
100
        [
101
            'new',
102
            '\\',
103
            'Symfony',
104
            '\\',
105
            'Component',
106
            '\\',
107
            'Translation',
108
            '\\',
109
            'TranslatableMessage',
110
            '(',
111
            self::MESSAGE_TOKEN,
112
        ],
113
        [
114
            'new',
115
            '\Symfony\Component\Translation\TranslatableMessage',
116
            '(',
117
            self::MESSAGE_TOKEN,
118
        ],
119
        [
120
            't',
121
            '(',
122
            self::MESSAGE_TOKEN,
123
            ',',
124
            self::METHOD_ARGUMENTS_TOKEN,
125
            ',',
126
            self::DOMAIN_TOKEN,
127
        ],
128
        [
129
            't',
130
            '(',
131
            self::MESSAGE_TOKEN,
132
        ],
133
    ];
134
 
135
    public function extract(string|iterable $resource, MessageCatalogue $catalog)
136
    {
137
        $files = $this->extractFiles($resource);
138
        foreach ($files as $file) {
139
            $this->parseTokens(token_get_all(file_get_contents($file)), $catalog, $file);
140
 
141
            gc_mem_caches();
142
        }
143
    }
144
 
145
    public function setPrefix(string $prefix)
146
    {
147
        $this->prefix = $prefix;
148
    }
149
 
150
    /**
151
     * Normalizes a token.
152
     */
153
    protected function normalizeToken(mixed $token): ?string
154
    {
155
        if (isset($token[1]) && 'b"' !== $token) {
156
            return $token[1];
157
        }
158
 
159
        return $token;
160
    }
161
 
162
    /**
163
     * Seeks to a non-whitespace token.
164
     */
165
    private function seekToNextRelevantToken(\Iterator $tokenIterator)
166
    {
167
        for (; $tokenIterator->valid(); $tokenIterator->next()) {
168
            $t = $tokenIterator->current();
169
            if (\T_WHITESPACE !== $t[0]) {
170
                break;
171
            }
172
        }
173
    }
174
 
175
    private function skipMethodArgument(\Iterator $tokenIterator)
176
    {
177
        $openBraces = 0;
178
 
179
        for (; $tokenIterator->valid(); $tokenIterator->next()) {
180
            $t = $tokenIterator->current();
181
 
182
            if ('[' === $t[0] || '(' === $t[0]) {
183
                ++$openBraces;
184
            }
185
 
186
            if (']' === $t[0] || ')' === $t[0]) {
187
                --$openBraces;
188
            }
189
 
190
            if ((0 === $openBraces && ',' === $t[0]) || (-1 === $openBraces && ')' === $t[0])) {
191
                break;
192
            }
193
        }
194
    }
195
 
196
    /**
197
     * Extracts the message from the iterator while the tokens
198
     * match allowed message tokens.
199
     */
200
    private function getValue(\Iterator $tokenIterator)
201
    {
202
        $message = '';
203
        $docToken = '';
204
        $docPart = '';
205
 
206
        for (; $tokenIterator->valid(); $tokenIterator->next()) {
207
            $t = $tokenIterator->current();
208
            if ('.' === $t) {
209
                // Concatenate with next token
210
                continue;
211
            }
212
            if (!isset($t[1])) {
213
                break;
214
            }
215
 
216
            switch ($t[0]) {
217
                case \T_START_HEREDOC:
218
                    $docToken = $t[1];
219
                    break;
220
                case \T_ENCAPSED_AND_WHITESPACE:
221
                case \T_CONSTANT_ENCAPSED_STRING:
222
                    if ('' === $docToken) {
223
                        $message .= PhpStringTokenParser::parse($t[1]);
224
                    } else {
225
                        $docPart = $t[1];
226
                    }
227
                    break;
228
                case \T_END_HEREDOC:
229
                    if ($indentation = strspn($t[1], ' ')) {
230
                        $docPartWithLineBreaks = $docPart;
231
                        $docPart = '';
232
 
233
                        foreach (preg_split('~(\r\n|\n|\r)~', $docPartWithLineBreaks, -1, \PREG_SPLIT_DELIM_CAPTURE) as $str) {
234
                            if (\in_array($str, ["\r\n", "\n", "\r"], true)) {
235
                                $docPart .= $str;
236
                            } else {
237
                                $docPart .= substr($str, $indentation);
238
                            }
239
                        }
240
                    }
241
 
242
                    $message .= PhpStringTokenParser::parseDocString($docToken, $docPart);
243
                    $docToken = '';
244
                    $docPart = '';
245
                    break;
246
                case \T_WHITESPACE:
247
                    break;
248
                default:
249
                    break 2;
250
            }
251
        }
252
 
253
        return $message;
254
    }
255
 
256
    /**
257
     * Extracts trans message from PHP tokens.
258
     */
259
    protected function parseTokens(array $tokens, MessageCatalogue $catalog, string $filename)
260
    {
261
        $tokenIterator = new \ArrayIterator($tokens);
262
 
263
        for ($key = 0; $key < $tokenIterator->count(); ++$key) {
264
            foreach ($this->sequences as $sequence) {
265
                $message = '';
266
                $domain = 'messages';
267
                $tokenIterator->seek($key);
268
 
269
                foreach ($sequence as $sequenceKey => $item) {
270
                    $this->seekToNextRelevantToken($tokenIterator);
271
 
272
                    if ($this->normalizeToken($tokenIterator->current()) === $item) {
273
                        $tokenIterator->next();
274
                        continue;
275
                    } elseif (self::MESSAGE_TOKEN === $item) {
276
                        $message = $this->getValue($tokenIterator);
277
 
278
                        if (\count($sequence) === ($sequenceKey + 1)) {
279
                            break;
280
                        }
281
                    } elseif (self::METHOD_ARGUMENTS_TOKEN === $item) {
282
                        $this->skipMethodArgument($tokenIterator);
283
                    } elseif (self::DOMAIN_TOKEN === $item) {
284
                        $domainToken = $this->getValue($tokenIterator);
285
                        if ('' !== $domainToken) {
286
                            $domain = $domainToken;
287
                        }
288
 
289
                        break;
290
                    } else {
291
                        break;
292
                    }
293
                }
294
 
295
                if ($message) {
296
                    $catalog->set($message, $this->prefix.$message, $domain);
297
                    $metadata = $catalog->getMetadata($message, $domain) ?? [];
298
                    $normalizedFilename = preg_replace('{[\\\\/]+}', '/', $filename);
299
                    $metadata['sources'][] = $normalizedFilename.':'.$tokens[$key][2];
300
                    $catalog->setMetadata($message, $metadata, $domain);
301
                    break;
302
                }
303
            }
304
        }
305
    }
306
 
307
    /**
308
     * @throws \InvalidArgumentException
309
     */
310
    protected function canBeExtracted(string $file): bool
311
    {
312
        return $this->isFile($file) && 'php' === pathinfo($file, \PATHINFO_EXTENSION);
313
    }
314
 
315
    protected function extractFromDirectory(string|array $directory): iterable
316
    {
317
        if (!class_exists(Finder::class)) {
318
            throw new \LogicException(sprintf('You cannot use "%s" as the "symfony/finder" package is not installed. Try running "composer require symfony/finder".', static::class));
319
        }
320
 
321
        $finder = new Finder();
322
 
323
        return $finder->files()->name('*.php')->in($directory);
324
    }
325
}