Skip to content

Commit

Permalink
[DI] fix locators with numeric keys
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Nov 8, 2019
1 parent b8cdc6e commit dad4344
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
Expand Up @@ -41,6 +41,8 @@ protected function processValue($value, $isRoot = false)
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;
Expand All @@ -49,10 +51,13 @@ protected function processValue($value, $isRoot = false)
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);
}
Expand Down
Expand Up @@ -114,6 +114,7 @@ public function testInheritedKeyOverwritesPreviousServiceWithKey()
->setArguments([[
'bar' => new Reference('baz'),
new Reference('bar'),
16 => new Reference('baz'),
]])
->addTag('container.service_locator')
;
Expand All @@ -124,6 +125,7 @@ public function testInheritedKeyOverwritesPreviousServiceWithKey()
$locator = $container->get('foo');

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

public function testBindingsAreCopied()
Expand Down

0 comments on commit dad4344

Please sign in to comment.