feature #31249 [Translator] Set sources when extracting strings from php files (Stadly)

This PR was squashed before being merged into the 4.3-dev branch (closes #31249).

Discussion
----------

[Translator] Set sources when extracting strings from php files

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

Set metadata about filenames and line numbers when extracting translatable strings from php files. This PR complements #30909 and #31248, as those PRs implement support for dumping the filenames and line numbers to Po and Qt file formats.

Commits
-------

f05d4e41be [Translator] Set sources when extracting strings from php files
This commit is contained in:
Fabien Potencier 2019-05-07 13:11:03 +02:00
commit b2f5b8a1e2
2 changed files with 16 additions and 2 deletions

View File

@ -81,7 +81,7 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
{ {
$files = $this->extractFiles($resource); $files = $this->extractFiles($resource);
foreach ($files as $file) { foreach ($files as $file) {
$this->parseTokens(token_get_all(file_get_contents($file)), $catalog); $this->parseTokens(token_get_all(file_get_contents($file)), $catalog, $file);
gc_mem_caches(); gc_mem_caches();
} }
@ -197,9 +197,15 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
* *
* @param array $tokens * @param array $tokens
* @param MessageCatalogue $catalog * @param MessageCatalogue $catalog
* @param string $filename
*/ */
protected function parseTokens($tokens, MessageCatalogue $catalog) protected function parseTokens($tokens, MessageCatalogue $catalog/*, string $filename*/)
{ {
if (\func_num_args() < 3 && __CLASS__ !== \get_class($this) && __CLASS__ !== (new \ReflectionMethod($this, __FUNCTION__))->getDeclaringClass()->getName() && !$this instanceof \PHPUnit\Framework\MockObject\MockObject && !$this instanceof \Prophecy\Prophecy\ProphecySubjectInterface) {
@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);
}
$filename = 2 < \func_num_args() ? \func_get_arg(2) : '';
$tokenIterator = new \ArrayIterator($tokens); $tokenIterator = new \ArrayIterator($tokens);
for ($key = 0; $key < $tokenIterator->count(); ++$key) { for ($key = 0; $key < $tokenIterator->count(); ++$key) {
@ -236,6 +242,10 @@ class PhpExtractor extends AbstractFileExtractor implements ExtractorInterface
if ($message) { if ($message) {
$catalog->set($message, $this->prefix.$message, $domain); $catalog->set($message, $this->prefix.$message, $domain);
$metadata = $catalog->getMetadata($message, $domain) ?? [];
$normalizedFilename = preg_replace('{[\\\\/]+}', '/', $filename);
$metadata['sources'][] = $normalizedFilename.':'.$tokens[$key][2];
$catalog->setMetadata($message, $metadata, $domain);
break; break;
} }
} }

View File

@ -69,6 +69,10 @@ EOF;
$actualCatalogue = $catalogue->all(); $actualCatalogue = $catalogue->all();
$this->assertEquals($expectedCatalogue, $actualCatalogue); $this->assertEquals($expectedCatalogue, $actualCatalogue);
$filename = __DIR__.'/../fixtures/extractor/translation.html.php';
$this->assertEquals(['sources' => [$filename.':2']], $catalogue->getMetadata('single-quoted key'));
$this->assertEquals(['sources' => [$filename.':43']], $catalogue->getMetadata('other-domain-test-no-params-short-array', 'not_messages'));
} }
public function resourcesProvider() public function resourcesProvider()