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
12namespace Symfony\Component\Translation\Extractor;
13
14use Symfony\Component\Finder\Finder;
15use Symfony\Component\Translation\MessageCatalogue;
16
17/**
18 * PhpExtractor extracts translation messages from a PHP template.
19 *
20 * @author Michel Salib <michelsalib@hotmail.com>
21 */
22class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
23{
24    public const MESSAGE_TOKEN = 300;
25    public const METHOD_ARGUMENTS_TOKEN = 1000;
26    public const DOMAIN_TOKEN = 1001;
27
28    /**
29     * Prefix for new found message.
30     *
31     * @var string
32     */
33    private $prefix = '';
34
35    /**
36     * The sequence that captures translation messages.
37     *
38     * @var array
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            'transChoice',
54            '(',
55            self::MESSAGE_TOKEN,
56            ',',
57            self::METHOD_ARGUMENTS_TOKEN,
58            ',',
59            self::METHOD_ARGUMENTS_TOKEN,
60            ',',
61            self::DOMAIN_TOKEN,
62        ],
63        [
64            '->',
65            'trans',
66            '(',
67            self::MESSAGE_TOKEN,
68        ],
69        [
70            '->',
71            'transChoice',
72            '(',
73            self::MESSAGE_TOKEN,
74        ],
75    ];
76
77    /**
78     * {@inheritdoc}
79     */
80    public function extract($resource, MessageCatalogue $catalog)
81    {
82        $files = $this->extractFiles($resource);
83        foreach ($files as $file) {
84            $this->parseTokens(token_get_all(file_get_contents($file)), $catalog, $file);
85
86            gc_mem_caches();
87        }
88    }
89
90    /**
91     * {@inheritdoc}
92     */
93    public function setPrefix($prefix)
94    {
95        $this->prefix = $prefix;
96    }
97
98    /**
99     * Normalizes a token.
100     *
101     * @param mixed $token
102     *
103     * @return string|null
104     */
105    protected function normalizeToken($token)
106    {
107        if (isset($token[1]) && 'b"' !== $token) {
108            return $token[1];
109        }
110
111        return $token;
112    }
113
114    /**
115     * Seeks to a non-whitespace token.
116     */
117    private function seekToNextRelevantToken(\Iterator $tokenIterator)
118    {
119        for (; $tokenIterator->valid(); $tokenIterator->next()) {
120            $t = $tokenIterator->current();
121            if (\T_WHITESPACE !== $t[0]) {
122                break;
123            }
124        }
125    }
126
127    private function skipMethodArgument(\Iterator $tokenIterator)
128    {
129        $openBraces = 0;
130
131        for (; $tokenIterator->valid(); $tokenIterator->next()) {
132            $t = $tokenIterator->current();
133
134            if ('[' === $t[0] || '(' === $t[0]) {
135                ++$openBraces;
136            }
137
138            if (']' === $t[0] || ')' === $t[0]) {
139                --$openBraces;
140            }
141
142            if ((0 === $openBraces && ',' === $t[0]) || (-1 === $openBraces && ')' === $t[0])) {
143                break;
144            }
145        }
146    }
147
148    /**
149     * Extracts the message from the iterator while the tokens
150     * match allowed message tokens.
151     */
152    private function getValue(\Iterator $tokenIterator)
153    {
154        $message = '';
155        $docToken = '';
156        $docPart = '';
157
158        for (; $tokenIterator->valid(); $tokenIterator->next()) {
159            $t = $tokenIterator->current();
160            if ('.' === $t) {
161                // Concatenate with next token
162                continue;
163            }
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                    if ('' === $docToken) {
175                        $message .= PhpStringTokenParser::parse($t[1]);
176                    } else {
177                        $docPart = $t[1];
178                    }
179                    break;
180                case \T_END_HEREDOC:
181                    if ($indentation = strspn($t[1], ' ')) {
182                        $docPartWithLineBreaks = $docPart;
183                        $docPart = '';
184
185                        foreach (preg_split('~(\r\n|\n|\r)~', $docPartWithLineBreaks, -1, \PREG_SPLIT_DELIM_CAPTURE) as $str) {
186                            if (\in_array($str, ["\r\n", "\n", "\r"], true)) {
187                                $docPart .= $str;
188                            } else {
189                                $docPart .= substr($str, $indentation);
190                            }
191                        }
192                    }
193
194                    $message .= PhpStringTokenParser::parseDocString($docToken, $docPart);
195                    $docToken = '';
196                    $docPart = '';
197                    break;
198                case \T_WHITESPACE:
199                    break;
200                default:
201                    break 2;
202            }
203        }
204
205        return $message;
206    }
207
208    /**
209     * Extracts trans message from PHP tokens.
210     *
211     * @param array  $tokens
212     * @param string $filename
213     */
214    protected function parseTokens($tokens, MessageCatalogue $catalog/*, string $filename*/)
215    {
216        if (\func_num_args() < 3 && __CLASS__ !== static::class && __CLASS__ !== (new \ReflectionMethod($this, __FUNCTION__))->getDeclaringClass()->getName() && !$this instanceof \PHPUnit\Framework\MockObject\MockObject && !$this instanceof \Prophecy\Prophecy\ProphecySubjectInterface && !$this instanceof \Mockery\MockInterface) {
217            @trigger_error(sprintf('The "%s()" method will have a new "string $filename" argument in version 5.0, not defining it is deprecated since Symfony 4.3.', __METHOD__), \E_USER_DEPRECATED);
218        }
219        $filename = 2 < \func_num_args() ? func_get_arg(2) : '';
220
221        $tokenIterator = new \ArrayIterator($tokens);
222
223        for ($key = 0; $key < $tokenIterator->count(); ++$key) {
224            foreach ($this->sequences as $sequence) {
225                $message = '';
226                $domain = 'messages';
227                $tokenIterator->seek($key);
228
229                foreach ($sequence as $sequenceKey => $item) {
230                    $this->seekToNextRelevantToken($tokenIterator);
231
232                    if ($this->normalizeToken($tokenIterator->current()) === $item) {
233                        $tokenIterator->next();
234                        continue;
235                    } elseif (self::MESSAGE_TOKEN === $item) {
236                        $message = $this->getValue($tokenIterator);
237
238                        if (\count($sequence) === ($sequenceKey + 1)) {
239                            break;
240                        }
241                    } elseif (self::METHOD_ARGUMENTS_TOKEN === $item) {
242                        $this->skipMethodArgument($tokenIterator);
243                    } elseif (self::DOMAIN_TOKEN === $item) {
244                        $domainToken = $this->getValue($tokenIterator);
245                        if ('' !== $domainToken) {
246                            $domain = $domainToken;
247                        }
248
249                        break;
250                    } else {
251                        break;
252                    }
253                }
254
255                if ($message) {
256                    $catalog->set($message, $this->prefix.$message, $domain);
257                    $metadata = $catalog->getMetadata($message, $domain) ?? [];
258                    $normalizedFilename = preg_replace('{[\\\\/]+}', '/', $filename);
259                    $metadata['sources'][] = $normalizedFilename.':'.$tokens[$key][2];
260                    $catalog->setMetadata($message, $metadata, $domain);
261                    break;
262                }
263            }
264        }
265    }
266
267    /**
268     * @param string $file
269     *
270     * @return bool
271     *
272     * @throws \InvalidArgumentException
273     */
274    protected function canBeExtracted($file)
275    {
276        return $this->isFile($file) && 'php' === pathinfo($file, \PATHINFO_EXTENSION);
277    }
278
279    /**
280     * {@inheritdoc}
281     */
282    protected function extractFromDirectory($directory)
283    {
284        $finder = new Finder();
285
286        return $finder->files()->name('*.php')->in($directory);
287    }
288}
289