ServiceLocatorTrait.php 3.4 KB

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