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

Commit 555a562

Browse filesBrowse files
committed
Collecting services filename
Type of injection: * Constructor Arguments * Public Property * Method Calls * Service Subscriber * Controller Arguments
1 parent 533d4d4 commit 555a562
Copy full SHA for 555a562
Expand file treeCollapse file tree

14 files changed

+313
-16
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Command/TranslationDebugCommand.php
+1-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Command/TranslationUpdateCommand.php
+1-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
+3-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationDebugCommandTest.php
+3-3Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationUpdateCommandTest.php
+3-3Lines changed: 3 additions & 3 deletions
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
$translator = $this->getMockBuilder('Symfony\Component\Translation\Translator')
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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/DependencyInjection/TranslatorPass.php
+7-7Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,16 @@ 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)) {
74-
$container->getDefinition($this->debugCommandServiceId)->replaceArgument(4, $container->getParameter('twig.default_path'));
75-
$container->getDefinition($this->debugCommandServiceId)->replaceArgument(6, $paths);
73+
$definition = $container->getDefinition($this->debugCommandServiceId);
74+
$definition->replaceArgument(4, $container->getParameter('twig.default_path'));
75+
$definition->replaceArgument(6, $paths);
7676
}
77-
7877
if ($container->hasDefinition($this->updateCommandServiceId)) {
79-
$container->getDefinition($this->updateCommandServiceId)->replaceArgument(5, $container->getParameter('twig.default_path'));
80-
$container->getDefinition($this->updateCommandServiceId)->replaceArgument(7, $paths);
78+
$definition = $container->getDefinition($this->updateCommandServiceId);
79+
$definition->replaceArgument(5, $container->getParameter('twig.default_path'));
80+
$definition->replaceArgument(7, $paths);
8181
}
8282
}
8383
}
+119Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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\Definition;
17+
use Symfony\Component\DependencyInjection\Reference;
18+
use Symfony\Component\DependencyInjection\ServiceLocator;
19+
20+
class TranslatorPathsPass extends AbstractRecursivePass
21+
{
22+
private $translatorServiceId;
23+
private $debugCommandServiceId;
24+
private $updateCommandServiceId;
25+
private $resolverServiceId;
26+
private $level = 0;
27+
private $paths = array();
28+
private $definitions = array();
29+
private $controllers = array();
30+
31+
public function __construct(string $translatorServiceId = 'translator', string $debugCommandServiceId = 'console.command.translation_debug', string $updateCommandServiceId = 'console.command.translation_update', string $resolverServiceId = 'argument_resolver.service')
32+
{
33+
$this->translatorServiceId = $translatorServiceId;
34+
$this->debugCommandServiceId = $debugCommandServiceId;
35+
$this->updateCommandServiceId = $updateCommandServiceId;
36+
$this->resolverServiceId = $resolverServiceId;
37+
}
38+
39+
public function process(ContainerBuilder $container)
40+
{
41+
if (!$container->hasDefinition($this->translatorServiceId)) {
42+
return;
43+
}
44+
45+
$controllerLocator = array();
46+
if ($container->hasDefinition($this->resolverServiceId)) {
47+
$controllerLocator = $container->getDefinition($this->resolverServiceId)->getArgument(0)->getArgument(0);
48+
} elseif ($container->hasDefinition('debug.'.$this->resolverServiceId)) {
49+
$controllerLocator = $container->getDefinition('debug.'.$this->resolverServiceId)->getArgument(0)->getArgument(0)->getArgument(0);
50+
}
51+
foreach ($controllerLocator as $controller => $argument) {
52+
list($ref) = $argument->getValues();
53+
$this->controllers[(string) $ref][substr($controller, 0, strpos($controller, ':'))] = true;
54+
}
55+
56+
try {
57+
parent::process($container);
58+
59+
$paths = array();
60+
foreach ($this->paths as $class => $_) {
61+
if (($r = $container->getReflectionClass($class)) && !$r->isInterface()) {
62+
$paths[] = $r->getFileName();
63+
}
64+
}
65+
if (!$paths) {
66+
return;
67+
}
68+
69+
if ($container->hasDefinition($this->debugCommandServiceId)) {
70+
$definition = $container->getDefinition($this->debugCommandServiceId);
71+
$definition->replaceArgument(6, array_merge($definition->getArgument(6), $paths));
72+
}
73+
if ($container->hasDefinition($this->updateCommandServiceId)) {
74+
$definition = $container->getDefinition($this->updateCommandServiceId);
75+
$definition->replaceArgument(7, array_merge($definition->getArgument(7), $paths));
76+
}
77+
} finally {
78+
$this->level = 0;
79+
$this->paths = array();
80+
$this->definitions = array();
81+
}
82+
}
83+
84+
protected function processValue($value, $isRoot = false)
85+
{
86+
if ($value instanceof Reference) {
87+
if ((string) $value === $this->translatorServiceId) {
88+
for ($i = $this->level - 1; $i >= 0; --$i) {
89+
$class = $this->definitions[$i]->getClass();
90+
91+
if (ServiceLocator::class === $class) {
92+
if (!isset($this->controllers[$this->currentId])) {
93+
continue;
94+
}
95+
foreach ($this->controllers[$this->currentId] as $class => $_) {
96+
$this->paths[$class] = true;
97+
}
98+
} else {
99+
$this->paths[$class] = true;
100+
}
101+
102+
break;
103+
}
104+
}
105+
106+
return $value;
107+
}
108+
109+
if ($value instanceof Definition) {
110+
$this->definitions[$this->level++] = $value;
111+
$value = parent::processValue($value, $isRoot);
112+
unset($this->definitions[--$this->level]);
113+
114+
return $value;
115+
}
116+
117+
return parent::processValue($value, $isRoot);
118+
}
119+
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Tests/DependencyInjection/TranslationPassTest.php
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,32 @@ public function testValidCollector()
5454
$expected = array('translation.xliff_loader' => new ServiceClosureArgument(new Reference('translation.xliff_loader')));
5555
$this->assertEquals($expected, $container->getDefinition((string) $translator->getArgument(0))->getArgument(0));
5656
}
57+
58+
public function testValidCommandsViewPathsArgument()
59+
{
60+
$container = new ContainerBuilder();
61+
$container->register('translator.default')
62+
->setArguments(array(null, null, null, null))
63+
;
64+
$debugCommand = $container->register('console.command.translation_debug')
65+
->setArguments(array(null, null, null, null, null, array(), array()))
66+
;
67+
$updateCommand = $container->register('console.command.translation_update')
68+
->setArguments(array(null, null, null, null, null, null, array(), array()))
69+
;
70+
$container->register('twig.template_iterator')
71+
->setArguments(array(null, null, array('other/templates' => null, 'tpl' => 'App')))
72+
;
73+
$container->setParameter('twig.default_path', 'templates');
74+
75+
$pass = new TranslatorPass('translator.default');
76+
$pass->process($container);
77+
78+
$expectedViewPaths = array('other/templates', 'tpl');
79+
80+
$this->assertSame('templates', $debugCommand->getArgument(4));
81+
$this->assertSame('templates', $updateCommand->getArgument(5));
82+
$this->assertSame($expectedViewPaths, $debugCommand->getArgument(6));
83+
$this->assertSame($expectedViewPaths, $updateCommand->getArgument(7));
84+
}
5785
}
+85Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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\Argument\ServiceClosureArgument;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Definition;
18+
use Symfony\Component\DependencyInjection\Reference;
19+
use Symfony\Component\DependencyInjection\ServiceLocator;
20+
use Symfony\Component\Translation\DependencyInjection\TranslatorPathsPass;
21+
use Symfony\Component\Translation\Tests\DependencyInjection\fixtures\ControllerArguments;
22+
use Symfony\Component\Translation\Tests\DependencyInjection\fixtures\ServiceArguments;
23+
use Symfony\Component\Translation\Tests\DependencyInjection\fixtures\ServiceMethodCalls;
24+
use Symfony\Component\Translation\Tests\DependencyInjection\fixtures\ServiceProperties;
25+
use Symfony\Component\Translation\Tests\DependencyInjection\fixtures\ServiceSubscriber;
26+
27+
class TranslationPathsPassTest extends TestCase
28+
{
29+
public function testProcess()
30+
{
31+
$container = new ContainerBuilder();
32+
$container->register('translator');
33+
$debugCommand = $container->register('console.command.translation_debug')
34+
->setArguments(array(null, null, null, null, null, array(), array()))
35+
;
36+
$updateCommand = $container->register('console.command.translation_update')
37+
->setArguments(array(null, null, null, null, null, null, array(), array()))
38+
;
39+
$container->register(ControllerArguments::class, ControllerArguments::class)
40+
->setTags(array('controller.service_arguments'))
41+
;
42+
$container->register(ServiceArguments::class, ServiceArguments::class)
43+
->setArguments(array(new Reference('translator')))
44+
;
45+
$container->register(ServiceProperties::class, ServiceProperties::class)
46+
->setProperties(array(new Reference('translator')))
47+
;
48+
$container->register(ServiceMethodCalls::class, ServiceMethodCalls::class)
49+
->setMethodCalls(array(array('setTranslator', array(new Reference('translator')))))
50+
;
51+
$container->register('service_rc')
52+
->setArguments(array(new Definition(), new Reference(ServiceMethodCalls::class)))
53+
;
54+
$serviceLocator1 = $container->register('.service_locator.foo', ServiceLocator::class)
55+
->setArguments(array(new ServiceClosureArgument(new Reference('translator'))))
56+
;
57+
$serviceLocator2 = (new Definition(ServiceLocator::class))
58+
->setArguments(array(ServiceSubscriber::class, new Reference('service_container')))
59+
->setFactory(array($serviceLocator1, 'withContext'))
60+
;
61+
$container->register('service_subscriber', ServiceSubscriber::class)
62+
->setArguments(array($serviceLocator2))
63+
;
64+
$serviceLocator3 = (new Definition(ServiceLocator::class))
65+
->setArguments(array(array(ControllerArguments::class.'::index' => new ServiceClosureArgument(new Reference('.service_locator.foo')))))
66+
;
67+
$container->register('argument_resolver.service')
68+
->setArguments(array($serviceLocator3))
69+
;
70+
71+
$pass = new TranslatorPathsPass('translator', 'console.command.translation_debug', 'console.command.translation_update', 'argument_resolver.service');
72+
$pass->process($container);
73+
74+
$expectedPaths = array(
75+
$container->getReflectionClass(ServiceArguments::class)->getFileName(),
76+
$container->getReflectionClass(ServiceProperties::class)->getFileName(),
77+
$container->getReflectionClass(ServiceMethodCalls::class)->getFileName(),
78+
$container->getReflectionClass(ControllerArguments::class)->getFileName(),
79+
$container->getReflectionClass(ServiceSubscriber::class)->getFileName(),
80+
);
81+
82+
$this->assertSame($expectedPaths, $debugCommand->getArgument(6));
83+
$this->assertSame($expectedPaths, $updateCommand->getArgument(7));
84+
}
85+
}
+12Lines 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 ControllerArguments
8+
{
9+
public function index(TranslatorInterface $translator)
10+
{
11+
}
12+
}
+12Lines 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+
}
+12Lines 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+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.