Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Translation] Moved PhpExtractor and PhpStringTokenParser to Translation component #24197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions 14 UPGRADE-3.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ FrameworkBundle
`TranslationDebugCommand`, `TranslationUpdateCommand`, `XliffLintCommand`
and `YamlLintCommand` classes have been marked as final

* The `Symfony\Bundle\FrameworkBundle\Translation\PhpExtractor`
class has been deprecated and will be removed in 4.0. Use the
`Symfony\Component\Translation\Extractor\PhpExtractor` class instead.

* The `Symfony\Bundle\FrameworkBundle\Translation\PhpStringTokenParser`
class has been deprecated and will be removed in 4.0. Use the
`Symfony\Component\Translation\Extractor\PhpStringTokenParser` class instead.

HttpFoundation
--------------

Expand Down Expand Up @@ -280,8 +288,8 @@ Profiler
Security
--------

* Deprecated the HTTP digest authentication: `NonceExpiredException`,
`DigestAuthenticationListener` and `DigestAuthenticationEntryPoint` will be
* Deprecated the HTTP digest authentication: `NonceExpiredException`,
`DigestAuthenticationListener` and `DigestAuthenticationEntryPoint` will be
removed in 4.0. Use another authentication system like `http_basic` instead.

SecurityBundle
Expand All @@ -305,7 +313,7 @@ SecurityBundle
* Added `logout_on_user_change` to the firewall options. This config item will
trigger a logout when the user has changed. Should be set to true to avoid
deprecations in the configuration.

* Deprecated the HTTP digest authentication: `HttpDigestFactory` will be removed in 4.0.
Use another authentication system like `http_basic` instead.

Expand Down
14 changes: 11 additions & 3 deletions 14 UPGRADE-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,14 @@ FrameworkBundle
`Symfony\Component\Translation\TranslatorInterface` as
first argument.

* The `Symfony\Bundle\FrameworkBundle\Translation\PhpExtractor`
class has been deprecated and will be removed in 4.0. Use the
`Symfony\Component\Translation\Extractor\PhpExtractor` class instead.

* The `Symfony\Bundle\FrameworkBundle\Translation\PhpStringTokenParser`
class has been deprecated and will be removed in 4.0. Use the
`Symfony\Component\Translation\Extractor\PhpStringTokenParser` class instead.

HttpFoundation
--------------

Expand Down Expand Up @@ -525,9 +533,9 @@ HttpFoundation
* The ability to check only for cacheable HTTP methods using `Request::isMethodSafe()` is
not supported anymore, use `Request::isMethodCacheable()` instead.

* The `Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeSessionHandler`,
* The `Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeSessionHandler`,
`Symfony\Component\HttpFoundation\Session\Storage\Proxy\AbstractProxy`,
`Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy` and
`Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy` and
`Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy` classes have been removed.

* `NativeSessionStorage::setSaveHandler()` now requires an instance of `\SessionHandlerInterface` as argument.
Expand Down Expand Up @@ -682,7 +690,7 @@ SecurityBundle

* The firewall option `logout_on_user_change` is now always true, which will
trigger a logout if the user changes between requests.

* Removed the HTTP digest authentication system. The `HttpDigestFactory` class
has been removed. Use another authentication system like `http_basic` instead.

Expand Down
4 changes: 4 additions & 0 deletions 4 src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ CHANGELOG
`Symfony\Component\Workflow\DependencyInjection\ValidateWorkflowsPass` instead
* Deprecated `ConstraintValidatorFactory`, use
`Symfony\Component\Validator\ContainerConstraintValidatorFactory` instead.
* Deprecated `PhpStringTokenParser`, use
`Symfony\Component\Translation\Extractor\PhpStringTokenParser` instead.
* Deprecated `PhpExtractor`, use
`Symfony\Component\Translation\Extractor\PhpExtractor` instead.

3.2.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
use Symfony\Bundle\FrameworkBundle\Translation\PhpExtractor;
use Symfony\Component\Translation\MessageCatalogue;

