-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/Symfony/Component/Translation/Extractor/PhpAstExtractor.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
welcoMattic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/Symfony/Component/Translation/Extractor/Visitor/AbstractVisitor.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.