merged branch lyrixx/debug-dic (PR #7861)

This PR was merged into the master branch.

Discussion
----------

[DependencyInjection] Add suggestions on ParameterNotFoundException

| Q             | A
| ------------- | ---
| Bug fix?      | [no]
| New feature?  | [yes]
| BC breaks?    | [no]
| Deprecations? | [no]
| Tests pass?   | [yes]
| Fixed tickets | [-]
| License       | MIT
| Doc PR        | [-]

Commits
-------

e989d8b [DependencyInjection] Added some check before guessing similar parameters
9300157 [DependencyInjection] Made tests more accurate
729db0f [DependencyInjection] Add suggestion on ServiceNotFoundException
f44db48 [DependencyInjection] Add suggestion on ParameterNotFoundException
This commit is contained in:
Fabien Potencier 2013-05-01 06:21:20 +02:00
commit 38f9cf3a7e
6 changed files with 115 additions and 11 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,20 +21,23 @@ class ParameterNotFoundException extends InvalidArgumentException
private $key;
private $sourceId;
private $sourceKey;
private $alternatives;
/**
* Constructor.
*
* @param string $key The requested parameter key
* @param string $sourceId The service id that references the non-existent parameter
* @param string $sourceKey The parameter key that references the non-existent parameter
* @param \Exception $previous The previous exception
* @param string $key The requested parameter key
* @param string $sourceId The service id that references the non-existent parameter
* @param string $sourceKey The parameter key that references the non-existent parameter
* @param \Exception $previous The previous exception
* @param string[] $alternatives Some parameter name alternatives
*/
public function __construct($key, $sourceId = null, $sourceKey = null, \Exception $previous = null)
public function __construct($key, $sourceId = null, $sourceKey = null, \Exception $previous = null, array $alternatives = array())
{
$this->key = $key;
$this->sourceId = $sourceId;
$this->sourceKey = $sourceKey;
$this->alternatives = $alternatives;
parent::__construct('', 0, $previous);
@ -50,6 +53,16 @@ class ParameterNotFoundException extends InvalidArgumentException
} else {
$this->message = sprintf('You have requested a non-existent parameter "%s".', $this->key);
}
if ($this->alternatives) {
if (1 == count($this->alternatives)) {
$this->message .= ' Did you mean this: "';
} elseif (1 < count($this->alternatives)) {
$this->message .= ' Did you mean one of these: "';
}
$this->message .= implode('", "', $this->alternatives);
$this->message .= '" ?';
}
}
public function getKey()

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

@ -93,7 +93,19 @@ class ParameterBag implements ParameterBagInterface
$name = strtolower($name);
if (!array_key_exists($name, $this->parameters)) {
throw new ParameterNotFoundException($name);
if (!$name) {
throw new ParameterNotFoundException($name);
}
$alternatives = array();
foreach (array_keys($this->parameters) as $key) {
$lev = levenshtein($name, $key);
if ($lev <= strlen($name) / 3 || false !== strpos($key, $name)) {
$alternatives[] = $key;
}
}
throw new ParameterNotFoundException($name, null, null, null, $alternatives);
}
return $this->parameters[$name];

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()
{

View File

@ -77,10 +77,43 @@ class ParameterBagTest extends \PHPUnit_Framework_TestCase
try {
$bag->get('baba');
$this->fail('->get() throws an \InvalidArgumentException if the key does not exist');
$this->fail('->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
} catch (\Exception $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e, '->get() throws an \InvalidArgumentException if the key does not exist');
$this->assertEquals('You have requested a non-existent parameter "baba".', $e->getMessage(), '->get() throws an \InvalidArgumentException if the key does not exist');
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException', $e, '->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
$this->assertEquals('You have requested a non-existent parameter "baba".', $e->getMessage(), '->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
}
}
public function testGetThrowParameterNotFoundException()
{
$bag = new ParameterBag(array(
'foo' => 'foo',
'bar' => 'bar',
'baz' => 'baz',
));
try {
$bag->get('foo1');
$this->fail('->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
} catch (\Exception $e) {
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException', $e, '->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
$this->assertEquals('You have requested a non-existent parameter "foo1". Did you mean this: "foo" ?', $e->getMessage(), '->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException with some advices');
}
try {
$bag->get('bag');
$this->fail('->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
} catch (\Exception $e) {
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException', $e, '->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
$this->assertEquals('You have requested a non-existent parameter "bag". Did you mean one of these: "bar", "baz" ?', $e->getMessage(), '->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException with some advices');
}
try {
$bag->get('');
$this->fail('->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
} catch (\Exception $e) {
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException', $e, '->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
$this->assertEquals('You have requested a non-existent parameter "".', $e->getMessage(), '->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException with some advices');
}
}