/**
* @group legacy
*/
class PhpExtractorTest extends TestCase
{
/**
Expand Down
247 changes: 5 additions & 242 deletions 247 src/Symfony/Bundle/FrameworkBundle/Translation/PhpExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,250 +11,13 @@

namespace Symfony\Bundle\FrameworkBundle\Translation;

use Symfony\Component\Finder\Finder;
use Symfony\Component\Translation\Extractor\AbstractFileExtractor;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Extractor\ExtractorInterface;
use Symfony\Component\Translation\Extractor\PhpExtractor as NewPhpExtractor;

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

/**
* PhpExtractor extracts translation messages from a PHP template.
*
* @author Michel Salib <michelsalib@hotmail.com>
* @deprecated since version 3.4 and will be removed in 4.0. Use Symfony\Component\Translation\Extractor\PhpExtractor instead
*/
class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
class PhpExtractor extends NewPhpExtractor
{
const MESSAGE_TOKEN = 300;
const METHOD_ARGUMENTS_TOKEN = 1000;
const DOMAIN_TOKEN = 1001;

/**
* Prefix for new found message.
*
* @var string
*/
private $prefix = '';

/**
* The sequence that captures translation messages.
*
* @var array
*/
protected $sequences = array(
array(
'->',
'trans',
'(',
self::MESSAGE_TOKEN,
',',
self::METHOD_ARGUMENTS_TOKEN,
',',
self::DOMAIN_TOKEN,
),
array(
'->',
'transChoice',
'(',
self::MESSAGE_TOKEN,
',',
self::METHOD_ARGUMENTS_TOKEN,
',',
self::METHOD_ARGUMENTS_TOKEN,
',',
self::DOMAIN_TOKEN,
),
array(
'->',
'trans',
'(',
self::MESSAGE_TOKEN,
),
array(
'->',
'transChoice',
'(',
self::MESSAGE_TOKEN,
),
);

/**
* {@inheritdoc}
*/
public function extract($resource, MessageCatalogue $catalog)
{
$files = $this->extractFiles($resource);
foreach ($files as $file) {
$this->parseTokens(token_get_all(file_get_contents($file)), $catalog);

if (\PHP_VERSION_ID >= 70000) {
// PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098
gc_mem_caches();
}
}
}

/**
* {@inheritdoc}
*/
public function setPrefix($prefix)
{
$this->prefix = $prefix;
}

/**
* Normalizes a token.
*
* @param mixed $token
*
* @return string
*/
protected function normalizeToken($token)
{
if (isset($token[1]) && 'b"' !== $token) {
return $token[1];
}

return $token;
}

/**
* Seeks to a non-whitespace token.
*/
private function seekToNextRelevantToken(\Iterator $tokenIterator)
{
for (; $tokenIterator->valid(); $tokenIterator->next()) {
$t = $tokenIterator->current();
if (T_WHITESPACE !== $t[0]) {
break;
}
}
}

private function skipMethodArgument(\Iterator $tokenIterator)
{
$openBraces = 0;

for (; $tokenIterator->valid(); $tokenIterator->next()) {
$t = $tokenIterator->current();

if ('[' === $t[0] || '(' === $t[0]) {
++$openBraces;
}

if (']' === $t[0] || ')' === $t[0]) {
--$openBraces;
}

if ((0 === $openBraces && ',' === $t[0]) || (-1 === $openBraces && ')' === $t[0])) {
break;
}
}
}

/**
* Extracts the message from the iterator while the tokens
* match allowed message tokens.
*/
private function getValue(\Iterator $tokenIterator)
{
$message = '';
$docToken = '';

for (; $tokenIterator->valid(); $tokenIterator->next()) {
$t = $tokenIterator->current();
if (!isset($t[1])) {
break;
}

switch ($t[0]) {
case T_START_HEREDOC:
$docToken = $t[1];
break;
case T_ENCAPSED_AND_WHITESPACE:
case T_CONSTANT_ENCAPSED_STRING:
$message .= $t[1];
break;
case T_END_HEREDOC:
return PhpStringTokenParser::parseDocString($docToken, $message);
default:
break 2;
}
}

if ($message) {
$message = PhpStringTokenParser::parse($message);
}

return $message;
}

/**
* Extracts trans message from PHP tokens.
*
* @param array $tokens
* @param MessageCatalogue $catalog
*/
protected function parseTokens($tokens, MessageCatalogue $catalog)
{
$tokenIterator = new \ArrayIterator($tokens);

for ($key = 0; $key < $tokenIterator->count(); ++$key) {
foreach ($this->sequences as $sequence) {
$message = '';
$domain = 'messages';
$tokenIterator->seek($key);

foreach ($sequence as $sequenceKey => $item) {
$this->seekToNextRelevantToken($tokenIterator);

if ($this->normalizeToken($tokenIterator->current()) === $item) {
$tokenIterator->next();
continue;
} elseif (self::MESSAGE_TOKEN === $item) {
$message = $this->getValue($tokenIterator);

if (count($sequence) === ($sequenceKey + 1)) {
break;
}
} elseif (self::METHOD_ARGUMENTS_TOKEN === $item) {
$this->skipMethodArgument($tokenIterator);
} elseif (self::DOMAIN_TOKEN === $item) {
$domain = $this->getValue($tokenIterator);

break;
} else {
break;
}
}

if ($message) {
$catalog->set($message, $this->prefix.$message, $domain);
break;
}
}
}
}

/**
* @param string $file
*
* @return bool
*
* @throws \InvalidArgumentException
*/
protected function canBeExtracted($file)
{
return $this->isFile($file) && 'php' === pathinfo($file, PATHINFO_EXTENSION);
}

/**
* @param string|array $directory
*
* @return array
*/
protected function extractFromDirectory($directory)
{
$finder = new Finder();

return $finder->files()->name('*.php')->in($directory);
}
}
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.