Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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
 
12
namespace Symfony\Component\HttpKernel\Controller;
13
 
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\DefaultValueResolver;
16
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestAttributeValueResolver;
17
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestValueResolver;
18
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\SessionValueResolver;
19
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\TraceableValueResolver;
20
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\VariadicValueResolver;
21
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactory;
22
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactoryInterface;
23
 
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
    /**
35
     * @param iterable<mixed, ArgumentValueResolverInterface|ValueResolverInterface> $argumentValueResolvers
36
     */
37
    public function __construct(ArgumentMetadataFactoryInterface $argumentMetadataFactory = null, iterable $argumentValueResolvers = [])
38
    {
39
        $this->argumentMetadataFactory = $argumentMetadataFactory ?? new ArgumentMetadataFactory();
40
        $this->argumentValueResolvers = $argumentValueResolvers ?: self::getDefaultArgumentValueResolvers();
41
    }
42
 
43
    public function getArguments(Request $request, callable $controller, \ReflectionFunctionAbstract $reflector = null): array
44
    {
45
        $arguments = [];
46
 
47
        foreach ($this->argumentMetadataFactory->createArgumentMetadata($controller, $reflector) as $metadata) {
48
            foreach ($this->argumentValueResolvers as $resolver) {
49
                if ((!$resolver instanceof ValueResolverInterface || $resolver instanceof TraceableValueResolver) && !$resolver->supports($request, $metadata)) {
50
                    continue;
51
                }
52
 
53
                $count = 0;
54
                foreach ($resolver->resolve($request, $metadata) as $argument) {
55
                    ++$count;
56
                    $arguments[] = $argument;
57
                }
58
 
59
                if (1 < $count && !$metadata->isVariadic()) {
60
                    throw new \InvalidArgumentException(sprintf('"%s::resolve()" must yield at most one value for non-variadic arguments.', get_debug_type($resolver)));
61
                }
62
 
63
                if ($count) {
64
                    // continue to the next controller argument
65
                    continue 2;
66
                }
67
 
68
                if (!$resolver instanceof ValueResolverInterface) {
69
                    throw new \InvalidArgumentException(sprintf('"%s::resolve()" must yield at least one value.', get_debug_type($resolver)));
70
                }
71
            }
72
 
73
            $representative = $controller;
74
 
75
            if (\is_array($representative)) {
76
                $representative = sprintf('%s::%s()', \get_class($representative[0]), $representative[1]);
77
            } elseif (\is_object($representative)) {
78
                $representative = get_debug_type($representative);
79
            }
80
 
81
            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()));
82
        }
83
 
84
        return $arguments;
85
    }
86
 
87
    /**
88
     * @return iterable<int, ArgumentValueResolverInterface>
89
     */
90
    public static function getDefaultArgumentValueResolvers(): iterable
91
    {
92
        return [
93
            new RequestAttributeValueResolver(),
94
            new RequestValueResolver(),
95
            new SessionValueResolver(),
96
            new DefaultValueResolver(),
97
            new VariadicValueResolver(),
98
        ];
99
    }
100
}