ServiceNotFoundException.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 service is requested.
  14. *
  15. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  16. */
  17. class ServiceNotFoundException extends InvalidArgumentException implements NotFoundExceptionInterface
  18. {
  19. private $id;
  20. private $sourceId;
  21. private $alternatives;
  22. public function __construct(string $id, string $sourceId = null, \Throwable $previous = null, array $alternatives = [], string $msg = null)
  23. {
  24. if (null !== $msg) {
  25. // no-op
  26. } elseif (null === $sourceId) {
  27. $msg = sprintf('You have requested a non-existent service "%s".', $id);
  28. } else {
  29. $msg = sprintf('The service "%s" has a dependency on a non-existent service "%s".', $sourceId, $id);
  30. }
  31. if ($alternatives) {
  32. if (1 == \count($alternatives)) {
  33. $msg .= ' Did you mean this: "';
  34. } else {
  35. $msg .= ' Did you mean one of these: "';
  36. }
  37. $msg .= implode('", "', $alternatives).'"?';
  38. }
  39. parent::__construct($msg, 0, $previous);
  40. $this->id = $id;
  41. $this->sourceId = $sourceId;
  42. $this->alternatives = $alternatives;
  43. }
  44. public function getId()
  45. {
  46. return $this->id;
  47. }
  48. public function getSourceId()
  49. {
  50. return $this->sourceId;
  51. }
  52. public function getAlternatives()
  53. {
  54. return $this->alternatives;
  55. }
  56. }