comparison vendor/symfony/http-kernel/Controller/ArgumentResolver.php @ 0:c75dbcec494b

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