PsrRequestController.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace Symfony\Bridge\PsrHttpMessage\Tests\Fixtures\App\Controller;
  3. use Psr\Http\Message\MessageInterface;
  4. use Psr\Http\Message\RequestInterface;
  5. use Psr\Http\Message\ResponseFactoryInterface;
  6. use Psr\Http\Message\ResponseInterface;
  7. use Psr\Http\Message\ServerRequestInterface;
  8. use Psr\Http\Message\StreamFactoryInterface;
  9. final class PsrRequestController
  10. {
  11. private $responseFactory;
  12. private $streamFactory;
  13. public function __construct(ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory)
  14. {
  15. $this->responseFactory = $responseFactory;
  16. $this->streamFactory = $streamFactory;
  17. }
  18. public function serverRequestAction(ServerRequestInterface $request): ResponseInterface
  19. {
  20. return $this->responseFactory
  21. ->createResponse()
  22. ->withBody($this->streamFactory->createStream(sprintf('<html><body>%s</body></html>', $request->getMethod())));
  23. }
  24. public function requestAction(RequestInterface $request): ResponseInterface
  25. {
  26. return $this->responseFactory
  27. ->createResponse()
  28. ->withStatus(403)
  29. ->withBody($this->streamFactory->createStream(sprintf('<html><body>%s %s</body></html>', $request->getMethod(), $request->getBody()->getContents())));
  30. }
  31. public function messageAction(MessageInterface $request): ResponseInterface
  32. {
  33. return $this->responseFactory
  34. ->createResponse()
  35. ->withStatus(422)
  36. ->withBody($this->streamFactory->createStream(sprintf('<html><body>%s</body></html>', $request->getHeader('X-My-Header')[0])));
  37. }
  38. }