feature #19892 [DI] Add corresponding service id in some exception messages (nicolas-grekas)

This PR was merged into the 3.2-dev branch.

Discussion
----------

[DI] Add corresponding service id in some exception messages

| Q             | A
| ------------- | ---
| Branch?       | master
| New feature?  | yes
| Tests pass?   | yes
| License       | MIT

This already helped me figure out where was my mistake :)

Commits
-------

d739f8d [DI] Add corresponding service id in some exception messages
This commit is contained in:
Fabien Potencier 2016-09-14 12:02:36 -07:00
commit c69b89a26b
2 changed files with 16 additions and 2 deletions

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\ExceptionInterface;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
/**
@ -95,9 +96,22 @@ class ResolveDefinitionTemplatesPass implements CompilerPassInterface
* @throws \RuntimeException When the definition is invalid
*/
private function resolveDefinition(ContainerBuilder $container, DefinitionDecorator $definition)
{
try {
return $this->doResolveDefinition($container, $definition);
} catch (ExceptionInterface $e) {
$r = new \ReflectionProperty($e, 'message');
$r->setAccessible(true);
$r->setValue($e, sprintf('Service "%s": %s', $this->currentId, $e->getMessage()));
throw $e;
}
}
private function doResolveDefinition(ContainerBuilder $container, DefinitionDecorator $definition)
{
if (!$container->has($parent = $definition->getParent())) {
throw new RuntimeException(sprintf('The parent definition "%s" defined for definition "%s" does not exist.', $parent, $this->currentId));
throw new RuntimeException(sprintf('Parent definition "%s" does not exist.', $parent));
}
$parentDef = $container->findDefinition($parent);

View File

@ -93,7 +93,7 @@ class Definition
public function setDecoratedService($id, $renamedId = null, $priority = 0)
{
if ($renamedId && $id == $renamedId) {
throw new \InvalidArgumentException(sprintf('The decorated service inner name for "%s" must be different than the service name itself.', $id));
throw new InvalidArgumentException(sprintf('The decorated service inner name for "%s" must be different than the service name itself.', $id));
}
if (null === $id) {