RequestValueResolver.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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\ArgumentResolver;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
  13. use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
  14. use Symfony\Component\HttpKernel\Exception\NearMissValueResolverException;
  15. /**
  16. * Yields the same instance as the request object passed along.
  17. *
  18. * @author Iltar van der Berg <kjarli@gmail.com>
  19. */
  20. final class RequestValueResolver implements ValueResolverInterface
  21. {
  22. public function resolve(Request $request, ArgumentMetadata $argument): array
  23. {
  24. if (Request::class === $argument->getType() || is_subclass_of($argument->getType(), Request::class)) {
  25. return [$request];
  26. }
  27. if (str_ends_with($argument->getType() ?? '', '\\Request')) {
  28. throw new NearMissValueResolverException(\sprintf('Looks like you required a Request object with the wrong class name "%s". Did you mean to use "%s" instead?', $argument->getType(), Request::class));
  29. }
  30. return [];
  31. }
  32. }