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

Skip to content

[DI] fix locators with numeric keys #34297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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