You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.7 KiB

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