annotate vendor/symfony/dependency-injection/Compiler/ResolveNamedArgumentsPass.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\Definition;
Chris@0 15 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
Chris@0 16 use Symfony\Component\DependencyInjection\LazyProxy\ProxyHelper;
Chris@0 17 use Symfony\Component\DependencyInjection\Reference;
Chris@0 18
Chris@0 19 /**
Chris@0 20 * Resolves named arguments to their corresponding numeric index.
Chris@0 21 *
Chris@0 22 * @author Kévin Dunglas <dunglas@gmail.com>
Chris@0 23 */
Chris@0 24 class ResolveNamedArgumentsPass extends AbstractRecursivePass
Chris@0 25 {
Chris@0 26 /**
Chris@0 27 * {@inheritdoc}
Chris@0 28 */
Chris@0 29 protected function processValue($value, $isRoot = false)
Chris@0 30 {
Chris@0 31 if (!$value instanceof Definition) {
Chris@0 32 return parent::processValue($value, $isRoot);
Chris@0 33 }
Chris@0 34
Chris@0 35 $calls = $value->getMethodCalls();
Chris@4 36 $calls[] = ['__construct', $value->getArguments()];
Chris@0 37
Chris@0 38 foreach ($calls as $i => $call) {
Chris@0 39 list($method, $arguments) = $call;
Chris@0 40 $parameters = null;
Chris@4 41 $resolvedArguments = [];
Chris@0 42
Chris@0 43 foreach ($arguments as $key => $argument) {
Chris@4 44 if (\is_int($key)) {
Chris@0 45 $resolvedArguments[$key] = $argument;
Chris@0 46 continue;
Chris@0 47 }
Chris@0 48
Chris@0 49 if (null === $parameters) {
Chris@0 50 $r = $this->getReflectionMethod($value, $method);
Chris@0 51 $class = $r instanceof \ReflectionMethod ? $r->class : $this->currentId;
Chris@4 52 $method = $r->getName();
Chris@0 53 $parameters = $r->getParameters();
Chris@0 54 }
Chris@0 55
Chris@0 56 if (isset($key[0]) && '$' === $key[0]) {
Chris@0 57 foreach ($parameters as $j => $p) {
Chris@0 58 if ($key === '$'.$p->name) {
Chris@0 59 $resolvedArguments[$j] = $argument;
Chris@0 60
Chris@0 61 continue 2;
Chris@0 62 }
Chris@0 63 }
Chris@0 64
Chris@0 65 throw new InvalidArgumentException(sprintf('Invalid service "%s": method "%s()" has no argument named "%s". Check your service definition.', $this->currentId, $class !== $this->currentId ? $class.'::'.$method : $method, $key));
Chris@0 66 }
Chris@0 67
Chris@0 68 if (null !== $argument && !$argument instanceof Reference && !$argument instanceof Definition) {
Chris@4 69 throw new InvalidArgumentException(sprintf('Invalid service "%s": the value of argument "%s" of method "%s()" must be null, an instance of %s or an instance of %s, %s given.', $this->currentId, $key, $class !== $this->currentId ? $class.'::'.$method : $method, Reference::class, Definition::class, \gettype($argument)));
Chris@0 70 }
Chris@0 71
Chris@0 72 $typeFound = false;
Chris@0 73 foreach ($parameters as $j => $p) {
Chris@5 74 if (!\array_key_exists($j, $resolvedArguments) && ProxyHelper::getTypeHint($r, $p, true) === $key) {
Chris@0 75 $resolvedArguments[$j] = $argument;
Chris@0 76 $typeFound = true;
Chris@0 77 }
Chris@0 78 }
Chris@0 79
Chris@0 80 if (!$typeFound) {
Chris@0 81 throw new InvalidArgumentException(sprintf('Invalid service "%s": method "%s()" has no argument type-hinted as "%s". Check your service definition.', $this->currentId, $class !== $this->currentId ? $class.'::'.$method : $method, $key));
Chris@0 82 }
Chris@0 83 }
Chris@0 84
Chris@0 85 if ($resolvedArguments !== $call[1]) {
Chris@0 86 ksort($resolvedArguments);
Chris@0 87 $calls[$i][1] = $resolvedArguments;
Chris@0 88 }
Chris@0 89 }
Chris@0 90
Chris@0 91 list(, $arguments) = array_pop($calls);
Chris@0 92
Chris@0 93 if ($arguments !== $value->getArguments()) {
Chris@0 94 $value->setArguments($arguments);
Chris@0 95 }
Chris@0 96 if ($calls !== $value->getMethodCalls()) {
Chris@0 97 $value->setMethodCalls($calls);
Chris@0 98 }
Chris@0 99
Chris@0 100 return parent::processValue($value, $isRoot);
Chris@0 101 }
Chris@0 102 }