8000 feature #28449 [DependencyInjection] improved message when alias serv… · symfony/symfony@7c08e43 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7c08e43

Browse files
committed
feature #28449 [DependencyInjection] improved message when alias service is not found (xabbuh)
This PR was merged into the 4.2-dev branch. Discussion ---------- [DependencyInjection] improved message when alias service is not found | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #28413 | License | MIT | Doc PR | 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' ``` Commits ------- 280ecbc improved message when alias service is not found
2 parents e95ea81 + 280ecbc commit 7c08e43

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
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()[0]) {
153+
throw new ServiceNotFoundException($e->getId(), $e->getSourceId(), null, array(substr($e->getId(), 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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\DependencyInjection\ContainerBuilder;
1515
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
16+
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
1617
use Symfony\Component\DependencyInjection\Reference;
1718

1819
/**
@@ -53,8 +54,12 @@ public function process(ContainerBuilder $container)
5354
$seenAliasTargets[$targetId] = true;
5455
try {
5556
$definition = $container->getDefinition($targetId);
56-
} catch (InvalidArgumentException $e) {
57-
throw new InvalidArgumentException(sprintf('Unable to replace alias "%s" with actual definition "%s".', $definitionId, $targetId), null, $e);
57+
} catch (ServiceNotFoundException $e) {
58+
if ('' !== $e->getId() && '@' === $e->getId()[0]) {
59+
throw new ServiceNotFoundException($e->getId(), $e->getSourceId(), null, array(substr($e->getId(), 1)));
60+
}
61+
62+
throw $e;
5863
}
5964
if ($definition->isPublic()) {
6065
continue;

0 commit comments

Comments
 (0)
0