10000 Extracting from services that have the translator injected into them · symfony/symfony@9fe9c2d · GitHub
[go: up one dir, main page]

Skip to content

Commit 9fe9c2d

Browse files
committed
Extracting from services that have the translator injected into them
1 parent 533d4d4 commit 9fe9c2d

13 files changed

+218
-12
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/TranslationDebugCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ private function extractMessages(string $locale, array $transPaths): MessageCata
346346
{
347347
$extractedCatalogue = new MessageCatalogue($locale);
348348
foreach ($transPaths as $path) {
349-
if (is_dir($path)) {
349+
if (is_dir($path) || is_file($path)) {
350350
$this->extractor->extract($path, $extractedCatalogue);
351351
}
352352
}

src/Symfony/Bundle/FrameworkBundle/Command/TranslationUpdateCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
206206
$errorIo->comment('Parsing templates...');
207207
$this->extractor->setPrefix($input->getOption('prefix'));
208208
foreach ($viewsPaths as $path) {
209-
if (is_dir($path)) {
209+
if (is_dir($path) || is_file($path)) {
210210
$this->extractor->extract($path, $extractedCatalogue);
211211
}
212212
}

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
use Symfony\Component\Translation\DependencyInjection\TranslationDumperPass;
5151
use Symfony\Component\Translation\DependencyInjection\TranslationExtractorPass;
5252
use Symfony\Component\Translation\DependencyInjection\TranslatorPass;
53+
use Symfony\Component\Translation\DependencyInjection\TranslatorPathsPass;
5354
use Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass;
5455
use Symfony\Component\Validator\DependencyInjection\AddValidatorInitializersPass;
5556
use Symfony\Component\Workflow\DependencyInjection\ValidateWorkflowsPass;
@@ -99,7 +100,8 @@ public function build(ContainerBuilder $container)
99100
$container->addCompilerPass(new AddAnnotationsCachedReaderPass(), PassConfig::TYPE_AFTER_REMOVING, -255);
100101
$this->addCompilerPassIfExists($container, AddValidatorInitializersPass::class);
101102
$this->addCompilerPassIfExists($container, AddConsoleCommandPass::class, PassConfig::TYPE_BEFORE_REMOVING);
102-
$this->addCompilerPassIfExists($container, TranslatorPass::class);
103+
$this->addCompilerPassIfExists($container, TranslatorPass::class, PassConfig::TYPE_BEFORE_OPTIMIZATION, -32);
104+
$this->addCompilerPassIfExists($container, TranslatorPathsPass::class, PassConfig::TYPE_AFTER_REMOVING);
103105
$container->addCompilerPass(new LoggingTranslatorPass());
104106
$container->addCompilerPass(new AddExpressionLanguageProvidersPass(false));
105107
$this->addCompilerPassIfExists($container, TranslationExtractorPass::class);

src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationDebugCommandTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function testDebugDefaultRootDirectory()
9090
$this->fs->mkdir($this->translationDir.'/translations');
9191
$this->fs->mkdir($this->translationDir.'/templates');
9292

93-
$tester = $this->createCommandTester(array('foo' => 'foo'), array('bar' => 'bar'));
93+
$tester = $this->createCommandTester(array('foo' => 'foo'), array('bar' => 'bar'), null, array($this->translationDir.'/trans'), array($this->translationDir.'/views'));
9494
$tester->execute(array('locale' => 'en'));
9595

9696
$this->assertRegExp('/missing/', $tester->getDisplay());
@@ -145,7 +145,7 @@ protected function tearDown()
145145
/**
146146
* @return CommandTester
147147
*/
148-
private function createCommandTester($extractedMessages = array(), $loadedMessages = array(), $kernel = null)
148+
private function createCommandTester($extractedMessages = array(), $loadedMessages = array(), $kernel = null, array $transPaths = array(), array $viewsPaths = array())
149149
{
150150
$translator = $this->getMockBuilder('Symfony\Component\Translation\Translator')
151151
->disableOriginalConstructor()
@@ -207,7 +207,7 @@ private function createCommandTester($extractedMessages = array(), $loadedMessag
207207
->method('getContainer')
208208
->will($this->returnValue($container));
209209

210-
$command = new TranslationDebugCommand($translator, $loader, $extractor, $this->translationDir.'/translations', $this->translationDir.'/templates');
210+
$command = new TranslationDebugCommand($translator, $loader, $extractor, $this->translationDir.'/translations', $this->translationDir.'/templates', $transPaths, $viewsPaths);
211211

212212
$application = new Application($kernel);
213213
$application->add($command);

src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationUpdateCommandTest.php

Lines changed: 3 additions & 3 deletions
< 10000 td data-grid-cell-id="diff-31da7cae0444fc6b56e28552e06cde537747f528d5fec6af7c551ff36330928f-126-126-2" data-line-anchor="diff-31da7cae0444fc6b56e28552e06cde537747f528d5fec6af7c551ff36330928fR126" data-selected="false" role="gridcell" style="background-color:var(--bgColor-default);padding-right:24px" tabindex="-1" valign="top" class="focusable-grid-cell diff-text-cell right-side-diff-cell left-side">
$translator = $this->getMockBuilder('Symfony\Component\Translation\Translator')
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function testDumpMessagesAndCleanInRootDirectory()
3939
$this->fs->mkdir($this->translationDir.'/translations');
4040
$this->fs->mkdir($this->translationDir.'/templates');
4141

42-
$tester = $this->createCommandTester(array('messages' => array('foo' => 'foo')));
42+
$tester = $this->createCommandTester(array('messages' => array('foo' => 'foo')), array(), null, array($this->translationDir.'/trans'), array($this->translationDir.'/views'));
4343
$tester->execute(array('command' => 'translation:update', 'locale' => 'en', '--dump-messages' => true, '--clean' => true));
4444
$this->assertRegExp('/foo/', $tester->getDisplay());
4545
$this->assertRegExp('/1 message was successfully extracted/', $tester->getDisplay());
@@ -121,7 +121,7 @@ protected function tearDown()
121121
/**
122122
* @return CommandTester
123123
*/
124-
private function createCommandTester($extractedMessages = array(), $loadedMessages = array(), HttpKernel\KernelInterface $kernel = null)
124+
private function createCommandTester($extractedMessages = array(), $loadedMessages = array(), HttpKernel\KernelInterface $kernel = null, array $transPaths = array(), array $viewsPaths = array())
125125
{
126126
127127
->disableOriginalConstructor()
@@ -197,7 +197,7 @@ private function createCommandTester($extractedMessages = array(), $loadedMessag
197197
->method('getContainer')
198198
->will($this->returnValue($container));
199199

200-
$command = new TranslationUpdateCommand($writer, $loader, $extractor, 'en', $this->translationDir.'/translations', $this->translationDir.'/templates');
200+
$command = new TranslationUpdateCommand($writer, $loader, $extractor, 'en', $this->translationDir.'/translations', $this->translationDir.'/templates', $transPaths, $viewsPaths);
201201

202202
$application = new Application($kernel);
203203
$application->add($command);

src/Symfony/Component/Translation/DependencyInjection/TranslatorPass.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,11 @@ public function process(ContainerBuilder $container)
6868
return;
6969
}
7070

71-
$paths = $container->getDefinition('twig.template_iterator')->getArgument(2);
72-
71+
$paths = array_keys($container->getDefinition('twig.template_iterator')->getArgument(2));
7372
if ($container->hasDefinition($this->debugCommandServiceId)) {
7473
$container->getDefinition($this->debugCommandServiceId)->replaceArgument(4, $container->getParameter('twig.default_path'));
7574
$container->getDefinition($this->debugCommandServiceId)->replaceArgument(6, $paths);
7675
}
77-
7876
if ($container->hasDefinition($this->updateCommandServiceId)) {
7977
$container->getDefinition($this->updateCommandServiceId)->replaceArgument(5, $container->getParameter('twig.default_path'));
8078
$container->getDefinition($this->updateCommandServiceId)->replaceArgument(7, $paths);
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Translation\DependencyInjection;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\AbstractRecursivePass;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Reference;
17+
18+
class TranslatorPathsPass extends AbstractRecursivePass
19+
{
20+
private $translatorServiceId;
21+
private $debugCommandServiceId;
22+
private $updateCommandServiceId;
23+
private $paths;
24+
25+
public function __construct(string $translatorServiceId = 'translator', string $debugCommandServiceId = 'console.command.translation_debug', string $updateCommandServiceId = 'console.command.translation_update')
26+
{
27+
$this->translatorServiceId = $translatorServiceId;
28+
$this->debugCommandServiceId = $debugCommandServiceId;
29+
$this->updateCommandServiceId = $updateCommandServiceId;
30+
}
31+
32+
public function process(ContainerBuilder $container)
33+
{
34+
if (!$container->hasDefinition($this->translatorServiceId)) {
35+
return;
36+
}
37+
38+
try {
39+
parent::process($container);
40+
41+
if (!$this->paths) {
42+
return;
43+
}
44+
$this->paths = array_keys($this->paths);
45+
46+
if ($container->hasDefinition($this->debugCommandServiceId)) {
47+
$definition = $container->getDefinition($this->debugCommandServiceId);
48+
$definition->replaceArgument(6, array_merge($definition->getArgument(6), $this->paths));
49+
}
50+
if ($container->hasDefinition($this->updateCommandServiceId)) {
51+
$definition = $container->getDefinition($this->updateCommandServiceId);
52+
$definition->replaceArgument(7, array_merge($definition->getArgument(7), $this->paths));
53+
}
54+
} finally {
55+
$this->paths = null;
56+
}
57+
}
58+
59+
protected function processValue($value, $isRoot = false)
60+
{
61+
if ($value instanceof Reference && (string) $value === $this->translatorServiceId && ($r = $this->container->getReflectionClass($this->container->getDefinition($this->currentId)->getClass())) && !$r->isInterface()) {
62+
$this->paths[$r->getFileName()] = $this->currentId;
63+
}
64+
65+
return parent::processValue($value, $isRoot);
66+
}
67+
}

src/Symfony/Component/Translation/Tests/DependencyInjection/TranslationPassTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\DependencyInjection\Definition;
1818
use Symfony\Component\DependencyInjection\Reference;
1919
use Symfony\Component\Translation\DependencyInjection\TranslatorPass;
20+
use Symfony\Component\Translation\Translator;
2021

2122
class TranslationPassTest extends TestCase
2223
{
@@ -54,4 +55,36 @@ public function testValidCollector()
5455
$expected = array('translation.xliff_loader' => new ServiceClosureArgument(new Reference('translation.xliff_loader')));
5556
$this->assertEquals($expected, $container->getDefinition((string) $translator->getArgument(0))->getArgument(0));
5657
}
58+
59+
public function testValidCommandsViewPathsArgument()
60+
{
61+
$translator = (new Definition(Translator::class))
62+
->setArguments(array(null, null, null, null));
63+
$debugCommand = (new Definition())
64+
->setArguments(array(null, null, null, null, null, array(), array()));
65+
$updateCommand = (new Definition())
66+
->setArguments(array(null, null, null, null, null, null, array(), array()));
67+
$twigTemplateIter 10000 ator = (new Definition())
68+
->setArguments(array(null, null, array('other/templates' => null, 'tpl' => 'App')));
69+
70+
$container = new ContainerBuilder();
71+
$container->setDefinition('translator.default', $translator);
72+
$container->setDefinition('console.command.translation_debug', $debugCommand);
73+
$container->setDefinition('console.command.translation_update', $updateCommand);
74+
$container->setDefinition('twig.template_iterator', $twigTemplateIterator);
75+
$container->setParameter('twig.default_path', 'templates');
76+
77+
$pass = new TranslatorPass('translator.default');
78+
$pass->process($container);
79+
80+
$viewPaths = array(
81+
'other/templates',
82+
'tpl',
83+
);
84+
85+
$this->assertSame('templates', $debugCommand->getArgument(4));
86+
$this->assertSame('templates', $updateCommand->getArgument(5));
87+
$this->assertSame($viewPaths, $debugCommand->getArgument(6));
88+
$this->assertSame($viewPaths, $updateCommand->getArgument(7));
89+
}
5790
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Translation\Tests\DependencyInjection;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Definition;
17+
use Symfony\Component\DependencyInjection\Reference;
18+
use Symfony\Component\Translation\DependencyInjection\TranslatorPathsPass;
19+
use Symfony\Component\Translation\Tests\DependencyInjection\fixtures\ServiceArguments;
20+
use Symfony\Component\Translation\Tests\DependencyInjection\fixtures\ServiceMethodCalls;
21+
use Symfony\Component\Translation\Tests\DependencyInjection\fixtures\ServiceProperties;
22+
use Symfony\Component\Translation\Translator;
23+
24+
class TranslationPathsPassTest extends TestCase
25+
{
26+
public function testValidCollector()
27+
{
28+
$translator = (new Definition(Translator::class))
29+
->setArguments(array(null, null, null, null));
30+
$debugCommand = (new Definition())
31+
->setArguments(array(null, null, null, null, null, array(), array()));
32+
$updateCommand = (new Definition())
33+
->setArguments(array(null, null, null, null, null, null, array(), array()));
34+
35+
$serviceProperties = (new Definition(ServiceProperties::class))
36+
->setProperties(array(new Reference('translator')));
37+
$serviceArguments = (new Definition(ServiceArguments::class))
38+
->setArguments(array(new Reference('translator')));
39+
$serviceMethodCalls = (new Definition(ServiceMethodCalls::class))
40+
->setMethodCalls(array(array('setTranslator', array(new Reference('translator')))));
41+
42+
$container = new ContainerBuilder();
43+
$container->setDefinition('translator', $translator);
44+
$container->setDefinition('console.command.translation_debug', $debugCommand);
45+
$container->setDefinition('console.command.translation_update', $updateCommand);
46+
$container->setDefinition('service_p', $serviceProperties);
47+
$container->setDefinition('service_a', $serviceArguments);
48+
$container->setDefinition('service_m', $serviceMethodCalls);
49+
50+
$pass = new TranslatorPathsPass('translator', 'console.command.translation_debug', 'console.command.translation_update');
51+
$pass->process($container);
52+
53+
$expectedPaths = array(
54+
$container->getReflectionClass(ServiceProperties::class)->getFileName(),
55+
$container->getReflectionClass(ServiceArguments::class)->getFileName(),
56+
$container->getReflectionClass(ServiceMethodCalls::class)->getFileName(),
57+
);
58+
59+
$this->assertSame($expectedPaths, $debugCommand->getArgument(6));
60+
$this->assertSame($expectedPaths, $updateCommand->getArgument(7));
61+
}
62+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Symfony\Component\Translation\Tests\DependencyInjection\fixtures;
4+
5+
use Symfony\Contracts\Translation\TranslatorInterface;
6+
7+
class ServiceArguments
8+
{
9+
public function __construct(TranslatorInterface $translator)
10+
{
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Symfony\Component\Translation\Tests\DependencyInjection\fixtures;
4+
5+
use Symfony\Contracts\Translation\TranslatorInterface;
6+
7+
class ServiceBindings
8+
{
9+
public function bindings(TranslatorInterface $translator)
10+
{
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Symfony\Component\Translation\Tests\DependencyInjection\fixtures;
4+
5+
use Symfony\Contracts\Translation\TranslatorInterface;
6+
7+
class ServiceMethodCalls
8+
{
9+
public function setTranslator(TranslatorInterface $translator)
10+
{
11+
}
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Symfony\Component\Translation\Tests\DependencyInjection\fixtures;
4+
5+
class ServiceProperties
6+
{
7+
public $translator;
8+
}

0 commit comments

Comments
 (0)
0