ServiceLocatorTrait.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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\Contracts\Service;
  11. use Psr\Container\ContainerExceptionInterface;
  12. use Psr\Container\NotFoundExceptionInterface;
  13. /**
  14. * A trait to help implement PSR-11 service locators.
  15. *
  16. * @author Robin Chalas <robin.chalas@gmail.com>
  17. * @author Nicolas Grekas <p@tchwork.com>
  18. */
  19. trait ServiceLocatorTrait
  20. {
  21. private $factories;
  22. private $loading = array();
  23. /**
  24. * @param callable[] $factories
  25. */
  26. public function __construct(array $factories)
  27. {
  28. $this->factories = $factories;
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function has($id)
  34. {
  35. return isset($this->factories[$id]);
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function get($id)
  41. {
  42. if (!isset($this->factories[$id])) {
  43. throw $this->createNotFoundException($id);
  44. }
  45. if (isset($this->loading[$id])) {
  46. $ids = array_values($this->loading);
  47. $ids = \array_slice($this->loading, array_search($id, $ids));
  48. $ids[] = $id;
  49. throw $this->createCircularReferenceException($id, $ids);
  50. }
  51. $this->loading[$id] = $id;
  52. try {
  53. return $this->factories[$id]($this);
  54. } finally {
  55. unset($this->loading[$id]);
  56. }
  57. }
  58. private function createNotFoundException(string $id): NotFoundExceptionInterface
  59. {
  60. if (!$alternatives = array_keys($this->factories)) {
  61. $message = 'is empty...';
  62. } else {
  63. $last = array_pop($alternatives);
  64. if ($alternatives) {
  65. $message = sprintf('only knows about the "%s" and "%s" services.', implode('", "', $alternatives), $last);
  66. } else {
  67. $message = sprintf('only knows about the "%s" service.', $last);
  68. }
  69. }
  70. if ($this->loading) {
  71. $message = sprintf('The service "%s" has a dependency on a non-existent service "%s". This locator %s', end($this->loading), $id, $message);
  72. } else {
  73. $message = sprintf('Service "%s" not found: the current service locator %s', $id, $message);
  74. }
  75. return new class($message) extends \InvalidArgumentException implements NotFoundExceptionInterface {
  76. };
  77. }
  78. private function createCircularReferenceException(string $id, array $path): ContainerExceptionInterface
  79. {
  80. return new class(sprintf('Circular reference detected for service "%s", path: "%s".', $id, implode(' -> ', $path))) extends \RuntimeException implements ContainerExceptionInterface {
  81. };
  82. }
  83. }