Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4407cd3

Browse files
Nyholmnicolas-grekas
authored andcommitted
Moved PhpExtractor and PhpStringTokenParser to Translation component
1 parent a442e37 commit 4407cd3

File tree

13 files changed

+579
-368
lines changed

13 files changed

+579
-368
lines changed

UPGRADE-3.4.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,14 @@ FrameworkBundle
204204
`TranslationDebugCommand`, `TranslationUpdateCommand`, `XliffLintCommand`
205205
and `YamlLintCommand` classes have been marked as final
206206

207+
* The `Symfony\Bundle\FrameworkBundle\Translation\PhpExtractor`
208+
class has been deprecated and will be removed in 4.0. Use the
209+
`Symfony\Component\Translation\Extractor\PhpExtractor` class instead.
210+
211+
* The `Symfony\Bundle\FrameworkBundle\Translation\PhpStringTokenParser`
212+
class has been deprecated and will be removed in 4.0. Use the
213+
`Symfony\Component\Translation\Extractor\PhpStringTokenParser` class instead.
214+
207215
HttpFoundation
208216
--------------
209217

UPGRADE-4.0.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,14 @@ FrameworkBundle
494494
`Symfony\Component\Translation\TranslatorInterface` as
495495
first argument.
496496

497+
* The `Symfony\Bundle\FrameworkBundle\Translation\PhpExtractor`
498+
class has been deprecated and will be removed in 4.0. Use the
499+
`Symfony\Component\Translation\Extractor\PhpExtractor` class instead.
500+
501+
* The `Symfony\Bundle\FrameworkBundle\Translation\PhpStringTokenParser`
502+
class has been deprecated and will be removed in 4.0. Use the
503+
`Symfony\Component\Translation\Extractor\PhpStringTokenParser` class instead.
504+
497505
HttpFoundation
498506
--------------
499507

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ CHANGELOG
105105
`Symfony\Component\Workflow\DependencyInjection\ValidateWorkflowsPass` instead
106106
* Deprecated `ConstraintValidatorFactory`, use
107107
`Symfony\Component\Validator\ContainerConstraintValidatorFactory` instead.
108+
* Deprecated `PhpStringTokenParser`, use
109+
`Symfony\Component\Translation\Extractor\PhpStringTokenParser` instead.
110+
* Deprecated `PhpExtractor`, use
111+
`Symfony\Component\Translation\Extractor\PhpExtractor` instead.
108112

109113
3.2.0
110114
-----

src/Symfony/Bundle/FrameworkBundle/Tests/Translation/PhpExtractorTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
use Symfony\Bundle\FrameworkBundle\Translation\PhpExtractor;
1616
use Symfony\Component\Translation\MessageCatalogue;
1717

18+
/**
19+
* @group legacy
20+
*/
1821
class PhpExtractorTest extends TestCase
1922
{
2023
/**

src/Symfony/Bundle/FrameworkBundle/Translation/PhpExtractor.php

Lines changed: 5 additions & 242 deletions
Original file line numberDiff line numberDiff line change
@@ -11,250 +11,13 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Translation;
1313

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);
1817

1918
/**
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
2320
*/
24-
class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
21+
class PhpExtractor extends NewPhpExtractor
2522
{
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-
}
26023
}

0 commit comments

Comments
 (0)