ParameterNotFoundException.php 2.7 KB

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