Skip to content

Commit 9774144

Browse files
committed
minor #40848 [DependencyInjection] Improve autowiring errors when named autowiring aliases exist (nicolas-grekas)
This PR was merged into the 5.3-dev branch. Discussion ---------- [DependencyInjection] Improve autowiring errors when named autowiring aliases exist | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Before: `Cannot autowire service "App\Controller\HelloController": argument "$workflow" of method "__construct()" references interface "Symfony\Component\Workflow\WorkflowInterface" but no such service exists. You should maybe alias this interface to one of these existing services: "state_machine.pull_request", "state_machine.comment".` After: `Cannot autowire service "App\Controller\HelloController": argument "$workflow" of method "__construct()" references interface "Symfony\Component\Workflow\WorkflowInterface" but no such service exists. Available autowiring aliases for this interface are: "$pullRequestStateMachine", "$commentStateMachine".` /cc `@lyrixx` `@weaverryan` Commits ------- 88eb0b57c1 [DependencyInjection] Improve autowiring errors when named autowiring aliases exist
2 parents 84f6280 + 9665e30 commit 9774144

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Compiler/AutowirePass.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class AutowirePass extends AbstractRecursivePass
3434
{
3535
private $types;
3636
private $ambiguousServiceTypes;
37+
private $autowiringAliases;
3738
private $lastFailure;
3839
private $throwOnAutowiringException;
3940
private $decoratedClass;
@@ -344,10 +345,15 @@ private function populateAvailableTypes(ContainerBuilder $container)
344345
{
345346
$this->types = [];
346347
$this->ambiguousServiceTypes = [];
348+
$this->autowiringAliases = [];
347349

348350
foreach ($container->getDefinitions() as $id => $definition) {
349351
$this->populateAvailableType($container, $id, $definition);
350352
}
353+
354+
foreach ($container->getAliases() as $id => $alias) {
355+
$this->populateAutowiringAlias($id);
356+
}
351357
}
352358

353359
/**
@@ -371,6 +377,8 @@ private function populateAvailableType(ContainerBuilder $container, string $id,
371377
do {
372378
$this->set($reflectionClass->name, $id);
373379
} while ($reflectionClass = $reflectionClass->getParentClass());
380+
381+
$this->populateAutowiringAlias($id);
374382
}
375383

376384
/**
@@ -460,6 +468,10 @@ private function createTypeAlternatives(ContainerBuilder $container, TypedRefere
460468
}
461469

462470
$servicesAndAliases = $container->getServiceIds();
471+
if (null !== ($autowiringAliases = $this->autowiringAliases[$type] ?? null) && !isset($autowiringAliases[''])) {
472+
return sprintf(' Available autowiring aliases for this %s are: "$%s".', class_exists($type, false) ? 'class' : 'interface', implode('", "$', $autowiringAliases));
473+
}
474+
463475
if (!$container->has($type) && false !== $key = array_search(strtolower($type), array_map('strtolower', $servicesAndAliases))) {
464476
return sprintf(' Did you mean "%s"?', $servicesAndAliases[$key]);
465477
} elseif (isset($this->ambiguousServiceTypes[$type])) {
@@ -498,4 +510,18 @@ private function getAliasesSuggestionForType(ContainerBuilder $container, string
498510

499511
return null;
500512
}
513+
514+
private function populateAutowiringAlias(string $id): void
515+
{
516+
if (!preg_match('/(?(DEFINE)(?<V>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+))^((?&V)(?:\\\\(?&V))*+)(?: \$((?&V)))?$/', $id, $m)) {
517+
return;
518+
}
519+
520+
$type = $m[2];
521+
$name = $m[3] ?? '';
522+
523+
if (class_exists($type, false) || interface_exists($type, false)) {
524+
$this->autowiringAliases[$type][$name] = $name;
525+
}
526+
}
501527
}

0 commit comments

Comments
 (0)