diff options
author | polo <ordipolo@gmx.fr> | 2024-08-13 23:45:21 +0200 |
---|---|---|
committer | polo <ordipolo@gmx.fr> | 2024-08-13 23:45:21 +0200 |
commit | bf6655a534a6775d30cafa67bd801276bda1d98d (patch) | |
tree | c6381e3f6c81c33eab72508f410b165ba05f7e9c /vendor/symfony/service-contracts/ServiceLocatorTrait.php | |
parent | 94d67a4b51f8e62e7d518cce26a526ae1ec48278 (diff) | |
download | AppliGestionPHP-bf6655a534a6775d30cafa67bd801276bda1d98d.zip |
VERSION 0.2 doctrine ORM et entités
Diffstat (limited to 'vendor/symfony/service-contracts/ServiceLocatorTrait.php')
-rw-r--r-- | vendor/symfony/service-contracts/ServiceLocatorTrait.php | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/vendor/symfony/service-contracts/ServiceLocatorTrait.php b/vendor/symfony/service-contracts/ServiceLocatorTrait.php new file mode 100644 index 0000000..b62ec3e --- /dev/null +++ b/vendor/symfony/service-contracts/ServiceLocatorTrait.php | |||
@@ -0,0 +1,115 @@ | |||
1 | <?php | ||
2 | |||
3 | /* | ||
4 | * This file is part of the Symfony package. | ||
5 | * | ||
6 | * (c) Fabien Potencier <fabien@symfony.com> | ||
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 <robin.chalas@gmail.com> | ||
25 | * @author Nicolas Grekas <p@tchwork.com> | ||
26 | */ | ||
27 | trait ServiceLocatorTrait | ||
28 | { | ||
29 | private array $factories; | ||
30 | private array $loading = []; | ||
31 | private array $providedTypes; | ||
32 | |||
33 | /** | ||
34 | * @param array<string, callable> $factories | ||
35 | */ | ||
36 | public function __construct(array $factories) | ||
37 | { | ||
38 | $this->factories = $factories; | ||
39 | } | ||
40 | |||
41 | public function has(string $id): bool | ||
42 | { | ||
43 | return isset($this->factories[$id]); | ||
44 | } | ||
45 | |||
46 | public function get(string $id): mixed | ||
47 | { | ||
48 | if (!isset($this->factories[$id])) { | ||
49 | throw $this->createNotFoundException($id); | ||
50 | } | ||
51 | |||
52 | if (isset($this->loading[$id])) { | ||
53 | $ids = array_values($this->loading); | ||
54 | $ids = \array_slice($this->loading, array_search($id, $ids)); | ||
55 | $ids[] = $id; | ||
56 | |||
57 | throw $this->createCircularReferenceException($id, $ids); | ||
58 | } | ||
59 | |||
60 | $this->loading[$id] = $id; | ||
61 | try { | ||
62 | return $this->factories[$id]($this); | ||
63 | } finally { | ||
64 | unset($this->loading[$id]); | ||
65 | } | ||
66 | } | ||
67 | |||
68 | public function getProvidedServices(): array | ||
69 | { | ||
70 | if (!isset($this->providedTypes)) { | ||
71 | $this->providedTypes = []; | ||
72 | |||
73 | foreach ($this->factories as $name => $factory) { | ||
74 | if (!\is_callable($factory)) { | ||
75 | $this->providedTypes[$name] = '?'; | ||
76 | } else { | ||
77 | $type = (new \ReflectionFunction($factory))->getReturnType(); | ||
78 | |||
79 | $this->providedTypes[$name] = $type ? ($type->allowsNull() ? '?' : '').($type instanceof \ReflectionNamedType ? $type->getName() : $type) : '?'; | ||
80 | } | ||
81 | } | ||
82 | } | ||
83 | |||
84 | return $this->providedTypes; | ||
85 | } | ||
86 | |||
87 | private function createNotFoundException(string $id): NotFoundExceptionInterface | ||
88 | { | ||
89 | if (!$alternatives = array_keys($this->factories)) { | ||
90 | $message = 'is empty...'; | ||
91 | } else { | ||
92 | $last = array_pop($alternatives); | ||
93 | if ($alternatives) { | ||
94 | $message = sprintf('only knows about the "%s" and "%s" services.', implode('", "', $alternatives), $last); | ||
95 | } else { | ||
96 | $message = sprintf('only knows about the "%s" service.', $last); | ||
97 | } | ||
98 | } | ||
99 | |||
100 | if ($this->loading) { | ||
101 | $message = sprintf('The service "%s" has a dependency on a non-existent service "%s". This locator %s', end($this->loading), $id, $message); | ||
102 | } else { | ||
103 | $message = sprintf('Service "%s" not found: the current service locator %s', $id, $message); | ||
104 | } | ||
105 | |||
106 | return new class($message) extends \InvalidArgumentException implements NotFoundExceptionInterface { | ||
107 | }; | ||
108 | } | ||
109 | |||
110 | private function createCircularReferenceException(string $id, array $path): ContainerExceptionInterface | ||
111 | { | ||
112 | return new class(sprintf('Circular reference detected for service "%s", path: "%s".', $id, implode(' -> ', $path))) extends \RuntimeException implements ContainerExceptionInterface { | ||
113 | }; | ||
114 | } | ||
115 | } | ||