ArgumentResolver
1 year ago
ArgumentResolver.php
1 year ago
ArgumentResolverInterface.php
2 years ago
ArgumentValueResolverInterface.php
2 years ago
ContainerControllerResolver.php
2 years ago
ControllerReference.php
2 years ago
ControllerResolver.php
1 year ago
ControllerResolverInterface.php
2 years ago
ErrorController.php
1 year ago
TraceableArgumentResolver.php
2 years ago
TraceableControllerResolver.php
2 years ago
ArgumentResolver.php
79 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | namespace Matomo\Dependencies\Symfony\Component\HttpKernel\Controller; |
| 12 | |
| 13 | use Matomo\Dependencies\Symfony\Component\HttpFoundation\Request; |
| 14 | use Matomo\Dependencies\Symfony\Component\HttpKernel\Controller\ArgumentResolver\DefaultValueResolver; |
| 15 | use Matomo\Dependencies\Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestAttributeValueResolver; |
| 16 | use Matomo\Dependencies\Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestValueResolver; |
| 17 | use Matomo\Dependencies\Symfony\Component\HttpKernel\Controller\ArgumentResolver\SessionValueResolver; |
| 18 | use Matomo\Dependencies\Symfony\Component\HttpKernel\Controller\ArgumentResolver\VariadicValueResolver; |
| 19 | use Matomo\Dependencies\Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactory; |
| 20 | use Matomo\Dependencies\Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactoryInterface; |
| 21 | /** |
| 22 | * Responsible for resolving the arguments passed to an action. |
| 23 | * |
| 24 | * @author Iltar van der Berg <kjarli@gmail.com> |
| 25 | */ |
| 26 | final class ArgumentResolver implements ArgumentResolverInterface |
| 27 | { |
| 28 | private $argumentMetadataFactory; |
| 29 | private $argumentValueResolvers; |
| 30 | /** |
| 31 | * @param iterable<mixed, ArgumentValueResolverInterface> $argumentValueResolvers |
| 32 | */ |
| 33 | public function __construct(?ArgumentMetadataFactoryInterface $argumentMetadataFactory = null, iterable $argumentValueResolvers = []) |
| 34 | { |
| 35 | $this->argumentMetadataFactory = $argumentMetadataFactory ?? new ArgumentMetadataFactory(); |
| 36 | $this->argumentValueResolvers = $argumentValueResolvers ?: self::getDefaultArgumentValueResolvers(); |
| 37 | } |
| 38 | /** |
| 39 | * {@inheritdoc} |
| 40 | */ |
| 41 | public function getArguments(Request $request, callable $controller) : array |
| 42 | { |
| 43 | $arguments = []; |
| 44 | foreach ($this->argumentMetadataFactory->createArgumentMetadata($controller) as $metadata) { |
| 45 | foreach ($this->argumentValueResolvers as $resolver) { |
| 46 | if (!$resolver->supports($request, $metadata)) { |
| 47 | continue; |
| 48 | } |
| 49 | $resolved = $resolver->resolve($request, $metadata); |
| 50 | $atLeastOne = \false; |
| 51 | foreach ($resolved as $append) { |
| 52 | $atLeastOne = \true; |
| 53 | $arguments[] = $append; |
| 54 | } |
| 55 | if (!$atLeastOne) { |
| 56 | throw new \InvalidArgumentException(sprintf('"%s::resolve()" must yield at least one value.', get_debug_type($resolver))); |
| 57 | } |
| 58 | // continue to the next controller argument |
| 59 | continue 2; |
| 60 | } |
| 61 | $representative = $controller; |
| 62 | if (\is_array($representative)) { |
| 63 | $representative = sprintf('%s::%s()', \get_class($representative[0]), $representative[1]); |
| 64 | } elseif (\is_object($representative)) { |
| 65 | $representative = \get_class($representative); |
| 66 | } |
| 67 | throw new \RuntimeException(sprintf('Controller "%s" requires that you provide a value for the "$%s" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.', $representative, $metadata->getName())); |
| 68 | } |
| 69 | return $arguments; |
| 70 | } |
| 71 | /** |
| 72 | * @return iterable<int, ArgumentValueResolverInterface> |
| 73 | */ |
| 74 | public static function getDefaultArgumentValueResolvers() : iterable |
| 75 | { |
| 76 | return [new RequestAttributeValueResolver(), new RequestValueResolver(), new SessionValueResolver(), new DefaultValueResolver(), new VariadicValueResolver()]; |
| 77 | } |
| 78 | } |
| 79 |