4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Controller;
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;
24 * Responsible for resolving the arguments passed to an action.
26 * @author Iltar van der Berg <kjarli@gmail.com>
28 final class ArgumentResolver implements ArgumentResolverInterface
30 private $argumentMetadataFactory;
33 * @var iterable|ArgumentValueResolverInterface[]
35 private $argumentValueResolvers;
37 public function __construct(ArgumentMetadataFactoryInterface $argumentMetadataFactory = null, $argumentValueResolvers = array())
39 $this->argumentMetadataFactory = $argumentMetadataFactory ?: new ArgumentMetadataFactory();
40 $this->argumentValueResolvers = $argumentValueResolvers ?: self::getDefaultArgumentValueResolvers();
46 public function getArguments(Request $request, $controller)
50 foreach ($this->argumentMetadataFactory->createArgumentMetadata($controller) as $metadata) {
51 foreach ($this->argumentValueResolvers as $resolver) {
52 if (!$resolver->supports($request, $metadata)) {
56 $resolved = $resolver->resolve($request, $metadata);
58 if (!$resolved instanceof \Generator) {
59 throw new \InvalidArgumentException(sprintf('%s::resolve() must yield at least one value.', get_class($resolver)));
62 foreach ($resolved as $append) {
63 $arguments[] = $append;
66 // continue to the next controller argument
70 $representative = $controller;
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);
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()));
84 public static function getDefaultArgumentValueResolvers()
87 new RequestAttributeValueResolver(),
88 new RequestValueResolver(),
89 new SessionValueResolver(),
90 new DefaultValueResolver(),
91 new VariadicValueResolver(),