annotate vendor/symfony/dependency-injection/Compiler/ResolveBindingsPass.php @ 5:12f9dff5fda9 tip

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