ServiceNotFoundException.php 1.4 KB

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