Chris@14: Chris@14: * Chris@14: * For the full copyright and license information, please view the LICENSE Chris@14: * file that was distributed with this source code. Chris@14: */ Chris@14: Chris@14: namespace Symfony\Component\DependencyInjection; Chris@14: Chris@14: use Psr\Container\ContainerInterface as PsrContainerInterface; Chris@14: use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException; Chris@14: use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; Chris@14: Chris@14: /** Chris@14: * @author Robin Chalas Chris@14: * @author Nicolas Grekas Chris@14: */ Chris@14: class ServiceLocator implements PsrContainerInterface Chris@14: { Chris@14: private $factories; Chris@17: private $loading = []; Chris@14: private $externalId; Chris@14: private $container; Chris@14: Chris@14: /** Chris@14: * @param callable[] $factories Chris@14: */ Chris@14: public function __construct(array $factories) Chris@14: { Chris@14: $this->factories = $factories; Chris@14: } Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@14: public function has($id) Chris@14: { Chris@14: return isset($this->factories[$id]); Chris@14: } Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@14: public function get($id) Chris@14: { Chris@14: if (!isset($this->factories[$id])) { Chris@17: throw new ServiceNotFoundException($id, end($this->loading) ?: null, null, [], $this->createServiceNotFoundMessage($id)); Chris@14: } Chris@14: Chris@14: if (isset($this->loading[$id])) { Chris@14: $ids = array_values($this->loading); Chris@17: $ids = \array_slice($this->loading, array_search($id, $ids)); Chris@14: $ids[] = $id; Chris@14: Chris@14: throw new ServiceCircularReferenceException($id, $ids); Chris@14: } Chris@14: Chris@14: $this->loading[$id] = $id; Chris@14: try { Chris@14: return $this->factories[$id](); Chris@14: } finally { Chris@14: unset($this->loading[$id]); Chris@14: } Chris@14: } Chris@14: Chris@14: public function __invoke($id) Chris@14: { Chris@14: return isset($this->factories[$id]) ? $this->get($id) : null; Chris@14: } Chris@14: Chris@14: /** Chris@14: * @internal Chris@14: */ Chris@14: public function withContext($externalId, Container $container) Chris@14: { Chris@14: $locator = clone $this; Chris@14: $locator->externalId = $externalId; Chris@14: $locator->container = $container; Chris@14: Chris@14: return $locator; Chris@14: } Chris@14: Chris@14: private function createServiceNotFoundMessage($id) Chris@14: { Chris@14: if ($this->loading) { Chris@14: return sprintf('The service "%s" has a dependency on a non-existent service "%s". This locator %s', end($this->loading), $id, $this->formatAlternatives()); Chris@14: } Chris@14: Chris@14: $class = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS, 3); Chris@17: $class = isset($class[2]['object']) ? \get_class($class[2]['object']) : null; Chris@14: $externalId = $this->externalId ?: $class; Chris@14: Chris@17: $msg = []; Chris@17: $msg[] = sprintf('Service "%s" not found:', $id); Chris@14: Chris@14: if (!$this->container) { Chris@14: $class = null; Chris@14: } elseif ($this->container->has($id) || isset($this->container->getRemovedIds()[$id])) { Chris@17: $msg[] = 'even though it exists in the app\'s container,'; Chris@14: } else { Chris@14: try { Chris@14: $this->container->get($id); Chris@14: $class = null; Chris@14: } catch (ServiceNotFoundException $e) { Chris@14: if ($e->getAlternatives()) { Chris@17: $msg[] = sprintf('did you mean %s? Anyway,', $this->formatAlternatives($e->getAlternatives(), 'or')); Chris@14: } else { Chris@14: $class = null; Chris@14: } Chris@14: } Chris@14: } Chris@14: if ($externalId) { Chris@17: $msg[] = sprintf('the container inside "%s" is a smaller service locator that %s', $externalId, $this->formatAlternatives()); Chris@14: } else { Chris@17: $msg[] = sprintf('the current service locator %s', $this->formatAlternatives()); Chris@14: } Chris@14: Chris@14: if (!$class) { Chris@14: // no-op Chris@14: } elseif (is_subclass_of($class, ServiceSubscriberInterface::class)) { Chris@17: $msg[] = sprintf('Unless you need extra laziness, try using dependency injection instead. Otherwise, you need to declare it using "%s::getSubscribedServices()".', preg_replace('/([^\\\\]++\\\\)++/', '', $class)); Chris@14: } else { Chris@17: $msg[] = 'Try using dependency injection instead.'; Chris@14: } Chris@14: Chris@17: return implode(' ', $msg); Chris@14: } Chris@14: Chris@14: private function formatAlternatives(array $alternatives = null, $separator = 'and') Chris@14: { Chris@14: $format = '"%s"%s'; Chris@14: if (null === $alternatives) { Chris@14: if (!$alternatives = array_keys($this->factories)) { Chris@14: return 'is empty...'; Chris@14: } Chris@17: $format = sprintf('only knows about the %s service%s.', $format, 1 < \count($alternatives) ? 's' : ''); Chris@14: } Chris@14: $last = array_pop($alternatives); Chris@14: Chris@14: return sprintf($format, $alternatives ? implode('", "', $alternatives) : $last, $alternatives ? sprintf(' %s "%s"', $separator, $last) : ''); Chris@14: } Chris@14: }