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