Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / http-kernel / Controller / ArgumentResolver.php
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\VariadicValueResolver;
19 use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactory;
20 use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactoryInterface;
21
22 /**
23  * Responsible for resolving the arguments passed to an action.
24  *
25  * @author Iltar van der Berg <kjarli@gmail.com>
26  */
27 final class ArgumentResolver implements ArgumentResolverInterface
28 {
29     private $argumentMetadataFactory;
30
31     /**
32      * @var ArgumentValueResolverInterface[]
33      */
34     private $argumentValueResolvers;
35
36     public function __construct(ArgumentMetadataFactoryInterface $argumentMetadataFactory = null, array $argumentValueResolvers = array())
37     {
38         $this->argumentMetadataFactory = $argumentMetadataFactory ?: new ArgumentMetadataFactory();
39         $this->argumentValueResolvers = $argumentValueResolvers ?: self::getDefaultArgumentValueResolvers();
40     }
41
42     /**
43      * {@inheritdoc}
44      */
45     public function getArguments(Request $request, $controller)
46     {
47         $arguments = array();
48
49         foreach ($this->argumentMetadataFactory->createArgumentMetadata($controller) as $metadata) {
50             foreach ($this->argumentValueResolvers as $resolver) {
51                 if (!$resolver->supports($request, $metadata)) {
52                     continue;
53                 }
54
55                 $resolved = $resolver->resolve($request, $metadata);
56
57                 if (!$resolved instanceof \Generator) {
58                     throw new \InvalidArgumentException(sprintf('%s::resolve() must yield at least one value.', get_class($resolver)));
59                 }
60
61                 foreach ($resolved as $append) {
62                     $arguments[] = $append;
63                 }
64
65                 // continue to the next controller argument
66                 continue 2;
67             }
68
69             $representative = $controller;
70
71             if (is_array($representative)) {
72                 $representative = sprintf('%s::%s()', get_class($representative[0]), $representative[1]);
73             } elseif (is_object($representative)) {
74                 $representative = get_class($representative);
75             }
76
77             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()));
78         }
79
80         return $arguments;
81     }
82
83     public static function getDefaultArgumentValueResolvers()
84     {
85         return array(
86             new RequestAttributeValueResolver(),
87             new RequestValueResolver(),
88             new DefaultValueResolver(),
89             new VariadicValueResolver(),
90         );
91     }
92 }