ArgumentResolver.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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\HttpKernel\Controller;
  11. use Psr\Container\ContainerInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpKernel\Attribute\ValueResolver;
  14. use Symfony\Component\HttpKernel\Controller\ArgumentResolver\DefaultValueResolver;
  15. use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestAttributeValueResolver;
  16. use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestValueResolver;
  17. use Symfony\Component\HttpKernel\Controller\ArgumentResolver\SessionValueResolver;
  18. use Symfony\Component\HttpKernel\Controller\ArgumentResolver\VariadicValueResolver;
  19. use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactory;
  20. use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactoryInterface;
  21. use Symfony\Component\HttpKernel\Exception\NearMissValueResolverException;
  22. use Symfony\Component\HttpKernel\Exception\ResolverNotFoundException;
  23. use Symfony\Contracts\Service\ServiceProviderInterface;
  24. /**
  25. * Responsible for resolving the arguments passed to an action.
  26. *
  27. * @author Iltar van der Berg <kjarli@gmail.com>
  28. */
  29. final class ArgumentResolver implements ArgumentResolverInterface
  30. {
  31. private ArgumentMetadataFactoryInterface $argumentMetadataFactory;
  32. private iterable $argumentValueResolvers;
  33. /**
  34. * @param iterable<mixed, ValueResolverInterface> $argumentValueResolvers
  35. */
  36. public function __construct(
  37. ?ArgumentMetadataFactoryInterface $argumentMetadataFactory = null,
  38. iterable $argumentValueResolvers = [],
  39. private ?ContainerInterface $namedResolvers = null,
  40. ) {
  41. $this->argumentMetadataFactory = $argumentMetadataFactory ?? new ArgumentMetadataFactory();
  42. $this->argumentValueResolvers = $argumentValueResolvers ?: self::getDefaultArgumentValueResolvers();
  43. }
  44. public function getArguments(Request $request, callable $controller, ?\ReflectionFunctionAbstract $reflector = null): array
  45. {
  46. $arguments = [];
  47. foreach ($this->argumentMetadataFactory->createArgumentMetadata($controller, $reflector) as $metadata) {
  48. $argumentValueResolvers = $this->argumentValueResolvers;
  49. $disabledResolvers = [];
  50. if ($this->namedResolvers && $attributes = $metadata->getAttributesOfType(ValueResolver::class, $metadata::IS_INSTANCEOF)) {
  51. $resolverName = null;
  52. foreach ($attributes as $attribute) {
  53. if ($attribute->disabled) {
  54. $disabledResolvers[$attribute->resolver] = true;
  55. } elseif ($resolverName) {
  56. throw new \LogicException(\sprintf('You can only pin one resolver per argument, but argument "$%s" of "%s()" has more.', $metadata->getName(), $metadata->getControllerName()));
  57. } else {
  58. $resolverName = $attribute->resolver;
  59. }
  60. }
  61. if ($resolverName) {
  62. if (!$this->namedResolvers->has($resolverName)) {
  63. throw new ResolverNotFoundException($resolverName, $this->namedResolvers instanceof ServiceProviderInterface ? array_keys($this->namedResolvers->getProvidedServices()) : []);
  64. }
  65. $argumentValueResolvers = [
  66. $this->namedResolvers->get($resolverName),
  67. new RequestAttributeValueResolver(),
  68. new DefaultValueResolver(),
  69. ];
  70. }
  71. }
  72. $valueResolverExceptions = [];
  73. foreach ($argumentValueResolvers as $name => $resolver) {
  74. if (isset($disabledResolvers[\is_int($name) ? $resolver::class : $name])) {
  75. continue;
  76. }
  77. try {
  78. $count = 0;
  79. foreach ($resolver->resolve($request, $metadata) as $argument) {
  80. ++$count;
  81. $arguments[] = $argument;
  82. }
  83. } catch (NearMissValueResolverException $e) {
  84. $valueResolverExceptions[] = $e;
  85. }
  86. if (1 < $count && !$metadata->isVariadic()) {
  87. throw new \InvalidArgumentException(\sprintf('"%s::resolve()" must yield at most one value for non-variadic arguments.', get_debug_type($resolver)));
  88. }
  89. if ($count) {
  90. // continue to the next controller argument
  91. continue 2;
  92. }
  93. }
  94. $reasons = array_map(static fn (NearMissValueResolverException $e) => $e->getMessage(), $valueResolverExceptions);
  95. if (!$reasons) {
  96. $reasons[] = 'Either the argument is nullable and no null value has been provided, no default value has been provided or there is a non-optional argument after this one.';
  97. }
  98. $reasonCounter = 1;
  99. if (\count($reasons) > 1) {
  100. foreach ($reasons as $i => $reason) {
  101. $reasons[$i] = $reasonCounter.') '.$reason;
  102. ++$reasonCounter;
  103. }
  104. }
  105. throw new \RuntimeException(\sprintf('Controller "%s" requires the "$%s" argument that could not be resolved. '.($reasonCounter > 1 ? 'Possible reasons: ' : '').'%s', $metadata->getControllerName(), $metadata->getName(), implode(' ', $reasons)));
  106. }
  107. return $arguments;
  108. }
  109. /**
  110. * @return iterable<int, ValueResolverInterface>
  111. */
  112. public static function getDefaultArgumentValueResolvers(): iterable
  113. {
  114. return [
  115. new RequestAttributeValueResolver(),
  116. new RequestValueResolver(),
  117. new SessionValueResolver(),
  118. new DefaultValueResolver(),
  119. new VariadicValueResolver(),
  120. ];
  121. }
  122. }