8000 Added notion and help when trying to access nested parameters · symfony/symfony@1986299 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1986299

Browse files
committed
Added notion and help when trying to access nested parameters
A common problem under beginners is to think that the dot notation is used to access nested arrays saved in parameters. Adding a little extra detail to the exception message and a working alternative should help pointing these people in the right direction before spending time debugging this.
1 parent e4009dc commit 1986299

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

src/Symfony/Component/DependencyInjection/Exception/ParameterNotFoundException.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class ParameterNotFoundException extends InvalidArgumentException
2222
private $sourceId;
2323
private $sourceKey;
2424
private $alternatives;
25+
private $nonNestedAlternative;
2526

2627
/**
2728
* @param string $key The requested parameter key
@@ -30,12 +31,13 @@ class ParameterNotFoundException extends InvalidArgumentException
3031
* @param \Exception $previous The previous exception
3132
* @param string[] $alternatives Some parameter name alternatives
3233
*/
33-
public function __construct($key, $sourceId = null, $sourceKey = null, \Exception $previous = null, array $alternatives = array())
34+
public function __construct($key, $sourceId = null, $sourceKey = null, \Exception $previous = null, array $alternatives = array(), $nonNestedAlternative = null)
3435
{
3536
$this->key = $key;
3637
$this->sourceId = $sourceId;
3738
$this->sourceKey = $sourceKey;
3839
$this->alternatives = $alternatives;
40+
$this->nonNestedAlternative = $nonNestedAlternative;
3941

4042
parent::__construct('', 0, $previous);
4143

@@ -59,6 +61,8 @@ public function updateRepr()
5961
$this->message .= ' Did you mean one of these: "';
6062
}
6163
$this->message .= implode('", "', $this->alternatives).'"?';
64+
} elseif (null !== $this->nonNestedAlternative) {
65+
$this->message .= ' You cannot access nested array items, do you want to inject "'.$this->nonNestedAlternative.'" instead?';
6266
}
6367
}
6468

src/Symfony/Component/DependencyInjection/ParameterBag/ParameterBag.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,23 @@ public function get($name)
8181
}
8282
}
8383

84-
throw new ParameterNotFoundException($name, null, null, null, $alternatives);
84+
$nonNestedAlternative = null;
85+
if (!count($alternatives) && false !== strpos($name, '.')) {
86+
$namePartsLength = array_map('strlen', explode('.', $name));
87+
$key = substr($name, 0, -1 * (1 + array_pop($namePartsLength)));
88+
while (count($namePartsLength)) {
89+
if ($this->has($key)) {
90+
if (is_array($this->get($key))) {
91+
$nonNestedAlternative = $key;
92+
}
93+
break;
94+
}
95+
96+
$key = substr($key, 0, -1 * (1 + array_pop($namePartsLength)));
97+
}
98+
}
99+
100+
throw new ParameterNotFoundException($name, null, null, null, $alternatives, $nonNestedAlternative);
85101
}
86102

87103
return $this->parameters[$name];

src/Symfony/Component/DependencyInjection/Tests/ParameterBag/ParameterBagTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public function testGetThrowParameterNotFoundException($parameterKey, $exception
8080
'foo' => 'foo',
8181
'bar' => 'bar',
8282
'baz' => 'baz',
83+
'fiz' => array('bar' => array('boo' => 12)),
8384
));
8485

8586
$this->setExpectedException('Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException', $exceptionMessage);
@@ -93,6 +94,8 @@ public function provideGetThrowParameterNotFoundExceptionData()
9394
array('foo1', 'You have requested a non-existent parameter "foo1". Did you mean this: "foo"?'),
9495
array('bag', 'You have requested a non-existent parameter "bag". Did you mean one of these: "bar", "baz"?'),
9596
array('', 'You have requested a non-existent parameter "".'),
97+
98+
array('fiz.bar.boo', 'You have requested a non-existent parameter "fiz.bar.boo". You cannot access nested array items, do you want to inject "fiz" instead?'),
9699
);
97100
}
98101

0 commit comments

Comments
 (0)
0