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\Compiler;
14 use Symfony\Component\DependencyInjection\Alias;
15 use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
16 use Symfony\Component\DependencyInjection\ContainerBuilder;
17 use Symfony\Component\DependencyInjection\Definition;
18 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
19 use Symfony\Component\DependencyInjection\Reference;
20 use Symfony\Component\DependencyInjection\ServiceLocator;
23 * Applies the "container.service_locator" tag by wrapping references into ServiceClosureArgument instances.
25 * @author Nicolas Grekas <p@tchwork.com>
27 final class ServiceLocatorTagPass extends AbstractRecursivePass
29 protected function processValue($value, $isRoot = false)
31 if (!$value instanceof Definition || !$value->hasTag('container.service_locator')) {
32 return parent::processValue($value, $isRoot);
35 if (!$value->getClass()) {
36 $value->setClass(ServiceLocator::class);
39 $arguments = $value->getArguments();
40 if (!isset($arguments[0]) || !\is_array($arguments[0])) {
41 throw new InvalidArgumentException(sprintf('Invalid definition for service "%s": an array of references is expected as first argument when the "container.service_locator" tag is set.', $this->currentId));
44 foreach ($arguments[0] as $k => $v) {
45 if ($v instanceof ServiceClosureArgument) {
48 if (!$v instanceof Reference) {
49 throw new InvalidArgumentException(sprintf('Invalid definition for service "%s": an array of references is expected as first argument when the "container.service_locator" tag is set, "%s" found for key "%s".', $this->currentId, \is_object($v) ? \get_class($v) : \gettype($v), $k));
51 $arguments[0][$k] = new ServiceClosureArgument($v);
55 $value->setArguments($arguments);
57 $id = 'service_locator.'.ContainerBuilder::hash($value);
60 if ($id !== $this->currentId) {
61 $this->container->setAlias($id, new Alias($this->currentId, false));
67 $this->container->setDefinition($id, $value->setPublic(false));
69 return new Reference($id);
73 * @param ContainerBuilder $container
74 * @param Reference[] $refMap
75 * @param string|null $callerId
79 public static function register(ContainerBuilder $container, array $refMap, $callerId = null)
81 foreach ($refMap as $id => $ref) {
82 if (!$ref instanceof Reference) {
83 throw new InvalidArgumentException(sprintf('Invalid service locator definition: only services can be referenced, "%s" found for key "%s". Inject parameter values using constructors instead.', \is_object($ref) ? \get_class($ref) : \gettype($ref), $id));
85 $refMap[$id] = new ServiceClosureArgument($ref);
89 $locator = (new Definition(ServiceLocator::class))
90 ->addArgument($refMap)
92 ->addTag('container.service_locator');
94 if (!$container->has($id = 'service_locator.'.ContainerBuilder::hash($locator))) {
95 $container->setDefinition($id, $locator);
98 if (null !== $callerId) {
100 // Locators are shared when they hold the exact same list of factories;
101 // to have them specialized per consumer service, we use a cloning factory
102 // to derivate customized instances from the prototype one.
103 $container->register($id .= '.'.$callerId, ServiceLocator::class)
105 ->setFactory(array(new Reference($locatorId), 'withContext'))
106 ->addArgument($callerId)
107 ->addArgument(new Reference('service_container'));
110 return new Reference($id);