Debian Codegraph @

vendor/symfony/service-contracts/ServiceLocatorTrait.php

raw
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Contracts\Service;
13
14 use Psr\Container\ContainerExceptionInterface;
15 use Psr\Container\NotFoundExceptionInterface;
16
17 // Help opcache.preload discover always-needed symbols
18 class_exists(ContainerExceptionInterface::class);
19 class_exists(NotFoundExceptionInterface::class);
20
21 /**
22 * A trait to help implement ServiceProviderInterface.
23 *
24 * @author Robin Chalas <[email protected]>
25 * @author Nicolas Grekas <[email protected]>
26 */
27 trait ServiceLocatorTrait
28 {
29 private array $loading = [];
30 private array $providedTypes;
31
32 /**
33 * @param array<string, callable> $factories
34 */
35 public function __construct(
36 private array $factories,
37 ) {
38 }
39
40 public function has(string $id): bool
41 {
42 return isset($this->factories[$id]);
43 }
44
45 public function get(string $id): mixed
46 {
47 if (!isset($this->factories[$id])) {
48 throw $this->createNotFoundException($id);
49 }
50
51 if (isset($this->loading[$id])) {
52 $ids = array_values($this->loading);
53 $ids = \array_slice($this->loading, array_search($id, $ids));
54 $ids[] = $id;
55
56 throw $this->createCircularReferenceException($id, $ids);
57 }
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 public function getProvidedServices(): array
68 {
69 if (!isset($this->providedTypes)) {
70 $this->providedTypes = [];
71
72 foreach ($this->factories as $name => $factory) {
73 if (!\is_callable($factory)) {
74 $this->providedTypes[$name] = '?';
75 } else {
76 $type = (new \ReflectionFunction($factory))->getReturnType();
77
78 $this->providedTypes[$name] = $type ? ($type->allowsNull() ? '?' : '').($type instanceof \ReflectionNamedType ? $type->getName() : $type) : '?';
79 }
80 }
81 }
82
83 return $this->providedTypes;
84 }
85
86 private function createNotFoundException(string $id): NotFoundExceptionInterface
87 {
88 if (!$alternatives = array_keys($this->factories)) {
89 $message = 'is empty...';
90 } else {
91 $last = array_pop($alternatives);
92 if ($alternatives) {
93 $message = \sprintf('only knows about the "%s" and "%s" services.', implode('", "', $alternatives), $last);
94 } else {
95 $message = \sprintf('only knows about the "%s" service.', $last);
96 }
97 }
98
99 if ($this->loading) {
100 $message = \sprintf('The service "%s" has a dependency on a non-existent service "%s". This locator %s', end($this->loading), $id, $message);
101 } else {
102 $message = \sprintf('Service "%s" not found: the current service locator %s', $id, $message);
103 }
104
105 return new class($message) extends \InvalidArgumentException implements NotFoundExceptionInterface {
106 };
107 }
108
109 private function createCircularReferenceException(string $id, array $path): ContainerExceptionInterface
110 {
111 return new class(\sprintf('Circular reference detected for service "%s", path: "%s".', $id, implode(' -> ', $path))) extends \RuntimeException implements ContainerExceptionInterface {
112 };
113 }
114 }