0
0

ServiceLocatorTrait.php 3.4 KB

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