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