Skip to content

[Translation] Add PhpAstExtractor #46161

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
Oct 20, 2022
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
6 changes: 6 additions & 0 deletions UPGRADE-6.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ Serializer
* Change the signature of `AttributeMetadataInterface::setSerializedName()` to `setSerializedName(?string)`
* Change the signature of `ClassMetadataInterface::setClassDiscriminatorMapping()` to `setClassDiscriminatorMapping(?ClassDiscriminatorMapping)`

Translation
-----------

* Deprecate `PhpExtractor` in favor of `PhpAstExtractor`
* Add `PhpAstExtractor` (requires [nikic/php-parser](https://github.com/nikic/php-parser) to be installed)

Validator
---------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class UnusedTagsPass implements CompilerPassInterface
'texter.transport_factory',
'translation.dumper',
'translation.extractor',
'translation.extractor.visitor',
'translation.loader',
'translation.provider_factory',
'twig.extension',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Http\Client\HttpClient;
use phpDocumentor\Reflection\DocBlockFactoryInterface;
use phpDocumentor\Reflection\Types\ContextFactory;
use PhpParser\Parser;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Container\ContainerInterface as PsrContainerInterface;
Expand Down Expand Up @@ -218,6 +219,7 @@
use Symfony\Component\Translation\Bridge\Loco\LocoProviderFactory;
use Symfony\Component\Translation\Bridge\Lokalise\LokaliseProviderFactory;
use Symfony\Component\Translation\Command\XliffLintCommand as BaseXliffLintCommand;
use Symfony\Component\Translation\Extractor\PhpAstExtractor;
use Symfony\Component\Translation\LocaleSwitcher;
use Symfony\Component\Translation\PseudoLocalizationTranslator;
use Symfony\Component\Translation\Translator;
Expand Down Expand Up @@ -1335,6 +1337,14 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
$container->removeDefinition('translation.locale_switcher');
}

if (ContainerBuilder::willBeAvailable('nikic/php-parser', Parser::class, ['symfony/translation'])
&& ContainerBuilder::willBeAvailable('symfony/translation', PhpAstExtractor::class, ['symfony/framework-bundle'])
) {
$container->removeDefinition('translation.extractor.php');
} else {
$container->removeDefinition('translation.extractor.php_ast');
}

$loader->load('translation_providers.php');

// Use the "real" translator instead of the identity default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
use Symfony\Component\Translation\Dumper\YamlFileDumper;
use Symfony\Component\Translation\Extractor\ChainExtractor;
use Symfony\Component\Translation\Extractor\ExtractorInterface;
use Symfony\Component\Translation\Extractor\PhpAstExtractor;
use Symfony\Component\Translation\Extractor\PhpExtractor;
use Symfony\Component\Translation\Extractor\Visitor\ConstraintVisitor;
use Symfony\Component\Translation\Extractor\Visitor\TranslatableMessageVisitor;
use Symfony\Component\Translation\Extractor\Visitor\TransMethodVisitor;
use Symfony\Component\Translation\Formatter\MessageFormatter;
use Symfony\Component\Translation\Loader\CsvFileLoader;
use Symfony\Component\Translation\Loader\IcuDatFileLoader;
Expand Down Expand Up @@ -151,6 +155,19 @@
->set('translation.extractor.php', PhpExtractor::class)
->tag('translation.extractor', ['alias' => 'php'])

->set('translation.extractor.php_ast', PhpAstExtractor::class)
->args([tagged_iterator('translation.extractor.visitor')])
->tag('translation.extractor', ['alias' => 'php'])

->set('translation.extractor.visitor.trans_method', TransMethodVisitor::class)
->tag('translation.extractor.visitor')

->set('translation.extractor.visitor.translatable_message', TranslatableMessageVisitor::class)
->tag('translation.extractor.visitor')

->set('translation.extractor.visitor.constraint', ConstraintVisitor::class)
->tag('translation.extractor.visitor')

->set('translation.reader', TranslationReader::class)
->alias(TranslationReaderInterface::class, 'translation.reader')

Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Translation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

6.2
---

* Deprecate `PhpExtractor` in favor of `PhpAstExtractor`
* Add `PhpAstExtractor` (requires [nikic/php-parser](https://github.com/nikic/php-parser) to be installed)

6.1
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ public function process(ContainerBuilder $container)
->replaceArgument(3, $loaders)
;

if ($container->hasDefinition('validator') && $container->hasDefinition('translation.extractor.visitor.constraint')) {
$constraintVisitorDefinition = $container->getDefinition('translation.extractor.visitor.constraint');
$constraintClassNames = [];

foreach ($container->findTaggedServiceIds('validator.constraint_validator', true) as $id => $attributes) {
$serviceDefinition = $container->getDefinition($id);
// Extraction of the constraint class name from the Constraint Validator FQCN
$constraintClassNames[] = str_replace('Validator', '', substr(strrchr($serviceDefinition->getClass(), '\\'), 1));
}

$constraintVisitorDefinition->setArgument(0, $constraintClassNames);
}

if (!$container->hasParameter('twig.default_path')) {
return;
}
Expand Down
78 changes: 78 additions & 0 deletions src/Symfony/Component/Translation/Extractor/PhpAstExtractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Translation\Extractor;

use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor;
use PhpParser\Parser;
use PhpParser\ParserFactory;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Translation\Extractor\Visitor\AbstractVisitor;
use Symfony\Component\Translation\MessageCatalogue;

/**
* PhpAstExtractor extracts translation messages from a PHP AST.
*
* @author Mathieu Santostefano <msantostefano@protonmail.com>
*/
final class PhpAstExtractor extends AbstractFileExtractor implements ExtractorInterface
{
private Parser $parser;

public function __construct(
/**
* @param iterable<AbstractVisitor&NodeVisitor> $visitors
*/
private readonly iterable $visitors,
private string $prefix = '',
) {
if (!class_exists(ParserFactory::class)) {
throw new \LogicException(sprintf('You cannot use "%s" as the "nikic/php-parser" package is not installed. Try running "composer require nikic/php-parser".', static::class));
}

$this->parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
}

public function extract(iterable|string $resource, MessageCatalogue $catalogue): void
{
foreach ($this->extractFiles($resource) as $file) {
$traverser = new NodeTraverser();
/** @var AbstractVisitor&NodeVisitor $visitor */
foreach ($this->visitors as $visitor) {
$visitor->initialize($catalogue, $file, $this->prefix);
$traverser->addVisitor($visitor);
}

$nodes = $this->parser->parse(file_get_contents($file));
$traverser->traverse($nodes);
}
}

public function setPrefix(string $prefix): void
{
$this->prefix = $prefix;
}

protected function canBeExtracted(string $file): bool
{
return 'php' === pathinfo($file, \PATHINFO_EXTENSION) && $this->isFile($file);
}

protected function extractFromDirectory(array|string $resource): iterable|Finder
{
if (!class_exists(Finder::class)) {
throw new \LogicException(sprintf('You cannot use "%s" as the "symfony/finder" package is not installed. Try running "composer require symfony/finder".', static::class));
}

return (new Finder())->files()->name('*.php')->in($resource);
}
}
4 changes: 4 additions & 0 deletions src/Symfony/Component/Translation/Extractor/PhpExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@

namespace Symfony\Component\Translation\Extractor;

trigger_deprecation('symfony/translation', '6.2', '"%s" is deprecated, use "%s" instead.', PhpExtractor::class, PhpAstExtractor::class);

use Symfony\Component\Finder\Finder;
use Symfony\Component\Translation\MessageCatalogue;

/**
* PhpExtractor extracts translation messages from a PHP template.
*
* @author Michel Salib <michelsalib@hotmail.com>
*
* @deprecated since Symfony 6.2, use the PhpAstExtractor instead
*/
class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Translation\Extractor\Visitor;

use PhpParser\Node;
use Symfony\Component\Translation\MessageCatalogue;

/**
* @author Mathieu Santostefano <msantostefano@protonmail.com>
*/
abstract class AbstractVisitor
{
private MessageCatalogue $catalogue;
private \SplFileInfo $file;
private string $messagePrefix;

public function initialize(MessageCatalogue $catalogue, \SplFileInfo $file, string $messagePrefix): void
{
$this->catalogue = $catalogue;
$this->file = $file;
$this->messagePrefix = $messagePrefix;
}

protected function addMessageToCatalogue(string $message, ?string $domain, int $line): void
{
$domain ??= 'messages';
$this->catalogue->set($message, $this->messagePrefix.$message, $domain);
$metadata = $this->catalogue->getMetadata($message, $domain) ?? [];
$normalizedFilename = preg_replace('{[\\\\/]+}', '/', $this->file);
$metadata['sources'][] = $normalizedFilename.':'.$line;
$this->catalogue->setMetadata($message, $metadata, $domain);
}

protected function getStringArguments(Node\Expr\CallLike|Node\Attribute|Node\Expr\New_ $node, int|string $index, bool $indexIsRegex = false): array
{
$args = $node instanceof Node\Expr\CallLike ? $node->getArgs() : $node->args;

if (\is_string($index)) {
return $this->getStringNamedArguments($node, $index, $indexIsRegex);
}

if (\count($args) < $index) {
return [];
}

/** @var Node\Arg $arg */
$arg = $args[$index];
if (!$result = $this->getStringValue($arg->value)) {
return [];
}

return [$result];
}

protected function hasNodeNamedArguments(Node\Expr\CallLike|Node\Attribute|Node\Expr\New_ $node): bool
{
$args = $node instanceof Node\Expr\CallLike ? $node->getArgs() : $node->args;

/** @var Node\Arg $arg */
foreach ($args as $arg) {
if (null !== $arg->name) {
return true;
}
}

return false;
}

private function getStringNamedArguments(Node\Expr\CallLike|Node\Attribute $node, string $argumentName = null, bool $isArgumentNamePattern = false): array
{
$args = $node instanceof Node\Expr\CallLike ? $node->getArgs() : $node->args;
$argumentValues = [];

foreach ($args as $arg) {
if (!$isArgumentNamePattern && $arg->name?->toString() === $argumentName) {
$argumentValues[] = $this->getStringValue($arg->value);
} elseif ($isArgumentNamePattern && preg_match($argumentName, $arg->name?->toString() ?? '') > 0) {
$argumentValues[] = $this->getStringValue($arg->value);
}
}

return array_filter($argumentValues);
}

private function getStringValue(Node $node): ?string
{
if ($node instanceof Node\Scalar\String_) {
return $node->value;
}

if ($node instanceof Node\Expr\BinaryOp\Concat) {
if (null === $left = $this->getStringValue($node->left)) {
return null;
}

if (null === $right = $this->getStringValue($node->right)) {
return null;
}

return $left.$right;
}

if ($node instanceof Node\Expr\Assign && $node->expr instanceof Node\Scalar\String_) {
return $node->expr->value;
}

return null;
}
}
Loading