ServiceLocatorTrait.php 3.7 KB

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