[DependencyInjection] Add suggestion on ParameterNotFoundException
This commit is contained in:
parent
4dcee0a528
commit
f44db48ebe
@ -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()
|
||||
|
@ -93,7 +93,15 @@ class ParameterBag implements ParameterBagInterface
|
||||
$name = strtolower($name);
|
||||
|
||||
if (!array_key_exists($name, $this->parameters)) {
|
||||
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];
|
||||
|
@ -84,6 +84,31 @@ class ParameterBagTest extends \PHPUnit_Framework_TestCase
|
||||
}
|
||||
}
|
||||
|
||||
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 \InvalidArgumentException 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 \InvalidArgumentException with some advices');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::has
|
||||
*/
|
||||
|
Reference in New Issue
Block a user