Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 98e9fc8

Browse files
bug #34297 [DI] fix locators with numeric keys (nicolas-grekas)
This PR was merged into the 3.4 branch. Discussion ---------- [DI] fix locators with numeric keys | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #34296 | License | MIT | Doc PR | - Commits ------- dad4344 [DI] fix locators with numeric keys
2 parents b8cdc6e + dad4344 commit 98e9fc8

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ protected function processValue($value, $isRoot = false)
4141
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.', $this->currentId));
4242
}
4343

44+
$i = 0;
45+
4446
foreach ($arguments[0] as $k => $v) {
4547
if ($v instanceof ServiceClosureArgument) {
4648
continue;
@@ -49,10 +51,13 @@ protected function processValue($value, $isRoot = false)
4951
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));
5052
}
5153

52-
if (\is_int($k)) {
54+
if ($i === $k) {
5355
unset($arguments[0][$k]);
5456

5557
$k = (string) $v;
58+
++$i;
59+
} elseif (\is_int($k)) {
60+
$i = null;
5661
}
5762
$arguments[0][$k] = new ServiceClosureArgument($v);
5863
}

src/Symfony/Component/DependencyInjection/Tests/Compiler/ServiceLocatorTagPassTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ public function testInheritedKeyOverwritesPreviousServiceWithKey()
114114
->setArguments([[
115115
'bar' => new Reference('baz'),
116116
new Reference('bar'),
117+
16 => new Reference('baz'),
117118
]])
118119
->addTag('container.service_locator')
119120
;
@@ -124,6 +125,7 @@ public function testInheritedKeyOverwritesPreviousServiceWithKey()
124125
$locator = $container->get('foo');
125126

126127
$this->assertSame(TestDefinition1::class, \get_class($locator('bar')));
128+
$this->assertSame(TestDefinition2::class, \get_class($locator(16)));
127129
}
128130

129131
public function testBindingsAreCopied()

0 commit comments

Comments
 (0)