ResolveNamedArgumentsPass.php 5.5 KB

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