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