4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection;
14 use Psr\Container\ContainerInterface as PsrContainerInterface;
15 use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
16 use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
19 * @author Robin Chalas <robin.chalas@gmail.com>
20 * @author Nicolas Grekas <p@tchwork.com>
22 class ServiceLocator implements PsrContainerInterface
25 private $loading = array();
30 * @param callable[] $factories
32 public function __construct(array $factories)
34 $this->factories = $factories;
40 public function has($id)
42 return isset($this->factories[$id]);
48 public function get($id)
50 if (!isset($this->factories[$id])) {
51 throw new ServiceNotFoundException($id, end($this->loading) ?: null, null, array(), $this->createServiceNotFoundMessage($id));
54 if (isset($this->loading[$id])) {
55 $ids = array_values($this->loading);
56 $ids = array_slice($this->loading, array_search($id, $ids));
59 throw new ServiceCircularReferenceException($id, $ids);
62 $this->loading[$id] = $id;
64 return $this->factories[$id]();
66 unset($this->loading[$id]);
70 public function __invoke($id)
72 return isset($this->factories[$id]) ? $this->get($id) : null;
78 public function withContext($externalId, Container $container)
80 $locator = clone $this;
81 $locator->externalId = $externalId;
82 $locator->container = $container;
87 private function createServiceNotFoundMessage($id)
90 return sprintf('The service "%s" has a dependency on a non-existent service "%s". This locator %s', end($this->loading), $id, $this->formatAlternatives());
93 $class = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS, 3);
94 $class = isset($class[2]['object']) ? get_class($class[2]['object']) : null;
95 $externalId = $this->externalId ?: $class;
97 $msg = sprintf('Service "%s" not found: ', $id);
99 if (!$this->container) {
101 } elseif ($this->container->has($id) || isset($this->container->getRemovedIds()[$id])) {
102 $msg .= 'even though it exists in the app\'s container, ';
105 $this->container->get($id);
107 } catch (ServiceNotFoundException $e) {
108 if ($e->getAlternatives()) {
109 $msg .= sprintf(' did you mean %s? Anyway, ', $this->formatAlternatives($e->getAlternatives(), 'or'));
116 $msg .= sprintf('the container inside "%s" is a smaller service locator that %s', $externalId, $this->formatAlternatives());
118 $msg .= sprintf('the current service locator %s', $this->formatAlternatives());
123 } elseif (is_subclass_of($class, ServiceSubscriberInterface::class)) {
124 $msg .= sprintf(' Unless you need extra laziness, try using dependency injection instead. Otherwise, you need to declare it using "%s::getSubscribedServices()".', preg_replace('/([^\\\\]++\\\\)++/', '', $class));
126 $msg .= 'Try using dependency injection instead.';
132 private function formatAlternatives(array $alternatives = null, $separator = 'and')
135 if (null === $alternatives) {
136 if (!$alternatives = array_keys($this->factories)) {
137 return 'is empty...';
139 $format = sprintf('only knows about the %s service%s.', $format, 1 < count($alternatives) ? 's' : '');
141 $last = array_pop($alternatives);
143 return sprintf($format, $alternatives ? implode('", "', $alternatives) : $last, $alternatives ? sprintf(' %s "%s"', $separator, $last) : '');