暫無描述

ArgumentResolver.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\Controller;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\Controller\ArgumentResolver\DefaultValueResolver;
  13. use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestAttributeValueResolver;
  14. use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestValueResolver;
  15. use Symfony\Component\HttpKernel\Controller\ArgumentResolver\VariadicValueResolver;
  16. use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactory;
  17. use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactoryInterface;
  18. /**
  19. * Responsible for resolving the arguments passed to an action.
  20. *
  21. * @author Iltar van der Berg <kjarli@gmail.com>
  22. */
  23. final class ArgumentResolver implements ArgumentResolverInterface
  24. {
  25. private $argumentMetadataFactory;
  26. /**
  27. * @var ArgumentValueResolverInterface[]
  28. */
  29. private $argumentValueResolvers;
  30. public function __construct(ArgumentMetadataFactoryInterface $argumentMetadataFactory = null, array $argumentValueResolvers = array())
  31. {
  32. $this->argumentMetadataFactory = $argumentMetadataFactory ?: new ArgumentMetadataFactory();
  33. $this->argumentValueResolvers = $argumentValueResolvers ?: self::getDefaultArgumentValueResolvers();
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function getArguments(Request $request, $controller)
  39. {
  40. $arguments = array();
  41. foreach ($this->argumentMetadataFactory->createArgumentMetadata($controller) as $metadata) {
  42. foreach ($this->argumentValueResolvers as $resolver) {
  43. if (!$resolver->supports($request, $metadata)) {
  44. continue;
  45. }
  46. $resolved = $resolver->resolve($request, $metadata);
  47. if (!$resolved instanceof \Generator) {
  48. throw new \InvalidArgumentException(sprintf('%s::resolve() must yield at least one value.', get_class($resolver)));
  49. }
  50. foreach ($resolved as $append) {
  51. $arguments[] = $append;
  52. }
  53. // continue to the next controller argument
  54. continue 2;
  55. }
  56. $representative = $controller;
  57. if (is_array($representative)) {
  58. $representative = sprintf('%s::%s()', get_class($representative[0]), $representative[1]);
  59. } elseif (is_object($representative)) {
  60. $representative = get_class($representative);
  61. }
  62. 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()));
  63. }
  64. return $arguments;
  65. }
  66. public static function getDefaultArgumentValueResolvers()
  67. {
  68. return array(
  69. new RequestAttributeValueResolver(),
  70. new RequestValueResolver(),
  71. new DefaultValueResolver(),
  72. new VariadicValueResolver(),
  73. );
  74. }
  75. }