Skip to content

Commit 5d56766

Browse files
committed
Collecting controller filename
1 parent f74603c commit 5d56766

File tree

3 files changed

+64
-16
lines changed

3 files changed

+64
-16
lines changed

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

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,18 @@ class TranslatorPathsPass extends AbstractRecursivePass
2222
private $translatorServiceId;
2323
private $debugCommandServiceId;
2424
private $updateCommandServiceId;
25+
private $resolverServiceId;
2526
private $level = 0;
2627
private $paths = array();
2728
private $definitions = array();
29+
private $controllers = array();
2830

29-
public function __construct(string $translatorServiceId = 'translator', string $debugCommandServiceId = 'console.command.translation_debug', string $updateCommandServiceId = 'console.command.translation_update')
31+
public function __construct(string $translatorServiceId = 'translator', string $debugCommandServiceId = 'console.command.translation_debug', string $updateCommandServiceId = 'console.command.translation_update', string $resolverServiceId = 'argument_resolver.service')
3032
{
3133
$this->translatorServiceId = $translatorServiceId;
3234
$this->debugCommandServiceId = $debugCommandServiceId;
3335
$this->updateCommandServiceId = $updateCommandServiceId;
36+
$this->resolverServiceId = $resolverServiceId;
3437
}
3538

3639
public function process(ContainerBuilder $container)
@@ -39,21 +42,37 @@ public function process(ContainerBuilder $container)
3942
return;
4043
}
4144

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+
4256
try {
4357
parent::process($container);
4458

45-
if (!$this->paths) {
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) {
4666
return;
4767
}
48-
$this->paths = array_values($this->paths);
4968

5069
if ($container->hasDefinition($this->debugCommandServiceId)) {
5170
$definition = $container->getDefinition($this->debugCommandServiceId);
52-
$definition->replaceArgument(6, array_merge($definition->getArgument(6), $this->paths));
71+
$definition->replaceArgument(6, array_merge($definition->getArgument(6), $paths));
5372
}
5473
if ($container->hasDefinition($this->updateCommandServiceId)) {
5574
$definition = $container->getDefinition($this->updateCommandServiceId);
56-
$definition->replaceArgument(7, array_merge($definition->getArgument(7), $this->paths));
75+
$definition->replaceArgument(7, array_merge($definition->getArgument(7), $paths));
5776
}
5877
} finally {
5978
$this->level = 0;
@@ -67,11 +86,17 @@ protected function processValue($value, $isRoot = false)
6786
if ($value instanceof Reference) {
6887
if ((string) $value === $this->translatorServiceId) {
6988
for ($i = $this->level - 1; $i >= 0; --$i) {
70-
if (ServiceLocator::class === $class = $this->definitions[$i]->getClass()) {
71-
continue;
72-
}
73-
if (!isset($this->paths[$class]) && ($r = $this->container->getReflectionClass($class, false)) && !$r->isInterface()) {
74-
$this->paths[$class] = $r->getFileName();
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;
75100
}
76101

77102
break;

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\DependencyInjection\Reference;
1919
use Symfony\Component\DependencyInjection\ServiceLocator;
2020
use Symfony\Component\Translation\DependencyInjection\TranslatorPathsPass;
21+
use Symfony\Component\Translation\Tests\DependencyInjection\fixtures\ControllerArguments;
2122
use Symfony\Component\Translation\Tests\DependencyInjection\fixtures\ServiceArguments;
2223
use Symfony\Component\Translation\Tests\DependencyInjection\fixtures\ServiceMethodCalls;
2324
use Symfony\Component\Translation\Tests\DependencyInjection\fixtures\ServiceProperties;
@@ -35,19 +36,22 @@ public function testProcess()
3536
$updateCommand = $container->register('console.command.translation_update')
3637
->setArguments(array(null, null, null, null, null, null, array(), array()))
3738
;
38-
$container->register('service_a', ServiceArguments::class)
39+
$container->register(ControllerArguments::class, ControllerArguments::class)
40+
->setTags(array('controller.service_arguments'))
41+
;
42+
$container->register(ServiceArguments::class, ServiceArguments::class)
3943
->setArguments(array(new Reference('translator')))
4044
;
41-
$container->register('service_p', ServiceProperties::class)
45+
$container->register(ServiceProperties::class, ServiceProperties::class)
4246
->setProperties(array(new Reference('translator')))
4347
;
44-
$container->register('service_c', ServiceMethodCalls::class)
48+
$container->register(ServiceMethodCalls::class, ServiceMethodCalls::class)
4549
->setMethodCalls(array(array('setTranslator', array(new Reference('translator')))))
4650
;
4751
$container->register('service_rc')
48-
->setArguments(array(new Definition(), new Reference('service_c')))
52+
->setArguments(array(new Definition(), new Reference(ServiceMethodCalls::class)))
4953
;
50-
$serviceLocator1 = (new Definition(ServiceLocator::class))
54+
$serviceLocator1 = $container->register('.service_locator.foo', ServiceLocator::class)
5155
->setArguments(array(new ServiceClosureArgument(new Reference('translator'))))
5256
;
5357
$serviceLocator2 = (new Definition(ServiceLocator::class))
@@ -57,14 +61,21 @@ public function testProcess()
5761
$container->register('service_subscriber', ServiceSubscriber::class)
5862
->setArguments(array($serviceLocator2))
5963
;
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+
;
6070

61-
$pass = new TranslatorPathsPass('translator', 'console.command.translation_debug', 'console.command.translation_update');
71+
$pass = new TranslatorPathsPass('translator', 'console.command.translation_debug', 'console.command.translation_update', 'argument_resolver.service');
6272
$pass->process($container);
6373

6474
$expectedPaths = array(
6575
$container->getReflectionClass(ServiceArguments::class)->getFileName(),
6676
$container->getReflectionClass(ServiceProperties::class)->getFileName(),
6777
$container->getReflectionClass(ServiceMethodCalls::class)->getFileName(),
78+
$container->getReflectionClass(ControllerArguments::class)->getFileName(),
6879
$container->getReflectionClass(ServiceSubscriber::class)->getFileName(),
6980
);
7081

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 ControllerArguments
8+
{
9+
public function index(TranslatorInterface $translator)
10+
{
11+
}
12+
}

0 commit comments

Comments
 (0)