9a895cb523258312c877fe7f63f485a3521eb9db
[yaffs-website] / vendor / symfony / dependency-injection / Compiler / ResolveBindingsPass.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\Argument\BoundArgument;
15 use Symfony\Component\DependencyInjection\ContainerBuilder;
16 use Symfony\Component\DependencyInjection\Definition;
17 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
18 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
19 use Symfony\Component\DependencyInjection\LazyProxy\ProxyHelper;
20 use Symfony\Component\DependencyInjection\TypedReference;
21 use Symfony\Component\DependencyInjection\Reference;
22
23 /**
24  * @author Guilhem Niot <guilhem.niot@gmail.com>
25  */
26 class ResolveBindingsPass extends AbstractRecursivePass
27 {
28     private $usedBindings = array();
29     private $unusedBindings = array();
30     private $errorMessages = array();
31
32     /**
33      * {@inheritdoc}
34      */
35     public function process(ContainerBuilder $container)
36     {
37         try {
38             parent::process($container);
39
40             foreach ($this->unusedBindings as list($key, $serviceId)) {
41                 $message = sprintf('Unused binding "%s" in service "%s".', $key, $serviceId);
42                 if ($this->errorMessages) {
43                     $message .= sprintf("\nCould be related to%s:", 1 < \count($this->errorMessages) ? ' one of' : '');
44                 }
45                 foreach ($this->errorMessages as $m) {
46                     $message .= "\n - ".$m;
47                 }
48                 throw new InvalidArgumentException($message);
49             }
50         } finally {
51             $this->usedBindings = array();
52             $this->unusedBindings = array();
53             $this->errorMessages = array();
54         }
55     }
56
57     /**
58      * {@inheritdoc}
59      */
60     protected function processValue($value, $isRoot = false)
61     {
62         if ($value instanceof TypedReference && $value->getType() === $this->container->normalizeId($value)) {
63             // Already checked
64             $bindings = $this->container->getDefinition($this->currentId)->getBindings();
65
66             if (isset($bindings[$value->getType()])) {
67                 return $this->getBindingValue($bindings[$value->getType()]);
68             }
69
70             return parent::processValue($value, $isRoot);
71         }
72
73         if (!$value instanceof Definition || !$bindings = $value->getBindings()) {
74             return parent::processValue($value, $isRoot);
75         }
76
77         foreach ($bindings as $key => $binding) {
78             list($bindingValue, $bindingId, $used) = $binding->getValues();
79             if ($used) {
80                 $this->usedBindings[$bindingId] = true;
81                 unset($this->unusedBindings[$bindingId]);
82             } elseif (!isset($this->usedBindings[$bindingId])) {
83                 $this->unusedBindings[$bindingId] = array($key, $this->currentId);
84             }
85
86             if (isset($key[0]) && '$' === $key[0]) {
87                 continue;
88             }
89
90             if (null !== $bindingValue && !$bindingValue instanceof Reference && !$bindingValue instanceof Definition) {
91                 throw new InvalidArgumentException(sprintf('Invalid value for binding key "%s" for service "%s": expected null, an instance of %s or an instance of %s, %s given.', $key, $this->currentId, Reference::class, Definition::class, gettype($bindingValue)));
92             }
93         }
94
95         if ($value->isAbstract()) {
96             return parent::processValue($value, $isRoot);
97         }
98
99         $calls = $value->getMethodCalls();
100
101         try {
102             if ($constructor = $this->getConstructor($value, false)) {
103                 $calls[] = array($constructor, $value->getArguments());
104             }
105         } catch (RuntimeException $e) {
106             $this->errorMessages[] = $e->getMessage();
107             $this->container->getDefinition($this->currentId)->addError($e->getMessage());
108
109             return parent::processValue($value, $isRoot);
110         }
111
112         foreach ($calls as $i => $call) {
113             list($method, $arguments) = $call;
114
115             if ($method instanceof \ReflectionFunctionAbstract) {
116                 $reflectionMethod = $method;
117             } else {
118                 $reflectionMethod = $this->getReflectionMethod($value, $method);
119             }
120
121             foreach ($reflectionMethod->getParameters() as $key => $parameter) {
122                 if (array_key_exists($key, $arguments) && '' !== $arguments[$key]) {
123                     continue;
124                 }
125
126                 if (array_key_exists('$'.$parameter->name, $bindings)) {
127                     $arguments[$key] = $this->getBindingValue($bindings['$'.$parameter->name]);
128
129                     continue;
130                 }
131
132                 $typeHint = ProxyHelper::getTypeHint($reflectionMethod, $parameter, true);
133
134                 if (!isset($bindings[$typeHint])) {
135                     continue;
136                 }
137
138                 $arguments[$key] = $this->getBindingValue($bindings[$typeHint]);
139             }
140
141             if ($arguments !== $call[1]) {
142                 ksort($arguments);
143                 $calls[$i][1] = $arguments;
144             }
145         }
146
147         if ($constructor) {
148             list(, $arguments) = array_pop($calls);
149
150             if ($arguments !== $value->getArguments()) {
151                 $value->setArguments($arguments);
152             }
153         }
154
155         if ($calls !== $value->getMethodCalls()) {
156             $value->setMethodCalls($calls);
157         }
158
159         return parent::processValue($value, $isRoot);
160     }
161
162     private function getBindingValue(BoundArgument $binding)
163     {
164         list($bindingValue, $bindingId) = $binding->getValues();
165
166         $this->usedBindings[$bindingId] = true;
167         unset($this->unusedBindings[$bindingId]);
168
169         return $bindingValue;
170     }
171 }