[DependencyInjection] Add suggestion on ServiceNotFoundException

This commit is contained in:
Grégoire Pineau 2013-04-30 19:55:25 +02:00
parent f44db48ebe
commit 729db0fde7
3 changed files with 48 additions and 2 deletions

View File

@ -285,7 +285,19 @@ class Container implements IntrospectableContainerInterface
}
if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
throw new ServiceNotFoundException($id);
if (!$id) {
throw new ServiceNotFoundException($id);
}
$alternatives = array();
foreach (array_keys($this->services) as $key) {
$lev = levenshtein($id, $key);
if ($lev <= strlen($id) / 3 || false !== strpos($key, $id)) {
$alternatives[] = $key;
}
}
throw new ServiceNotFoundException($id, null, null, $alternatives);
}
}

View File

@ -21,7 +21,7 @@ class ServiceNotFoundException extends InvalidArgumentException
private $id;
private $sourceId;
public function __construct($id, $sourceId = null, \Exception $previous = null)
public function __construct($id, $sourceId = null, \Exception $previous = null, array $alternatives = array())
{
if (null === $sourceId) {
$msg = sprintf('You have requested a non-existent service "%s".', $id);
@ -29,6 +29,16 @@ class ServiceNotFoundException extends InvalidArgumentException
$msg = sprintf('The service "%s" has a dependency on a non-existent service "%s".', $sourceId, $id);
}
if ($alternatives) {
if (1 == count($alternatives)) {
$msg .= ' Did you mean this: "';
} elseif (1 < count($alternatives)) {
$msg .= ' Did you mean one of these: "';
}
$msg .= implode('", "', $alternatives);
$msg .= '" ?';
}
parent::__construct($msg, 0, $previous);
$this->id = $id;

View File

@ -167,6 +167,30 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
$this->assertNull($sc->get('', ContainerInterface::NULL_ON_INVALID_REFERENCE));
}
public function testGetThrowServiceNotFoundException()
{
$sc = new ProjectServiceContainer();
$sc->set('foo', $foo = new \stdClass());
$sc->set('bar', $foo = new \stdClass());
$sc->set('baz', $foo = new \stdClass());
try {
$sc->get('foo1');
$this->fail('->get() throws an Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException if the key does not exist');
} catch (\Exception $e) {
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException', $e, '->get() throws an Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException if the key does not exist');
$this->assertEquals('You have requested a non-existent service "foo1". Did you mean this: "foo" ?', $e->getMessage(), '->get() throws an Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException with some advices');
}
try {
$sc->get('bag');
$this->fail('->get() throws an Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException if the key does not exist');
} catch (\Exception $e) {
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException', $e, '->get() throws an Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException if the key does not exist');
$this->assertEquals('You have requested a non-existent service "bag". Did you mean one of these: "bar", "baz" ?', $e->getMessage(), '->get() throws an Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException with some advices');
}
}
public function testGetCircularReference()
{