ParameterNotFoundException.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. /**
  23. * @param string $key The requested parameter key
  24. * @param string $sourceId The service id that references the non-existent parameter
  25. * @param string $sourceKey The parameter key that references the non-existent parameter
  26. * @param \Exception $previous The previous exception
  27. * @param string[] $alternatives Some parameter name alternatives
  28. */
  29. public function __construct($key, $sourceId = null, $sourceKey = null, \Exception $previous = null, array $alternatives = array())
  30. {
  31. $this->key = $key;
  32. $this->sourceId = $sourceId;
  33. $this->sourceKey = $sourceKey;
  34. $this->alternatives = $alternatives;
  35. parent::__construct('', 0, $previous);
  36. $this->updateRepr();
  37. }
  38. public function updateRepr()
  39. {
  40. if (null !== $this->sourceId) {
  41. $this->message = sprintf('The service "%s" has a dependency on a non-existent parameter "%s".', $this->sourceId, $this->key);
  42. } elseif (null !== $this->sourceKey) {
  43. $this->message = sprintf('The parameter "%s" has a dependency on a non-existent parameter "%s".', $this->sourceKey, $this->key);
  44. } else {
  45. $this->message = sprintf('You have requested a non-existent parameter "%s".', $this->key);
  46. }
  47. if ($this->alternatives) {
  48. if (1 == count($this->alternatives)) {
  49. $this->message .= ' Did you mean this: "';
  50. } else {
  51. $this->message .= ' Did you mean one of these: "';
  52. }
  53. $this->message .= implode('", "', $this->alternatives).'"?';
  54. }
  55. }
  56. public function getKey()
  57. {
  58. return $this->key;
  59. }
  60. public function getSourceId()
  61. {
  62. return $this->sourceId;
  63. }
  64. public function getSourceKey()
  65. {
  66. return $this->sourceKey;
  67. }
  68. public function setSourceId($sourceId)
  69. {
  70. $this->sourceId = $sourceId;
  71. $this->updateRepr();
  72. }
  73. public function setSourceKey($sourceKey)
  74. {
  75. $this->sourceKey = $sourceKey;
  76. $this->updateRepr();
  77. }
  78. }