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