ServiceLocatorTrait.php 3.6 KB

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