ParameterNotFoundException.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. use Psr\Container\NotFoundExceptionInterface;
  12. /**
  13. * This exception is thrown when a non-existent parameter is used.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class ParameterNotFoundException extends InvalidArgumentException implements NotFoundExceptionInterface
  18. {
  19. private $key;
  20. private $sourceId;
  21. private $sourceKey;
  22. private $alternatives;
  23. private $nonNestedAlternative;
  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 \Throwable $previous The previous exception
  29. * @param string[] $alternatives Some parameter name alternatives
  30. * @param string|null $nonNestedAlternative The alternative parameter name when the user expected dot notation for nested parameters
  31. */
  32. public function __construct(string $key, string $sourceId = null, string $sourceKey = null, \Throwable $previous = null, array $alternatives = [], string $nonNestedAlternative = null)
  33. {
  34. $this->key = $key;
  35. $this->sourceId = $sourceId;
  36. $this->sourceKey = $sourceKey;
  37. $this->alternatives = $alternatives;
  38. $this->nonNestedAlternative = $nonNestedAlternative;
  39. parent::__construct('', 0, $previous);
  40. $this->updateRepr();
  41. }
  42. public function updateRepr()
  43. {
  44. if (null !== $this->sourceId) {
  45. $this->message = sprintf('The service "%s" has a dependency on a non-existent parameter "%s".', $this->sourceId, $this->key);
  46. } elseif (null !== $this->sourceKey) {
  47. $this->message = sprintf('The parameter "%s" has a dependency on a non-existent parameter "%s".', $this->sourceKey, $this->key);
  48. } else {
  49. $this->message = sprintf('You have requested a non-existent parameter "%s".', $this->key);
  50. }
  51. if ($this->alternatives) {
  52. if (1 == \count($this->alternatives)) {
  53. $this->message .= ' Did you mean this: "';
  54. } else {
  55. $this->message .= ' Did you mean one of these: "';
  56. }
  57. $this->message .= implode('", "', $this->alternatives).'"?';
  58. } elseif (null !== $this->nonNestedAlternative) {
  59. $this->message .= ' You cannot access nested array items, do you want to inject "'.$this->nonNestedAlternative.'" instead?';
  60. }
  61. }
  62. public function getKey()
  63. {
  64. return $this->key;
  65. }
  66. public function getSourceId()
  67. {
  68. return $this->sourceId;
  69. }
  70. public function getSourceKey()
  71. {
  72. return $this->sourceKey;
  73. }
  74. public function setSourceId($sourceId)
  75. {
  76. $this->sourceId = $sourceId;
  77. $this->updateRepr();
  78. }
  79. public function setSourceKey($sourceKey)
  80. {
  81. $this->sourceKey = $sourceKey;
  82. $this->updateRepr();
  83. }
  84. }