Attribute
2 years ago
Test
1 month ago
ResetInterface.php
2 years ago
ServiceLocatorTrait.php
1 month ago
ServiceProviderInterface.php
2 years ago
ServiceSubscriberInterface.php
1 month ago
ServiceSubscriberTrait.php
1 month ago
ServiceLocatorTrait.php
111 lines
| 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 | namespace IAWPSCOPED\Symfony\Contracts\Service; |
| 12 | |
| 13 | use IAWPSCOPED\Psr\Container\ContainerExceptionInterface; |
| 14 | use IAWPSCOPED\Psr\Container\NotFoundExceptionInterface; |
| 15 | // Help opcache.preload discover always-needed symbols |
| 16 | \class_exists(ContainerExceptionInterface::class); |
| 17 | \class_exists(NotFoundExceptionInterface::class); |
| 18 | /** |
| 19 | * A trait to help implement ServiceProviderInterface. |
| 20 | * |
| 21 | * @author Robin Chalas <robin.chalas@gmail.com> |
| 22 | * @author Nicolas Grekas <p@tchwork.com> |
| 23 | * @internal |
| 24 | */ |
| 25 | trait ServiceLocatorTrait |
| 26 | { |
| 27 | private array $factories; |
| 28 | private array $loading = []; |
| 29 | private array $providedTypes; |
| 30 | /** |
| 31 | * @param callable[] $factories |
| 32 | */ |
| 33 | public function __construct(array $factories) |
| 34 | { |
| 35 | $this->factories = $factories; |
| 36 | } |
| 37 | /** |
| 38 | * {@inheritdoc} |
| 39 | */ |
| 40 | public function has(string $id) : bool |
| 41 | { |
| 42 | return isset($this->factories[$id]); |
| 43 | } |
| 44 | /** |
| 45 | * {@inheritdoc} |
| 46 | */ |
| 47 | public function get(string $id) : mixed |
| 48 | { |
| 49 | if (!isset($this->factories[$id])) { |
| 50 | throw $this->createNotFoundException($id); |
| 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 | throw $this->createCircularReferenceException($id, $ids); |
| 57 | } |
| 58 | $this->loading[$id] = $id; |
| 59 | try { |
| 60 | return $this->factories[$id]($this); |
| 61 | } finally { |
| 62 | unset($this->loading[$id]); |
| 63 | } |
| 64 | } |
| 65 | /** |
| 66 | * {@inheritdoc} |
| 67 | */ |
| 68 | public function getProvidedServices() : array |
| 69 | { |
| 70 | if (!isset($this->providedTypes)) { |
| 71 | $this->providedTypes = []; |
| 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 | $this->providedTypes[$name] = $type ? ($type->allowsNull() ? '?' : '') . ($type instanceof \ReflectionNamedType ? $type->getName() : $type) : '?'; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | return $this->providedTypes; |
| 82 | } |
| 83 | private function createNotFoundException(string $id) : NotFoundExceptionInterface |
| 84 | { |
| 85 | if (!($alternatives = \array_keys($this->factories))) { |
| 86 | $message = 'is empty...'; |
| 87 | } else { |
| 88 | $last = \array_pop($alternatives); |
| 89 | if ($alternatives) { |
| 90 | $message = \sprintf('only knows about the "%s" and "%s" services.', \implode('", "', $alternatives), $last); |
| 91 | } else { |
| 92 | $message = \sprintf('only knows about the "%s" service.', $last); |
| 93 | } |
| 94 | } |
| 95 | if ($this->loading) { |
| 96 | $message = \sprintf('The service "%s" has a dependency on a non-existent service "%s". This locator %s', \end($this->loading), $id, $message); |
| 97 | } else { |
| 98 | $message = \sprintf('Service "%s" not found: the current service locator %s', $id, $message); |
| 99 | } |
| 100 | return new class($message) extends \InvalidArgumentException implements NotFoundExceptionInterface |
| 101 | { |
| 102 | }; |
| 103 | } |
| 104 | private function createCircularReferenceException(string $id, array $path) : ContainerExceptionInterface |
| 105 | { |
| 106 | return new class(\sprintf('Circular reference detected for service "%s", path: "%s".', $id, \implode(' -> ', $path))) extends \RuntimeException implements ContainerExceptionInterface |
| 107 | { |
| 108 | }; |
| 109 | } |
| 110 | } |
| 111 |