| 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 YoastSEO_Vendor\Symfony\Component\DependencyInjection; |
| 12 |
|
| 13 |
use YoastSEO_Vendor\Psr\Container\ContainerExceptionInterface; |
| 14 |
use YoastSEO_Vendor\Psr\Container\NotFoundExceptionInterface; |
| 15 |
use YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\RuntimeException; |
| 16 |
use YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException; |
| 17 |
use YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; |
| 18 |
use YoastSEO_Vendor\Symfony\Contracts\Service\ServiceLocatorTrait; |
| 19 |
use YoastSEO_Vendor\Symfony\Contracts\Service\ServiceProviderInterface; |
| 20 |
use YoastSEO_Vendor\Symfony\Contracts\Service\ServiceSubscriberInterface; |
| 21 |
/** |
| 22 |
* @author Robin Chalas <robin.chalas@gmail.com> |
| 23 |
* @author Nicolas Grekas <p@tchwork.com> |
| 24 |
*/ |
| 25 |
class ServiceLocator implements \YoastSEO_Vendor\Symfony\Contracts\Service\ServiceProviderInterface |
| 26 |
{ |
| 27 |
use ServiceLocatorTrait { |
| 28 |
get as private doGet; |
| 29 |
} |
| 30 |
private $externalId; |
| 31 |
private $container; |
| 32 |
/** |
| 33 |
* {@inheritdoc} |
| 34 |
* |
| 35 |
* @return mixed |
| 36 |
*/ |
| 37 |
public function get(string $id) |
| 38 |
{ |
| 39 |
if (!$this->externalId) { |
| 40 |
return $this->doGet($id); |
| 41 |
} |
| 42 |
try { |
| 43 |
return $this->doGet($id); |
| 44 |
} catch (\YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\RuntimeException $e) { |
| 45 |
$what = \sprintf('service "%s" required by "%s"', $id, $this->externalId); |
| 46 |
$message = \preg_replace('/service "\\.service_locator\\.[^"]++"/', $what, $e->getMessage()); |
| 47 |
if ($e->getMessage() === $message) { |
| 48 |
$message = \sprintf('Cannot resolve %s: %s', $what, $message); |
| 49 |
} |
| 50 |
$r = new \ReflectionProperty($e, 'message'); |
| 51 |
$r->setAccessible(\true); |
| 52 |
$r->setValue($e, $message); |
| 53 |
throw $e; |
| 54 |
} |
| 55 |
} |
| 56 |
public function __invoke(string $id) |
| 57 |
{ |
| 58 |
return isset($this->factories[$id]) ? $this->get($id) : null; |
| 59 |
} |
| 60 |
/** |
| 61 |
* @internal |
| 62 |
* |
| 63 |
* @return static |
| 64 |
*/ |
| 65 |
public function withContext(string $externalId, \YoastSEO_Vendor\Symfony\Component\DependencyInjection\Container $container) : self |
| 66 |
{ |
| 67 |
$locator = clone $this; |
| 68 |
$locator->externalId = $externalId; |
| 69 |
$locator->container = $container; |
| 70 |
return $locator; |
| 71 |
} |
| 72 |
private function createNotFoundException(string $id) : \YoastSEO_Vendor\Psr\Container\NotFoundExceptionInterface |
| 73 |
{ |
| 74 |
if ($this->loading) { |
| 75 |
$msg = \sprintf('The service "%s" has a dependency on a non-existent service "%s". This locator %s', \end($this->loading), $id, $this->formatAlternatives()); |
| 76 |
return new \YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException($id, \end($this->loading) ?: null, null, [], $msg); |
| 77 |
} |
| 78 |
$class = \debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT | \DEBUG_BACKTRACE_IGNORE_ARGS, 4); |
| 79 |
$class = isset($class[3]['object']) ? \get_class($class[3]['object']) : null; |
| 80 |
$externalId = $this->externalId ?: $class; |
| 81 |
$msg = []; |
| 82 |
$msg[] = \sprintf('Service "%s" not found:', $id); |
| 83 |
if (!$this->container) { |
| 84 |
$class = null; |
| 85 |
} elseif ($this->container->has($id) || isset($this->container->getRemovedIds()[$id])) { |
| 86 |
$msg[] = 'even though it exists in the app\'s container,'; |
| 87 |
} else { |
| 88 |
try { |
| 89 |
$this->container->get($id); |
| 90 |
$class = null; |
| 91 |
} catch (\YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException $e) { |
| 92 |
if ($e->getAlternatives()) { |
| 93 |
$msg[] = \sprintf('did you mean %s? Anyway,', $this->formatAlternatives($e->getAlternatives(), 'or')); |
| 94 |
} else { |
| 95 |
$class = null; |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
if ($externalId) { |
| 100 |
$msg[] = \sprintf('the container inside "%s" is a smaller service locator that %s', $externalId, $this->formatAlternatives()); |
| 101 |
} else { |
| 102 |
$msg[] = \sprintf('the current service locator %s', $this->formatAlternatives()); |
| 103 |
} |
| 104 |
if (!$class) { |
| 105 |
// no-op |
| 106 |
} elseif (\is_subclass_of($class, \YoastSEO_Vendor\Symfony\Contracts\Service\ServiceSubscriberInterface::class)) { |
| 107 |
$msg[] = \sprintf('Unless you need extra laziness, try using dependency injection instead. Otherwise, you need to declare it using "%s::getSubscribedServices()".', \preg_replace('/([^\\\\]++\\\\)++/', '', $class)); |
| 108 |
} else { |
| 109 |
$msg[] = 'Try using dependency injection instead.'; |
| 110 |
} |
| 111 |
return new \YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException($id, \end($this->loading) ?: null, null, [], \implode(' ', $msg)); |
| 112 |
} |
| 113 |
private function createCircularReferenceException(string $id, array $path) : \YoastSEO_Vendor\Psr\Container\ContainerExceptionInterface |
| 114 |
{ |
| 115 |
return new \YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException($id, $path); |
| 116 |
} |
| 117 |
private function formatAlternatives(?array $alternatives = null, string $separator = 'and') : string |
| 118 |
{ |
| 119 |
$format = '"%s"%s'; |
| 120 |
if (null === $alternatives) { |
| 121 |
if (!($alternatives = \array_keys($this->factories))) { |
| 122 |
return 'is empty...'; |
| 123 |
} |
| 124 |
$format = \sprintf('only knows about the %s service%s.', $format, 1 < \count($alternatives) ? 's' : ''); |
| 125 |
} |
| 126 |
$last = \array_pop($alternatives); |
| 127 |
return \sprintf($format, $alternatives ? \implode('", "', $alternatives) : $last, $alternatives ? \sprintf(' %s "%s"', $separator, $last) : ''); |
| 128 |
} |
| 129 |
} |
| 130 |
|