Skip to content

Commit d0e1039

Browse files
committed
feature #40800 [DependencyInjection] Add #[Target] to tell how a dependency is used and hint named autowiring aliases (nicolas-grekas)
This PR was merged into the 5.3-dev branch. Discussion ---------- [DependencyInjection] Add `#[Target]` to tell how a dependency is used and hint named autowiring aliases | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Right now, when one wants to target a specific service in a list of candidates, we rely on the name of the argument in addition to the type-hint, eg: `function foo(WorkflowInterface $reviewStateMachine)` The deal is that by giving the argument a name that matches the target use case of the required dependency, we make autowiring more useful. But sometimes, being able to de-correlate the name of the argument and the purpose is desired. This PR introduces a new `#[Target]` attribute on PHP8 that allows doing so. The previous example could be written as such thanks to it: `function foo(#[Target('review.state_machine')] WorkflowInterface $workflow)` That's all folks :) Commits ------- cc76eab795 [DependencyInjection] Add `#[Target]` to tell how a dependency is used and hint named autowiring aliases
2 parents 84bd0f2 + 34640ee commit d0e1039

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

Command/DebugAutowiringCommand.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8181
$serviceIds = array_filter($serviceIds, [$this, 'filterToServiceTypes']);
8282

8383
if ($search = $input->getArgument('search')) {
84-
$serviceIds = array_filter($serviceIds, function ($serviceId) use ($search) {
85-
return false !== stripos(str_replace('\\', '', $serviceId), $search) && 0 !== strpos($serviceId, '.');
84+
$searchNormalized = preg_replace('/[^a-zA-Z0-9\x7f-\xff]++/', '', $search);
85+
$serviceIds = array_filter($serviceIds, function ($serviceId) use ($searchNormalized) {
86+
return false !== stripos(str_replace('\\', '', $serviceId), $searchNormalized) && 0 !== strpos($serviceId, '.');
8687
});
8788

8889
if (empty($serviceIds)) {

0 commit comments

Comments
 (0)