fb32d501ac29e0596a3ea1a005afad380b0cb600
[yaffs-website] / vendor / symfony / dependency-injection / Compiler / ServiceLocatorTagPass.php
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
12 namespace Symfony\Component\DependencyInjection\Compiler;
13
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;
21
22 /**
23  * Applies the "container.service_locator" tag by wrapping references into ServiceClosureArgument instances.
24  *
25  * @author Nicolas Grekas <p@tchwork.com>
26  */
27 final class ServiceLocatorTagPass extends AbstractRecursivePass
28 {
29     protected function processValue($value, $isRoot = false)
30     {
31         if (!$value instanceof Definition || !$value->hasTag('container.service_locator')) {
32             return parent::processValue($value, $isRoot);
33         }
34
35         if (!$value->getClass()) {
36             $value->setClass(ServiceLocator::class);
37         }
38
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));
42         }
43
44         foreach ($arguments[0] as $k => $v) {
45             if ($v instanceof ServiceClosureArgument) {
46                 continue;
47             }
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));
50             }
51             $arguments[0][$k] = new ServiceClosureArgument($v);
52         }
53         ksort($arguments[0]);
54
55         $value->setArguments($arguments);
56
57         $id = 'service_locator.'.ContainerBuilder::hash($value);
58
59         if ($isRoot) {
60             if ($id !== $this->currentId) {
61                 $this->container->setAlias($id, new Alias($this->currentId, false));
62             }
63
64             return $value;
65         }
66
67         $this->container->setDefinition($id, $value->setPublic(false));
68
69         return new Reference($id);
70     }
71
72     /**
73      * @param ContainerBuilder $container
74      * @param Reference[]      $refMap
75      * @param string|null      $callerId
76      *
77      * @return Reference
78      */
79     public static function register(ContainerBuilder $container, array $refMap, $callerId = null)
80     {
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));
84             }
85             $refMap[$id] = new ServiceClosureArgument($ref);
86         }
87         ksort($refMap);
88
89         $locator = (new Definition(ServiceLocator::class))
90             ->addArgument($refMap)
91             ->setPublic(false)
92             ->addTag('container.service_locator');
93
94         if (!$container->has($id = 'service_locator.'.ContainerBuilder::hash($locator))) {
95             $container->setDefinition($id, $locator);
96         }
97
98         if (null !== $callerId) {
99             $locatorId = $id;
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)
104                 ->setPublic(false)
105                 ->setFactory(array(new Reference($locatorId), 'withContext'))
106                 ->addArgument($callerId)
107                 ->addArgument(new Reference('service_container'));
108         }
109
110         return new Reference($id);
111     }
112 }