ServiceLocatorTrait.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. // Help opcache.preload discover always-needed symbols
  14. class_exists(ContainerExceptionInterface::class);
  15. class_exists(NotFoundExceptionInterface::class);
  16. /**
  17. * A trait to help implement ServiceProviderInterface.
  18. *
  19. * @author Robin Chalas <robin.chalas@gmail.com>
  20. * @author Nicolas Grekas <p@tchwork.com>
  21. */
  22. trait ServiceLocatorTrait
  23. {
  24. private array $factories;
  25. private array $loading = [];
  26. private array $providedTypes;
  27. /**
  28. * @param callable[] $factories
  29. */
  30. public function __construct(array $factories)
  31. {
  32. $this->factories = $factories;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function has(string $id): bool
  38. {
  39. return isset($this->factories[$id]);
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function get(string $id): mixed
  45. {
  46. if (!isset($this->factories[$id])) {
  47. throw $this->createNotFoundException($id);
  48. }
  49. if (isset($this->loading[$id])) {
  50. $ids = array_values($this->loading);
  51. $ids = \array_slice($this->loading, array_search($id, $ids));
  52. $ids[] = $id;
  53. throw $this->createCircularReferenceException($id, $ids);
  54. }
  55. $this->loading[$id] = $id;
  56. try {
  57. return $this->factories[$id]($this);
  58. } finally {
  59. unset($this->loading[$id]);
  60. }
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function getProvidedServices(): array
  66. {
  67. if (!isset($this->providedTypes)) {
  68. $this->providedTypes = [];
  69. foreach ($this->factories as $name => $factory) {
  70. if (!\is_callable($factory)) {
  71. $this->providedTypes[$name] = '?';
  72. } else {
  73. $type = (new \ReflectionFunction($factory))->getReturnType();
  74. $this->providedTypes[$name] = $type ? ($type->allowsNull() ? '?' : '').($type instanceof \ReflectionNamedType ? $type->getName() : $type) : '?';
  75. }
  76. }
  77. }
  78. return $this->providedTypes;
  79. }
  80. private function createNotFoundException(string $id): NotFoundExceptionInterface
  81. {
  82. if (!$alternatives = array_keys($this->factories)) {
  83. $message = 'is empty...';
  84. } else {
  85. $last = array_pop($alternatives);
  86. if ($alternatives) {
  87. $message = sprintf('only knows about the "%s" and "%s" services.', implode('", "', $alternatives), $last);
  88. } else {
  89. $message = sprintf('only knows about the "%s" service.', $last);
  90. }
  91. }
  92. if ($this->loading) {
  93. $message = sprintf('The service "%s" has a dependency on a non-existent service "%s". This locator %s', end($this->loading), $id, $message);
  94. } else {
  95. $message = sprintf('Service "%s" not found: the current service locator %s', $id, $message);
  96. }
  97. return new class($message) extends \InvalidArgumentException implements NotFoundExceptionInterface {
  98. };
  99. }
  100. private function createCircularReferenceException(string $id, array $path): ContainerExceptionInterface
  101. {
  102. return new class(sprintf('Circular reference detected for service "%s", path: "%s".', $id, implode(' -> ', $path))) extends \RuntimeException implements ContainerExceptionInterface {
  103. };
  104. }
  105. }