-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] Use ID of the original service when key not specified in service_locator config #48653
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,7 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed | |
$value->setValues($this->findAndSortTaggedServices($value->getTaggedIteratorArgument(), $this->container)); | ||
} | ||
|
||
return self::register($this->container, $value->getValues()); | ||
return self::register($this->container, $this->replaceNumericServiceLocatorKeys($value->getValues())); | ||
} | ||
|
||
if ($value instanceof Definition) { | ||
|
@@ -62,26 +62,15 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed | |
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; | ||
$services = $this->replaceNumericServiceLocatorKeys($services); | ||
|
||
foreach ($services as $k => $v) { | ||
if ($v instanceof ServiceClosureArgument) { | ||
continue; | ||
} | ||
|
||
if ($i === $k) { | ||
if ($v instanceof Reference) { | ||
unset($services[$k]); | ||
$k = (string) $v; | ||
} | ||
++$i; | ||
} elseif (\is_int($k)) { | ||
$i = null; | ||
} | ||
|
||
$services[$k] = new ServiceClosureArgument($v); | ||
} | ||
ksort($services); | ||
|
||
$value->setArgument(0, $services); | ||
|
||
|
@@ -100,6 +89,39 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed | |
return new Reference($id); | ||
} | ||
|
||
/** | ||
* Replaces numeric service IDs with service names. | ||
* | ||
* @param array<array-key, mixed> $services | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
private function replaceNumericServiceLocatorKeys(array $services): array | ||
{ | ||
$i = 0; | ||
|
||
foreach ($services as $k => $v) { | ||
if ($i === $k && $v instanceof Reference) { | ||
unset($services[$k]); | ||
$k = (string) $v; | ||
++$i; | ||
} elseif (\is_int($k)) { | ||
/** | ||
* Not consecutive numbers, so stop replacing names but continue the loop. | ||
* | ||
* @see ServiceLocatorTagPassTest::testInheritedKeyOverwritesPreviousServiceWithKey | ||
*/ | ||
$i = null; | ||
} | ||
|
||
$services[$k] = $v; | ||
} | ||
|
||
ksort($services); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I knew... I extracted this code for reusing. The |
||
|
||
return $services; | ||
} | ||
|
||
public static function register(ContainerBuilder $container, array $map, string $callerId = null): Reference | ||
{ | ||
foreach ($map as $k => $v) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my view, this needs documentation to explain what it is doing and why. The method name certainly gives no indication as to what is going on here.
The method name could probably be improved along these lines, also, e.g. replaceNumericServiceLocatorKeys.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion!