/src/Symfony/Component/DependencyInjection/Compiler/ResolveNamedArgumentsPass.php

https://github.com/pulzarraider/symfony · PHP · 112 lines · 77 code · 19 blank · 16 comment · 21 complexity · dd48330791b772b058b3f5700f68cf26 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\Compiler;
  11. use Symfony\Component\DependencyInjection\Definition;
  12. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  13. use Symfony\Component\DependencyInjection\LazyProxy\ProxyHelper;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. /**
  16. * Resolves named arguments to their corresponding numeric index.
  17. *
  18. * @author Kévin Dunglas <dunglas@gmail.com>
  19. */
  20. class ResolveNamedArgumentsPass extends AbstractRecursivePass
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. protected function processValue($value, $isRoot = false)
  26. {
  27. if (!$value instanceof Definition) {
  28. return parent::processValue($value, $isRoot);
  29. }
  30. $calls = $value->getMethodCalls();
  31. $calls[] = ['__construct', $value->getArguments()];
  32. foreach ($calls as $i => $call) {
  33. list($method, $arguments) = $call;
  34. $parameters = null;
  35. $resolvedArguments = [];
  36. foreach ($arguments as $key => $argument) {
  37. if (\is_int($key)) {
  38. $resolvedArguments[$key] = $argument;
  39. continue;
  40. }
  41. if (null === $parameters) {
  42. $r = $this->getReflectionMethod($value, $method);
  43. $class = $r instanceof \ReflectionMethod ? $r->class : $this->currentId;
  44. $method = $r->getName();
  45. $parameters = $r->getParameters();
  46. }
  47. if (isset($key[0]) && '$' !== $key[0] && !class_exists($key)) {
  48. throw new InvalidArgumentException(sprintf('Invalid service "%s": did you forget to add the "$" prefix to argument "%s"?', $this->currentId, $key));
  49. }
  50. if (isset($key[0]) && '$' === $key[0]) {
  51. foreach ($parameters as $j => $p) {
  52. if ($key === '$'.$p->name) {
  53. if ($p->isVariadic() && \is_array($argument)) {
  54. foreach ($argument as $variadicArgument) {
  55. $resolvedArguments[$j++] = $variadicArgument;
  56. }
  57. } else {
  58. $resolvedArguments[$j] = $argument;
  59. }
  60. continue 2;
  61. }
  62. }
  63. 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));
  64. }
  65. if (null !== $argument && !$argument instanceof Reference && !$argument instanceof Definition) {
  66. 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)));
  67. }
  68. $typeFound = false;
  69. foreach ($parameters as $j => $p) {
  70. if (!\array_key_exists($j, $resolvedArguments) && ProxyHelper::getTypeHint($r, $p, true) === $key) {
  71. $resolvedArguments[$j] = $argument;
  72. $typeFound = true;
  73. }
  74. }
  75. if (!$typeFound) {
  76. 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));
  77. }
  78. }
  79. if ($resolvedArguments !== $call[1]) {
  80. ksort($resolvedArguments);
  81. $calls[$i][1] = $resolvedArguments;
  82. }
  83. }
  84. list(, $arguments) = array_pop($calls);
  85. if ($arguments !== $value->getArguments()) {
  86. $value->setArguments($arguments);
  87. }
  88. if ($calls !== $value->getMethodCalls()) {
  89. $value->setMethodCalls($calls);
  90. }
  91. return parent::processValue($value, $isRoot);
  92. }
  93. }