PsrServerRequestResolver.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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\Bridge\PsrHttpMessage\ArgumentValueResolver;
  11. use Psr\Http\Message\MessageInterface;
  12. use Psr\Http\Message\RequestInterface;
  13. use Psr\Http\Message\ServerRequestInterface;
  14. use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
  17. use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
  18. /**
  19. * Injects the RequestInterface, MessageInterface or ServerRequestInterface when requested.
  20. *
  21. * @author Iltar van der Berg <kjarli@gmail.com>
  22. * @author Alexander M. Turek <me@derrabus.de>
  23. */
  24. final class PsrServerRequestResolver implements ArgumentValueResolverInterface
  25. {
  26. private const SUPPORTED_TYPES = [
  27. ServerRequestInterface::class => true,
  28. RequestInterface::class => true,
  29. MessageInterface::class => true,
  30. ];
  31. private $httpMessageFactory;
  32. public function __construct(HttpMessageFactoryInterface $httpMessageFactory)
  33. {
  34. $this->httpMessageFactory = $httpMessageFactory;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function supports(Request $request, ArgumentMetadata $argument): bool
  40. {
  41. return self::SUPPORTED_TYPES[$argument->getType()] ?? false;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function resolve(Request $request, ArgumentMetadata $argument): \Traversable
  47. {
  48. yield $this->httpMessageFactory->createRequest($request);
  49. }
  50. }