4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Compiler;
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;
20 * Resolves named arguments to their corresponding numeric index.
22 * @author Kévin Dunglas <dunglas@gmail.com>
24 class ResolveNamedArgumentsPass extends AbstractRecursivePass
29 protected function processValue($value, $isRoot = false)
31 if (!$value instanceof Definition) {
32 return parent::processValue($value, $isRoot);
35 $calls = $value->getMethodCalls();
36 $calls[] = array('__construct', $value->getArguments());
38 foreach ($calls as $i => $call) {
39 list($method, $arguments) = $call;
41 $resolvedArguments = array();
43 foreach ($arguments as $key => $argument) {
45 $resolvedArguments[$key] = $argument;
49 if (null === $parameters) {
50 $r = $this->getReflectionMethod($value, $method);
51 $class = $r instanceof \ReflectionMethod ? $r->class : $this->currentId;
52 $parameters = $r->getParameters();
55 if (isset($key[0]) && '$' === $key[0]) {
56 foreach ($parameters as $j => $p) {
57 if ($key === '$'.$p->name) {
58 $resolvedArguments[$j] = $argument;
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));
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)));
72 foreach ($parameters as $j => $p) {
73 if (!array_key_exists($j, $resolvedArguments) && ProxyHelper::getTypeHint($r, $p, true) === $key) {
74 $resolvedArguments[$j] = $argument;
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));
84 if ($resolvedArguments !== $call[1]) {
85 ksort($resolvedArguments);
86 $calls[$i][1] = $resolvedArguments;
90 list(, $arguments) = array_pop($calls);
92 if ($arguments !== $value->getArguments()) {
93 $value->setArguments($arguments);
95 if ($calls !== $value->getMethodCalls()) {
96 $value->setMethodCalls($calls);
99 return parent::processValue($value, $isRoot);