ServiceNotFoundException.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. public function __construct($id, $sourceId = null, \Exception $previous = null, array $alternatives = array())
  22. {
  23. if (null === $sourceId) {
  24. $msg = sprintf('You have requested a non-existent service "%s".', $id);
  25. } else {
  26. $msg = sprintf('The service "%s" has a dependency on a non-existent service "%s".', $sourceId, $id);
  27. }
  28. if ($alternatives) {
  29. if (1 == count($alternatives)) {
  30. $msg .= ' Did you mean this: "';
  31. } else {
  32. $msg .= ' Did you mean one of these: "';
  33. }
  34. $msg .= implode('", "', $alternatives).'"?';
  35. }
  36. parent::__construct($msg, 0, $previous);
  37. $this->id = $id;
  38. $this->sourceId = $sourceId;
  39. }
  40. public function getId()
  41. {
  42. return $this->id;
  43. }
  44. public function getSourceId()
  45. {
  46. return $this->sourceId;
  47. }
  48. }