comparison vendor/symfony/dependency-injection/Compiler/ResolveBindingsPass.php @ 14:1fec387a4317

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