ParameterNotFoundException.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\Exception;
  11. /**
  12. * This exception is thrown when a non-existent parameter is used.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class ParameterNotFoundException extends InvalidArgumentException
  17. {
  18. private $key;
  19. private $sourceId;
  20. private $sourceKey;
  21. private $alternatives;
  22. private $nonNestedAlternative;
  23. /**
  24. * @param string $key The requested parameter key
  25. * @param string $sourceId The service id that references the non-existent parameter
  26. * @param string $sourceKey The parameter key that references the non-existent parameter
  27. * @param \Exception $previous The previous exception
  28. * @param string[] $alternatives Some parameter name alternatives
  29. * @param string|null $nonNestedAlternative The alternative parameter name when the user expected dot notation for nested parameters
  30. */
  31. public function __construct($key, $sourceId = null, $sourceKey = null, \Exception $previous = null, array $alternatives = array(), $nonNestedAlternative = null)
  32. {
  33. $this->key = $key;
  34. $this->sourceId = $sourceId;
  35. $this->sourceKey = $sourceKey;
  36. $this->alternatives = $alternatives;
  37. $this->nonNestedAlternative = $nonNestedAlternative;
  38. parent::__construct('', 0, $previous);
  39. $this->updateRepr();
  40. }
  41. public function updateRepr()
  42. {
  43. if (null !== $this->sourceId) {
  44. $this->message = sprintf('The service "%s" has a dependency on a non-existent parameter "%s".', $this->sourceId, $this->key);
  45. } elseif (null !== $this->sourceKey) {
  46. $this->message = sprintf('The parameter "%s" has a dependency on a non-existent parameter "%s".', $this->sourceKey, $this->key);
  47. } else {
  48. $this->message = sprintf('You have requested a non-existent parameter "%s".', $this->key);
  49. }
  50. if ($this->alternatives) {
  51. if (1 == count($this->alternatives)) {
  52. $this->message .= ' Did you mean this: "';
  53. } else {
  54. $this->message .= ' Did you mean one of these: "';
  55. }
  56. $this->message .= implode('", "', $this->alternatives).'"?';
  57. } elseif (null !== $this->nonNestedAlternative) {
  58. $this->message .= ' You cannot access nested array items, do you want to inject "'.$this->nonNestedAlternative.'" instead?';
  59. }
  60. }
  61. public function getKey()
  62. {
  63. return $this->key;
  64. }
  65. public function getSourceId()
  66. {
  67. return $this->sourceId;
  68. }
  69. public function getSourceKey()
  70. {
  71. return $this->sourceKey;
  72. }
  73. public function setSourceId($sourceId)
  74. {
  75. $this->sourceId = $sourceId;
  76. $this->updateRepr();
  77. }
  78. public function setSourceKey($sourceKey)
  79. {
  80. $this->sourceKey = $sourceKey;
  81. $this->updateRepr();
  82. }
  83. }