Skip to content

Commit

Permalink
[DI] Display previous error messages when throwing unused bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed May 9, 2018
1 parent 40bcd77 commit f2231b5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
Expand Up @@ -118,8 +118,12 @@ protected function getConstructor(Definition $definition, $required)

$class = $definition->getClass();

if (!$r = $this->container->getReflectionClass($class)) {
throw new RuntimeException(sprintf('Invalid service "%s": class "%s" does not exist.', $this->currentId, $class));
try {
if (!$r = $this->container->getReflectionClass($class)) {
throw new RuntimeException(sprintf('Invalid service "%s": class "%s" does not exist.', $this->currentId, $class));
}
} catch (\ReflectionException $e) {
throw new RuntimeException(sprintf('Invalid service "%s": %s.', $this->currentId, lcfirst(rtrim($e->getMessage(), '.'))));
}
if (!$r = $r->getConstructor()) {
if ($required) {
Expand Down
Expand Up @@ -27,6 +27,7 @@ class ResolveBindingsPass extends AbstractRecursivePass
{
private $usedBindings = array();
private $unusedBindings = array();
private $errorMessages = array();

/**
* {@inheritdoc}
Expand All @@ -37,11 +38,19 @@ public function process(ContainerBuilder $container)
parent::process($container);

foreach ($this->unusedBindings as list($key, $serviceId)) {
throw new InvalidArgumentException(sprintf('Unused binding "%s" in service "%s".', $key, $serviceId));
$message = sprintf('Unused binding "%s" in service "%s".', $key, $serviceId);
if ($this->errorMessages) {
$message .= sprintf("\nCould be related to%s:", 1 < \count($this->errorMessages) ? ' one of' : '');
}
foreach ($this->errorMessages as $m) {
$message .= "\n - ".$m;
}
throw new InvalidArgumentException($message);
}
} finally {
$this->usedBindings = array();
$this->unusedBindings = array();
$this->errorMessages = array();
}
}

Expand Down Expand Up @@ -94,6 +103,7 @@ protected function processValue($value, $isRoot = false)
$calls[] = array($constructor, $value->getArguments());
}
} catch (RuntimeException $e) {
$this->errorMessages[] = $e->getMessage();
$this->container->getDefinition($this->currentId)->addError($e->getMessage());

return parent::processValue($value, $isRoot);
Expand Down
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy;
use Symfony\Component\DependencyInjection\Tests\Fixtures\ParentNotExists;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
use Symfony\Component\DependencyInjection\TypedReference;

Expand Down Expand Up @@ -61,6 +62,21 @@ public function testUnusedBinding()
$pass->process($container);
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessageRegexp Unused binding "$quz" in service [\s\S]+ Invalid service ".*\\ParentNotExists": class NotExists not found\.
*/
public function testMissingParent()
{
$container = new ContainerBuilder();

$definition = $container->register(ParentNotExists::class, ParentNotExists::class);
$definition->setBindings(array('$quz' => '123'));

$pass = new ResolveBindingsPass();
$pass->process($container);
}

public function testTypedReferenceSupport()
{
$container = new ContainerBuilder();
Expand Down

0 comments on commit f2231b5

Please sign in to comment.