annotate vendor/symfony/dependency-injection/Compiler/ResolveBindingsPass.php @ 16:c2387f117808

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