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
-------

dad4344793 [DI] fix locators with numeric keys
This commit is contained in:
Nicolas Grekas 2019-11-08 17:22:08 +01:00
commit 98e9fc8aee
2 changed files with 8 additions and 1 deletions

View File

@ -41,6 +41,8 @@ final class ServiceLocatorTagPass extends AbstractRecursivePass
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));
}
$i = 0;
foreach ($arguments[0] as $k => $v) {
if ($v instanceof ServiceClosureArgument) {
continue;
@ -49,10 +51,13 @@ final class ServiceLocatorTagPass extends AbstractRecursivePass
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));
}
if (\is_int($k)) {
if ($i === $k) {
unset($arguments[0][$k]);
$k = (string) $v;
++$i;
} elseif (\is_int($k)) {
$i = null;
}
$arguments[0][$k] = new ServiceClosureArgument($v);
}

View File

@ -114,6 +114,7 @@ class ServiceLocatorTagPassTest extends TestCase
->setArguments([[
'bar' => new Reference('baz'),
new Reference('bar'),
16 => new Reference('baz'),
]])
->addTag('container.service_locator')
;
@ -124,6 +125,7 @@ class ServiceLocatorTagPassTest extends TestCase
$locator = $container->get('foo');
$this->assertSame(TestDefinition1::class, \get_class($locator('bar')));
$this->assertSame(TestDefinition2::class, \get_class($locator(16)));
}
public function testBindingsAreCopied()