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\VariadicValueResolver;
19 use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactory;
20 use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactoryInterface;
23 * Responsible for resolving the arguments passed to an action.
25 * @author Iltar van der Berg <kjarli@gmail.com>
27 final class ArgumentResolver implements ArgumentResolverInterface
29 private $argumentMetadataFactory;
32 * @var ArgumentValueResolverInterface[]
34 private $argumentValueResolvers;
36 public function __construct(ArgumentMetadataFactoryInterface $argumentMetadataFactory = null, array $argumentValueResolvers = array())
38 $this->argumentMetadataFactory = $argumentMetadataFactory ?: new ArgumentMetadataFactory();
39 $this->argumentValueResolvers = $argumentValueResolvers ?: self::getDefaultArgumentValueResolvers();
45 public function getArguments(Request $request, $controller)
49 foreach ($this->argumentMetadataFactory->createArgumentMetadata($controller) as $metadata) {
50 foreach ($this->argumentValueResolvers as $resolver) {
51 if (!$resolver->supports($request, $metadata)) {
55 $resolved = $resolver->resolve($request, $metadata);
57 if (!$resolved instanceof \Generator) {
58 throw new \InvalidArgumentException(sprintf('%s::resolve() must yield at least one value.', get_class($resolver)));
61 foreach ($resolved as $append) {
62 $arguments[] = $append;
65 // continue to the next controller argument
69 $representative = $controller;
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);
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()));
83 public static function getDefaultArgumentValueResolvers()
86 new RequestAttributeValueResolver(),
87 new RequestValueResolver(),
88 new DefaultValueResolver(),
89 new VariadicValueResolver(),