8000 improved message when alias service is not found · symfony/symfony@3e31fb5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3e31fb5

Browse files
committed
improved message when alias service is not found
When using the YAML config format, it can be confusing that you need to prefix the aliased service id with an `@` character when passing it as a string, but that you have to omit it when using the `alias` attribute: ```yaml foo: '@app\Foo' foo: alias: 'App\Foo' ``` This commit will enhance the generated error message in cases where the aliased service id is prefixed with the `@` character in the `alias` option like this: ```yaml foo: alias: '@app\Foo' ```
1 parent eb71aaf commit 3e31fb5

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\Console\Output\OutputInterface;
2323
use Symfony\Component\Console\Style\SymfonyStyle;
2424
use Symfony\Component\DependencyInjection\ContainerBuilder;
25+
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
2526
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
2627
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
2728

@@ -144,7 +145,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
144145
$options['show_hidden'] = $input->getOption('show-hidden');
145146
$options['raw_text'] = $input->getOption('raw');
146147
$options['output'] = $io;
147-
$helper->describe($io, $object, $options);
148+
149+
try {
150+
$helper->describe($io, $object, $options);
151+
} catch (ServiceNotFoundException $e) {
152+
if ('' !== $e->getId() && '@' === $e->getId()) {
153+
throw new ServiceNotFoundException($e->getId(), $e->getSourceId(), null, array(substr($targetId, 1)));
154+
}
155+
156+
throw $e;
157+
}
148158

149159
if (!$input->getArgument('name') && !$input->getOption('tag') && !$input->getOption('parameter') && $input->isInteractive()) {
150160
if ($input->getOption('tags')) {

src/Symfony/Component/DependencyInjection/Compiler/ReplaceAliasByActualDefinitionPass.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,13 @@ public function process(ContainerBuilder $container)
5454
try {
5555
$definition = $container->getDefinition($targetId);
5656
} catch (InvalidArgumentException $e) {
57-
throw new InvalidArgumentException(sprintf('Unable to replace alias "%s" with actual definition "%s".', $definitionId, $targetId), null, $e);
57+
$message = sprintf('Unable to replace alias "%s" with actual definition "%s".', $definitionId, $targetId);
58+
59+
if ('' !== $targetId && '@' === $targetId[0]) {
60+
$message .= sprintf(' Did you mean "%s"?', substr($targetId, 1));
61+
}
62+
63+
throw new InvalidArgumentException($message, null, $e);
5864
}
5965
if ($definition->isPublic()) {
6066
continue;

0 commit comments

Comments
 (0)
0