4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Contracts\Service;
14 use Psr\Container\ContainerExceptionInterface;
15 use Psr\Container\NotFoundExceptionInterface;
17 // Help opcache.preload discover always-needed symbols
18 class_exists(ContainerExceptionInterface::class);
19 class_exists(NotFoundExceptionInterface::class);
22 * A trait to help implement ServiceProviderInterface.
24 * @author Robin Chalas <robin.chalas@gmail.com>
25 * @author Nicolas Grekas <p@tchwork.com>
27 trait ServiceLocatorTrait
30 private $loading = [];
31 private $providedTypes;
34 * @param callable[] $factories
36 public function __construct(array $factories)
38 $this->factories = $factories;
46 public function has(string $id)
48 return isset($this->factories[$id]);
56 public function get(string $id)
58 if (!isset($this->factories[$id])) {
59 throw $this->createNotFoundException($id);
62 if (isset($this->loading[$id])) {
63 $ids = array_values($this->loading);
64 $ids = \array_slice($this->loading, array_search($id, $ids));
67 throw $this->createCircularReferenceException($id, $ids);
70 $this->loading[$id] = $id;
72 return $this->factories[$id]($this);
74 unset($this->loading[$id]);
81 public function getProvidedServices(): array
83 if (null === $this->providedTypes) {
84 $this->providedTypes = [];
86 foreach ($this->factories as $name => $factory) {
87 if (!\is_callable($factory)) {
88 $this->providedTypes[$name] = '?';
90 $type = (new \ReflectionFunction($factory))->getReturnType();
92 $this->providedTypes[$name] = $type ? ($type->allowsNull() ? '?' : '').($type instanceof \ReflectionNamedType ? $type->getName() : $type) : '?';
97 return $this->providedTypes;
100 private function createNotFoundException(string $id): NotFoundExceptionInterface
102 if (!$alternatives = array_keys($this->factories)) {
103 $message = 'is empty...';
105 $last = array_pop($alternatives);
107 $message = sprintf('only knows about the "%s" and "%s" services.', implode('", "', $alternatives), $last);
109 $message = sprintf('only knows about the "%s" service.', $last);
113 if ($this->loading) {
114 $message = sprintf('The service "%s" has a dependency on a non-existent service "%s". This locator %s', end($this->loading), $id, $message);
116 $message = sprintf('Service "%s" not found: the current service locator %s', $id, $message);
119 return new class($message) extends \InvalidArgumentException implements NotFoundExceptionInterface {
123 private function createCircularReferenceException(string $id, array $path): ContainerExceptionInterface
125 return new class(sprintf('Circular reference detected for service "%s", path: "%s".', $id, implode(' -> ', $path))) extends \RuntimeException implements ContainerExceptionInterface {