Argument
2 years ago
Attribute
2 years ago
Exception
2 years ago
ParameterBag
3 years ago
Alias.php
4 years ago
ChildDefinition.php
4 years ago
Container.php
2 years ago
ContainerAwareInterface.php
2 years ago
ContainerAwareTrait.php
2 years ago
ContainerBuilder.php
2 years ago
ContainerInterface.php
4 years ago
Definition.php
2 years ago
EnvVarLoaderInterface.php
4 years ago
EnvVarProcessor.php
2 years ago
EnvVarProcessorInterface.php
4 years ago
ExpressionLanguage.php
2 years ago
ExpressionLanguageProvider.php
2 years ago
Parameter.php
4 years ago
Reference.php
4 years ago
ReverseContainer.php
2 years ago
ServiceLocator.php
2 years ago
TaggedContainerInterface.php
4 years ago
TypedReference.php
2 years ago
Variable.php
4 years ago
index.php
3 years ago
ServiceLocator.php
107 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Component\DependencyInjection; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Psr\Container\ContainerExceptionInterface; |
| 5 | use MailPoetVendor\Psr\Container\NotFoundExceptionInterface; |
| 6 | use MailPoetVendor\Symfony\Component\DependencyInjection\Exception\RuntimeException; |
| 7 | use MailPoetVendor\Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException; |
| 8 | use MailPoetVendor\Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; |
| 9 | use MailPoetVendor\Symfony\Contracts\Service\ServiceLocatorTrait; |
| 10 | use MailPoetVendor\Symfony\Contracts\Service\ServiceProviderInterface; |
| 11 | use MailPoetVendor\Symfony\Contracts\Service\ServiceSubscriberInterface; |
| 12 | class ServiceLocator implements ServiceProviderInterface |
| 13 | { |
| 14 | use ServiceLocatorTrait { |
| 15 | get as private doGet; |
| 16 | } |
| 17 | private $externalId; |
| 18 | private $container; |
| 19 | public function get(string $id) |
| 20 | { |
| 21 | if (!$this->externalId) { |
| 22 | return $this->doGet($id); |
| 23 | } |
| 24 | try { |
| 25 | return $this->doGet($id); |
| 26 | } catch (RuntimeException $e) { |
| 27 | $what = \sprintf('service "%s" required by "%s"', $id, $this->externalId); |
| 28 | $message = \preg_replace('/service "\\.service_locator\\.[^"]++"/', $what, $e->getMessage()); |
| 29 | if ($e->getMessage() === $message) { |
| 30 | $message = \sprintf('Cannot resolve %s: %s', $what, $message); |
| 31 | } |
| 32 | $r = new \ReflectionProperty($e, 'message'); |
| 33 | $r->setAccessible(\true); |
| 34 | $r->setValue($e, $message); |
| 35 | throw $e; |
| 36 | } |
| 37 | } |
| 38 | public function __invoke(string $id) |
| 39 | { |
| 40 | return isset($this->factories[$id]) ? $this->get($id) : null; |
| 41 | } |
| 42 | public function withContext(string $externalId, Container $container) : self |
| 43 | { |
| 44 | $locator = clone $this; |
| 45 | $locator->externalId = $externalId; |
| 46 | $locator->container = $container; |
| 47 | return $locator; |
| 48 | } |
| 49 | private function createNotFoundException(string $id) : NotFoundExceptionInterface |
| 50 | { |
| 51 | if ($this->loading) { |
| 52 | $msg = \sprintf('The service "%s" has a dependency on a non-existent service "%s". This locator %s', \end($this->loading), $id, $this->formatAlternatives()); |
| 53 | return new ServiceNotFoundException($id, \end($this->loading) ?: null, null, [], $msg); |
| 54 | } |
| 55 | $class = \debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT | \DEBUG_BACKTRACE_IGNORE_ARGS, 4); |
| 56 | $class = isset($class[3]['object']) ? \get_class($class[3]['object']) : null; |
| 57 | $externalId = $this->externalId ?: $class; |
| 58 | $msg = []; |
| 59 | $msg[] = \sprintf('Service "%s" not found:', $id); |
| 60 | if (!$this->container) { |
| 61 | $class = null; |
| 62 | } elseif ($this->container->has($id) || isset($this->container->getRemovedIds()[$id])) { |
| 63 | $msg[] = 'even though it exists in the app\'s container,'; |
| 64 | } else { |
| 65 | try { |
| 66 | $this->container->get($id); |
| 67 | $class = null; |
| 68 | } catch (ServiceNotFoundException $e) { |
| 69 | if ($e->getAlternatives()) { |
| 70 | $msg[] = \sprintf('did you mean %s? Anyway,', $this->formatAlternatives($e->getAlternatives(), 'or')); |
| 71 | } else { |
| 72 | $class = null; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | if ($externalId) { |
| 77 | $msg[] = \sprintf('the container inside "%s" is a smaller service locator that %s', $externalId, $this->formatAlternatives()); |
| 78 | } else { |
| 79 | $msg[] = \sprintf('the current service locator %s', $this->formatAlternatives()); |
| 80 | } |
| 81 | if (!$class) { |
| 82 | // no-op |
| 83 | } elseif (\is_subclass_of($class, ServiceSubscriberInterface::class)) { |
| 84 | $msg[] = \sprintf('Unless you need extra laziness, try using dependency injection instead. Otherwise, you need to declare it using "%s::getSubscribedServices()".', \preg_replace('/([^\\\\]++\\\\)++/', '', $class)); |
| 85 | } else { |
| 86 | $msg[] = 'Try using dependency injection instead.'; |
| 87 | } |
| 88 | return new ServiceNotFoundException($id, \end($this->loading) ?: null, null, [], \implode(' ', $msg)); |
| 89 | } |
| 90 | private function createCircularReferenceException(string $id, array $path) : ContainerExceptionInterface |
| 91 | { |
| 92 | return new ServiceCircularReferenceException($id, $path); |
| 93 | } |
| 94 | private function formatAlternatives(?array $alternatives = null, string $separator = 'and') : string |
| 95 | { |
| 96 | $format = '"%s"%s'; |
| 97 | if (null === $alternatives) { |
| 98 | if (!($alternatives = \array_keys($this->factories))) { |
| 99 | return 'is empty...'; |
| 100 | } |
| 101 | $format = \sprintf('only knows about the %s service%s.', $format, 1 < \count($alternatives) ? 's' : ''); |
| 102 | } |
| 103 | $last = \array_pop($alternatives); |
| 104 | return \sprintf($format, $alternatives ? \implode('", "', $alternatives) : $last, $alternatives ? \sprintf(' %s "%s"', $separator, $last) : ''); |
| 105 | } |
| 106 | } |
| 107 |