Test
3 years ago
ResetInterface.php
4 years ago
ServiceLocatorTrait.php
4 years ago
ServiceProviderInterface.php
4 years ago
ServiceSubscriberInterface.php
4 years ago
ServiceSubscriberTrait.php
4 years ago
index.php
3 years ago
ServiceLocatorTrait.php
83 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Contracts\Service; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Psr\Container\ContainerExceptionInterface; |
| 5 | use MailPoetVendor\Psr\Container\NotFoundExceptionInterface; |
| 6 | // Help opcache.preload discover always-needed symbols |
| 7 | \class_exists(ContainerExceptionInterface::class); |
| 8 | \class_exists(NotFoundExceptionInterface::class); |
| 9 | trait ServiceLocatorTrait |
| 10 | { |
| 11 | private $factories; |
| 12 | private $loading = []; |
| 13 | private $providedTypes; |
| 14 | public function __construct(array $factories) |
| 15 | { |
| 16 | $this->factories = $factories; |
| 17 | } |
| 18 | public function has($id) |
| 19 | { |
| 20 | return isset($this->factories[$id]); |
| 21 | } |
| 22 | public function get($id) |
| 23 | { |
| 24 | if (!isset($this->factories[$id])) { |
| 25 | throw $this->createNotFoundException($id); |
| 26 | } |
| 27 | if (isset($this->loading[$id])) { |
| 28 | $ids = \array_values($this->loading); |
| 29 | $ids = \array_slice($this->loading, \array_search($id, $ids)); |
| 30 | $ids[] = $id; |
| 31 | throw $this->createCircularReferenceException($id, $ids); |
| 32 | } |
| 33 | $this->loading[$id] = $id; |
| 34 | try { |
| 35 | return $this->factories[$id]($this); |
| 36 | } finally { |
| 37 | unset($this->loading[$id]); |
| 38 | } |
| 39 | } |
| 40 | public function getProvidedServices() : array |
| 41 | { |
| 42 | if (null === $this->providedTypes) { |
| 43 | $this->providedTypes = []; |
| 44 | foreach ($this->factories as $name => $factory) { |
| 45 | if (!\is_callable($factory)) { |
| 46 | $this->providedTypes[$name] = '?'; |
| 47 | } else { |
| 48 | $type = (new \ReflectionFunction($factory))->getReturnType(); |
| 49 | $this->providedTypes[$name] = $type ? ($type->allowsNull() ? '?' : '') . ($type instanceof \ReflectionNamedType ? $type->getName() : $type) : '?'; |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | return $this->providedTypes; |
| 54 | } |
| 55 | private function createNotFoundException(string $id) : NotFoundExceptionInterface |
| 56 | { |
| 57 | if (!($alternatives = \array_keys($this->factories))) { |
| 58 | $message = 'is empty...'; |
| 59 | } else { |
| 60 | $last = \array_pop($alternatives); |
| 61 | if ($alternatives) { |
| 62 | $message = \sprintf('only knows about the "%s" and "%s" services.', \implode('", "', $alternatives), $last); |
| 63 | } else { |
| 64 | $message = \sprintf('only knows about the "%s" service.', $last); |
| 65 | } |
| 66 | } |
| 67 | if ($this->loading) { |
| 68 | $message = \sprintf('The service "%s" has a dependency on a non-existent service "%s". This locator %s', \end($this->loading), $id, $message); |
| 69 | } else { |
| 70 | $message = \sprintf('Service "%s" not found: the current service locator %s', $id, $message); |
| 71 | } |
| 72 | return new class($message) extends \InvalidArgumentException implements NotFoundExceptionInterface |
| 73 | { |
| 74 | }; |
| 75 | } |
| 76 | private function createCircularReferenceException(string $id, array $path) : ContainerExceptionInterface |
| 77 | { |
| 78 | return new class(\sprintf('Circular reference detected for service "%s", path: "%s".', $id, \implode(' -> ', $path))) extends \RuntimeException implements ContainerExceptionInterface |
| 79 | { |
| 80 | }; |
| 81 | } |
| 82 | } |
| 83 |