4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Exception;
15 * This exception is thrown when a non-existent parameter is used.
17 * @author Fabien Potencier <fabien@symfony.com>
19 class ParameterNotFoundException extends InvalidArgumentException
24 private $alternatives;
25 private $nonNestedAlternative;
28 * @param string $key The requested parameter key
29 * @param string $sourceId The service id that references the non-existent parameter
30 * @param string $sourceKey The parameter key that references the non-existent parameter
31 * @param \Exception $previous The previous exception
32 * @param string[] $alternatives Some parameter name alternatives
33 * @param string|null $nonNestedAlternative The alternative parameter name when the user expected dot notation for nested parameters
35 public function __construct($key, $sourceId = null, $sourceKey = null, \Exception $previous = null, array $alternatives = array(), $nonNestedAlternative = null)
38 $this->sourceId = $sourceId;
39 $this->sourceKey = $sourceKey;
40 $this->alternatives = $alternatives;
41 $this->nonNestedAlternative = $nonNestedAlternative;
43 parent::__construct('', 0, $previous);
48 public function updateRepr()
50 if (null !== $this->sourceId) {
51 $this->message = sprintf('The service "%s" has a dependency on a non-existent parameter "%s".', $this->sourceId, $this->key);
52 } elseif (null !== $this->sourceKey) {
53 $this->message = sprintf('The parameter "%s" has a dependency on a non-existent parameter "%s".', $this->sourceKey, $this->key);
55 $this->message = sprintf('You have requested a non-existent parameter "%s".', $this->key);
58 if ($this->alternatives) {
59 if (1 == \count($this->alternatives)) {
60 $this->message .= ' Did you mean this: "';
62 $this->message .= ' Did you mean one of these: "';
64 $this->message .= implode('", "', $this->alternatives).'"?';
65 } elseif (null !== $this->nonNestedAlternative) {
66 $this->message .= ' You cannot access nested array items, do you want to inject "'.$this->nonNestedAlternative.'" instead?';
70 public function getKey()
75 public function getSourceId()
77 return $this->sourceId;
80 public function getSourceKey()
82 return $this->sourceKey;
85 public function setSourceId($sourceId)
87 $this->sourceId = $sourceId;
92 public function setSourceKey($sourceKey)
94 $this->sourceKey = $sourceKey;