Skip to content

Commit 104e922

Browse files
feature symfony#28571 [DependencyInjection] Improve ServiceLocatorTagPass service matching (codedmonkey)
This PR was squashed before being merged into the 4.2-dev branch (closes symfony#28571). Discussion ---------- [DependencyInjection] Improve ServiceLocatorTagPass service matching | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#26892 | License | MIT | Doc PR | symfony/symfony-docs#10397 Allows omitting of keys for service locator arguments (it will automatically take over the original definition alias). Commits ------- 1c1210a [DependencyInjection] Improve ServiceLocatorTagPass service matching
2 parents 3b604ff + 1c1210a commit 104e922

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

src/Symfony/Component/DependencyInjection/Compiler/ServiceLocatorTagPass.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ protected function processValue($value, $isRoot = false)
5252
if (!$v instanceof Reference) {
5353
throw new InvalidArgumentException(sprintf('Invalid definition for service "%s": an array of references is expected as first argument when the "container.service_locator" tag is set, "%s" found for key "%s".', $this->currentId, \is_object($v) ? \get_class($v) : \gettype($v), $k));
5454
}
55+
56+
if (\is_int($k)) {
57+
unset($arguments[0][$k]);
58+
59+
$k = (string) $v;
60+
}
5561
$arguments[0][$k] = new ServiceClosureArgument($v);
5662
}
5763
ksort($arguments[0]);
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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\DependencyInjection\Tests\Compiler;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Reference;
18+
use Symfony\Component\DependencyInjection\ServiceLocator;
19+
use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition;
20+
use Symfony\Component\DependencyInjection\Tests\Fixtures\TestDefinition1;
21+
use Symfony\Component\DependencyInjection\Tests\Fixtures\TestDefinition2;
22+
23+
require_once __DIR__.'/../Fixtures/includes/classes.php';
24+
25+
class ServiceLocatorTagPassTest extends TestCase
26+
{
27+
/**
28+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
29+
* @expectedExceptionMessage Invalid definition for service "foo": an array of references is expected as first argument when the "container.service_locator" tag is set.
30+
*/
31+
public function testNoServices()
32+
{
33+
$container = new ContainerBuilder();
34+
35+
$container->register('foo', ServiceLocator::class)
36+
->addTag('container.service_locator')
37+
;
38+
39+
(new ServiceLocatorTagPass())->process($container);
40+
}
41+
42+
/**
43+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
44+
* @expectedExceptionMessage Invalid definition for service "foo": an array of references is expected as first argument when the "container.service_locator" tag is set, "string" found for key "0".
45+
*/
46+
public function testInvalidServices()
47+
{
48+
$container = new ContainerBuilder();
49+
50+
$container->register('foo', ServiceLocator::class)
51+
->setArguments(array(array(
52+
'dummy',
53+
)))
54+
->addTag('container.service_locator')
55+
;
56+
57+
(new ServiceLocatorTagPass())->process($container);
58+
}
59+
60+
public function testProcessValue()
61+
{
62+
$container = new ContainerBuilder();
63+
64+
$container->register('bar', CustomDefinition::class);
65+
$container->register('baz', CustomDefinition::class);
66+
67+
$container->register('foo', ServiceLocator::class)
68+
->setArguments(array(array(
69+
new Reference('bar'),
70+
new Reference('baz'),
71+
'some.service' => new Reference('bar'),
72+
)))
73+
->addTag('container.service_locator')
74+
;
75+
76+
(new ServiceLocatorTagPass())->process($container);
77+
78+
/** @var ServiceLocator $locator */
79+
$locator = $container->get('foo');
80+
81+
$this->assertSame(CustomDefinition::class, \get_class($locator('bar')));
82+
$this->assertSame(CustomDefinition::class, \get_class($locator('baz')));
83+
$this->assertSame(CustomDefinition::class, \get_class($locator('some.service')));
84+
}
85+
86+
public function testServiceWithKeyOverwritesPreviousInheritedKey()
87+
{
88+
$container = new ContainerBuilder();
89+
90+
$container->register('bar', TestDefinition1::class);
91+
$container->register('baz', TestDefinition2::class);
92+
93+
$container->register('foo', ServiceLocator::class)
94+
->setArguments(array(array(
95+
new Reference('bar'),
96+
'bar' => new Reference('baz'),
97+
)))
98+
->addTag('container.service_locator')
99+
;
100+
101+
(new ServiceLocatorTagPass())->process($container);
102+
103+
/** @var ServiceLocator $locator */
104+
$locator = $container->get('foo');
105+
106+
$this->assertSame(TestDefinition2::class, \get_class($locator('bar')));
107+
}
108+
109+
public function testInheritedKeyOverwritesPreviousServiceWithKey()
110+
{
111+
$container = new ContainerBuilder();
112+
113+
$container->register('bar', TestDefinition1::class);
114+
$container->register('baz', TestDefinition2::class);
115+
116+
$container->register('foo', ServiceLocator::class)
117+
->setArguments(array(array(
118+
'bar' => new Reference('baz'),
119+
new Reference('bar'),
120+
)))
121+
->addTag('container.service_locator')
122+
;
123+
124+
(new ServiceLocatorTagPass())->process($container);
125+
126+
/** @var ServiceLocator $locator */
127+
$locator = $container->get('foo');
128+
129+
$this->assertSame(TestDefinition1::class, \get_class($locator('bar')));
130+
}
131+
}

0 commit comments

Comments
 (0)