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 f74603c

Browse filesBrowse files
committed
Collecting service subscriber filename
1 parent de13e61 commit f74603c
Copy full SHA for f74603c

File tree

Expand file treeCollapse file tree

4 files changed

+77
-44
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+77
-44
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/DependencyInjection/TranslatorPathsPass.php
+11-5Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,23 @@ public function process(ContainerBuilder $container)
6565
protected function processValue($value, $isRoot = false)
6666
{
6767
if ($value instanceof Reference) {
68-
if ((string) $value === $this->translatorServiceId && isset($this->definitions[$this->level - 1])) {
69-
$class = $this->definitions[$this->level - 1]->getClass();
70-
if (!isset($this->paths[$class]) && ($r = $this->container->getReflectionClass($class)) && !$r->isInterface()) {
71-
$this->paths[$class] = $r->getFileName();
68+
if ((string) $value === $this->translatorServiceId) {
69+
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();
75+
}
76+
77+
break;
7278
}
7379
}
7480

7581
return $value;
7682
}
7783

78-
if ($value instanceof Definition && ServiceLocator::class !== $value->getClass()) {
84+
if ($value instanceof Definition) {
7985
$this->definitions[$this->level++] = $value;
8086
$value = parent::processValue($value, $isRoot);
8187
unset($this->definitions[--$this->level]);

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Tests/DependencyInjection/TranslationPassTest.php
+15-20Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
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;
2120

2221
class TranslationPassTest extends TestCase
2322
{
@@ -58,33 +57,29 @@ public function testValidCollector()
5857

5958
public function testValidCommandsViewPathsArgument()
6059
{
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-
$twigTemplateIterator = (new Definition())
68-
->setArguments(array(null, null, array('other/templates' => null, 'tpl' => 'App')));
69-
7060
$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);
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+
;
7573
$container->setParameter('twig.default_path', 'templates');
7674

7775
$pass = new TranslatorPass('translator.default');
7876
$pass->process($container);
7977

80-
$viewPaths = array(
81-
'other/templates',
82-
'tpl',
83-
);
78+
$expectedViewPaths = array('other/templates', 'tpl');
8479

8580
$this->assertSame('templates', $debugCommand->getArgument(4));
8681
$this->assertSame('templates', $updateCommand->getArgument(5));
87-
$this->assertSame($viewPaths, $debugCommand->getArgument(6));
88-
$this->assertSame($viewPaths, $updateCommand->getArgument(7));
82+
$this->assertSame($expectedViewPaths, $debugCommand->getArgument(6));
83+
$this->assertSame($expectedViewPaths, $updateCommand->getArgument(7));
8984
}
9085
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Tests/DependencyInjection/TranslationPathsPassTest.php
+32-19Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,51 @@
1212
namespace Symfony\Component\Translation\Tests\DependencyInjection;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
1516
use Symfony\Component\DependencyInjection\ContainerBuilder;
1617
use Symfony\Component\DependencyInjection\Definition;
1718
use Symfony\Component\DependencyInjection\Reference;
19+
use Symfony\Component\DependencyInjection\ServiceLocator;
1820
use Symfony\Component\Translation\DependencyInjection\TranslatorPathsPass;
1921
use Symfony\Component\Translation\Tests\DependencyInjection\fixtures\ServiceArguments;
2022
use Symfony\Component\Translation\Tests\DependencyInjection\fixtures\ServiceMethodCalls;
2123
use Symfony\Component\Translation\Tests\DependencyInjection\fixtures\ServiceProperties;
24+
use Symfony\Component\Translation\Tests\DependencyInjection\fixtures\ServiceSubscriber;
2225

2326
class TranslationPathsPassTest extends TestCase
2427
{
2528
public function testProcess()
2629
{
27-
$debugCommand = (new Definition())
28-
->setArguments(array(null, null, null, null, null, array(), array()));
29-
$updateCommand = (new Definition())
30-
->setArguments(array(null, null, null, null, null, null, array(), array()));
31-
32-
$serviceArguments = (new Definition(ServiceArguments::class))
33-
->setArguments(array(new Reference('translator')));
34-
$serviceProperties = (new Definition(ServiceProperties::class))
35-
->setProperties(array(new Reference('translator')));
36-
$serviceMethodCalls = (new Definition(ServiceMethodCalls::class))
37-
->setMethodCalls(array(array('setTranslator', array(new Reference('translator')))));
38-
$rootService = (new Definition())
39-
->setArguments(array(new Definition(), $serviceMethodCalls));
40-
4130
$container = new ContainerBuilder();
4231
$container->register('translator');
43-
$container->setDefinition('console.command.translation_debug', $debugCommand);
44-
$container->setDefinition('console.command.translation_update', $updateCommand);
45-
$container->setDefinition('service_a', $serviceArguments);
46-
$container->setDefinition('service_p', $serviceProperties);
47-
$container->setDefinition('service_r_c', $rootService);
32+
$debugCommand = $container->register('console.command.translation_debug')
33+
->setArguments(array(null, null, null, null, null, array(), array()))
34+
;
35+
$updateCommand = $container->register('console.command.translation_update')
36+
->setArguments(array(null, null, null, null, null, null, array(), array()))
37+
;
38+
$container->register('service_a', ServiceArguments::class)
39+
->setArguments(array(new Reference('translator')))
40+
;
41+
$container->register('service_p', ServiceProperties::class)
42+
->setProperties(array(new Reference('translator')))
43+
;
44+
$container->register('service_c', ServiceMethodCalls::class)
45+
->setMethodCalls(array(array('setTranslator', array(new Reference('translator')))))
46+
;
47+
$container->register('service_rc')
48+
->setArguments(array(new Definition(), new Reference('service_c')))
49+
;
50+
$serviceLocator1 = (new Definition(ServiceLocator::class))
51+
->setArguments(array(new ServiceClosureArgument(new Reference('translator'))))
52+
;
53+
$serviceLocator2 = (new Definition(ServiceLocator::class))
54+
->setArguments(array(ServiceSubscriber::class, new Reference('service_container')))
55+
->setFactory(array($serviceLocator1, 'withContext'))
56+
;
57+
$container->register('service_subscriber', ServiceSubscriber::class)
58+
->setArguments(array($serviceLocator2))
59+
;
4860

4961
$pass = new TranslatorPathsPass('translator', 'console.command.translation_debug', 'console.command.translation_update');
5062
$pass->process($container);
@@ -53,6 +65,7 @@ public function testProcess()
5365
$container->getReflectionClass(ServiceArguments::class)->getFileName(),
5466
$container->getReflectionClass(ServiceProperties::class)->getFileName(),
5567
$container->getReflectionClass(ServiceMethodCalls::class)->getFileName(),
68+
$container->getReflectionClass(ServiceSubscriber::class)->getFileName(),
5669
);
5770

5871
$this->assertSame($expectedPaths, $debugCommand->getArgument(6));
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Symfony\Component\Translation\Tests\DependencyInjection\fixtures;
4+
5+
use Psr\Container\ContainerInterface;
6+
use Symfony\Contracts\Service\ServiceSubscriberInterface;
7+
use Symfony\Contracts\Translation\TranslatorInterface;
8+
9+
class ServiceSubscriber implements ServiceSubscriberInterface
10+
{
11+
public function __construct(ContainerInterface $container)
12+
{
13+
}
14+
15+
public static function getSubscribedServices()
16+
{
17+
return array('translator' => TranslatorInterface::class);
18+
}
19+
}

0 commit comments

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