|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace DI; |
| 6 | + |
| 7 | +use Psr\Container\ContainerExceptionInterface; |
| 8 | +use Psr\Container\ContainerInterface; |
| 9 | +use Psr\Container\NotFoundExceptionInterface; |
| 10 | + |
| 11 | +/** |
| 12 | + * Class ServiceLocator. |
| 13 | + * |
| 14 | + * Serving "lazy" dependencies for classes using ServiceSubscriberInterface. |
| 15 | + * Suggested as a lightweight alternative for heavyweight proxies from ocramius/proxy-manager |
| 16 | + */ |
| 17 | +class ServiceLocator implements ContainerInterface |
| 18 | +{ |
| 19 | + /** |
| 20 | + * Constructor. |
| 21 | + * @param string|null $subscriber className of a ServiceSubscriber to which this service locator instance belongs to |
| 22 | + */ |
| 23 | + public function __construct( |
| 24 | + private ContainerInterface $container, |
| 25 | + private array $services, |
| 26 | + private ?string $subscriber = null |
| 27 | + ) { |
| 28 | + $this->setServices($services); |
| 29 | + } |
| 30 | + |
| 31 | + protected function setServices(array $services) |
| 32 | + { |
| 33 | + foreach ($services as $key => $value) { |
| 34 | + if (is_numeric($key)) { |
| 35 | + $key = $value; |
| 36 | + } |
| 37 | + $this->services[$key] = $value; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Get defined services. |
| 43 | + */ |
| 44 | + public function getServices() : array |
| 45 | + { |
| 46 | + return $this->services; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Get name of a class to which this service locator instance belongs to. |
| 51 | + */ |
| 52 | + public function getSubscriber() : string |
| 53 | + { |
| 54 | + return $this->subscriber; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Finds a service by its identifier. |
| 59 | + * |
| 60 | + * @param string $id Identifier of the entry to look for. |
| 61 | + * |
| 62 | + * @throws NotFoundExceptionInterface No entry was found for **this** identifier. |
| 63 | + * @throws ContainerExceptionInterface Error while retrieving the entry. |
| 64 | + */ |
| 65 | + public function get(string $id) : mixed |
| 66 | + { |
| 67 | + if (!isset($this->services[$id])) { |
| 68 | + throw new NotFoundException("Service '$id' is not defined."); |
| 69 | + } |
| 70 | + |
| 71 | + return $this->container->get($this->services[$id]); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Returns true if the container can return an entry for the given identifier. |
| 76 | + * Returns false otherwise. |
| 77 | + * |
| 78 | + * `has($id)` returning true does not mean that `get($id)` will not throw an exception. |
| 79 | + * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. |
| 80 | + * |
| 81 | + * @param string $id Identifier of the entry to look for. |
| 82 | + */ |
| 83 | + public function has(string $id) : bool |
| 84 | + { |
| 85 | + if (!isset($this->services[$id])) { |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + return $this->container->has($this->services[$id]); |
| 90 | + } |
| 91 | +} |
0 commit comments