PsrServerRequestResolver.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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\Controller\ValueResolverInterface as BaseValueResolverInterface;
  18. use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
  19. /**
  20. * Injects the RequestInterface, MessageInterface or ServerRequestInterface when requested.
  21. *
  22. * @author Iltar van der Berg <kjarli@gmail.com>
  23. * @author Alexander M. Turek <me@derrabus.de>
  24. */
  25. final class PsrServerRequestResolver implements ArgumentValueResolverInterface, ValueResolverInterface
  26. {
  27. private const SUPPORTED_TYPES = [
  28. ServerRequestInterface::class => true,
  29. RequestInterface::class => true,
  30. MessageInterface::class => true,
  31. ];
  32. private $httpMessageFactory;
  33. public function __construct(HttpMessageFactoryInterface $httpMessageFactory)
  34. {
  35. $this->httpMessageFactory = $httpMessageFactory;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function supports(Request $request, ArgumentMetadata $argument): bool
  41. {
  42. if ($this instanceof BaseValueResolverInterface) {
  43. trigger_deprecation('symfony/psr-http-message-bridge', '2.3', 'Method "%s" is deprecated, call "resolve()" without calling "supports()" first.', __METHOD__);
  44. }
  45. return self::SUPPORTED_TYPES[$argument->getType()] ?? false;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function resolve(Request $request, ArgumentMetadata $argument): \Traversable
  51. {
  52. if (!isset(self::SUPPORTED_TYPES[$argument->getType()])) {
  53. return;
  54. }
  55. yield $this->httpMessageFactory->createRequest($request);
  56. }
  57. }