PsrHttpFactory.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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\Factory;
  11. use Psr\Http\Message\ResponseFactoryInterface;
  12. use Psr\Http\Message\ServerRequestFactoryInterface;
  13. use Psr\Http\Message\StreamFactoryInterface;
  14. use Psr\Http\Message\UploadedFileFactoryInterface;
  15. use Psr\Http\Message\UploadedFileInterface;
  16. use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
  17. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  18. use Symfony\Component\HttpFoundation\File\UploadedFile;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\HttpFoundation\StreamedResponse;
  22. /**
  23. * Builds Psr\HttpMessage instances using a PSR-17 implementation.
  24. *
  25. * @author Antonio J. García Lagar <aj@garcialagar.es>
  26. */
  27. class PsrHttpFactory implements HttpMessageFactoryInterface
  28. {
  29. private $serverRequestFactory;
  30. private $streamFactory;
  31. private $uploadedFileFactory;
  32. private $responseFactory;
  33. public function __construct(ServerRequestFactoryInterface $serverRequestFactory, StreamFactoryInterface $streamFactory, UploadedFileFactoryInterface $uploadedFileFactory, ResponseFactoryInterface $responseFactory)
  34. {
  35. $this->serverRequestFactory = $serverRequestFactory;
  36. $this->streamFactory = $streamFactory;
  37. $this->uploadedFileFactory = $uploadedFileFactory;
  38. $this->responseFactory = $responseFactory;
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function createRequest(Request $symfonyRequest)
  44. {
  45. $request = $this->serverRequestFactory->createServerRequest(
  46. $symfonyRequest->getMethod(),
  47. $symfonyRequest->getSchemeAndHttpHost().$symfonyRequest->getRequestUri(),
  48. $symfonyRequest->server->all()
  49. );
  50. foreach ($symfonyRequest->headers->all() as $name => $value) {
  51. $request = $request->withHeader($name, $value);
  52. }
  53. if (PHP_VERSION_ID < 50600) {
  54. $body = $this->streamFactory->createStreamFromFile('php://temp', 'wb+');
  55. $body->write($symfonyRequest->getContent());
  56. } else {
  57. $body = $this->streamFactory->createStreamFromResource($symfonyRequest->getContent(true));
  58. }
  59. $request = $request
  60. ->withBody($body)
  61. ->withUploadedFiles($this->getFiles($symfonyRequest->files->all()))
  62. ->withCookieParams($symfonyRequest->cookies->all())
  63. ->withQueryParams($symfonyRequest->query->all())
  64. ->withParsedBody($symfonyRequest->request->all())
  65. ;
  66. foreach ($symfonyRequest->attributes->all() as $key => $value) {
  67. $request = $request->withAttribute($key, $value);
  68. }
  69. return $request;
  70. }
  71. /**
  72. * Converts Symfony uploaded files array to the PSR one.
  73. *
  74. * @param array $uploadedFiles
  75. *
  76. * @return array
  77. */
  78. private function getFiles(array $uploadedFiles)
  79. {
  80. $files = array();
  81. foreach ($uploadedFiles as $key => $value) {
  82. if (null === $value) {
  83. $files[$key] = $this->uploadedFileFactory->createUploadedFile($this->streamFactory->createStream(), 0, UPLOAD_ERR_NO_FILE);
  84. continue;
  85. }
  86. if ($value instanceof UploadedFile) {
  87. $files[$key] = $this->createUploadedFile($value);
  88. } else {
  89. $files[$key] = $this->getFiles($value);
  90. }
  91. }
  92. return $files;
  93. }
  94. /**
  95. * Creates a PSR-7 UploadedFile instance from a Symfony one.
  96. *
  97. * @param UploadedFile $symfonyUploadedFile
  98. *
  99. * @return UploadedFileInterface
  100. */
  101. private function createUploadedFile(UploadedFile $symfonyUploadedFile)
  102. {
  103. return $this->uploadedFileFactory->createUploadedFile(
  104. $this->streamFactory->createStreamFromFile(
  105. $symfonyUploadedFile->getRealPath()
  106. ),
  107. (int) $symfonyUploadedFile->getSize(),
  108. $symfonyUploadedFile->getError(),
  109. $symfonyUploadedFile->getClientOriginalName(),
  110. $symfonyUploadedFile->getClientMimeType()
  111. );
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function createResponse(Response $symfonyResponse)
  117. {
  118. $response = $this->responseFactory->createResponse($symfonyResponse->getStatusCode());
  119. if ($symfonyResponse instanceof BinaryFileResponse) {
  120. $stream = $this->streamFactory->createStreamFromFile(
  121. $symfonyResponse->getFile()->getPathname()
  122. );
  123. } else {
  124. $stream = $this->streamFactory->createStreamFromFile('php://temp', 'wb+');
  125. if ($symfonyResponse instanceof StreamedResponse) {
  126. ob_start(function ($buffer) use ($stream) {
  127. $stream->write($buffer);
  128. return '';
  129. });
  130. $symfonyResponse->sendContent();
  131. ob_end_clean();
  132. } else {
  133. $stream->write($symfonyResponse->getContent());
  134. }
  135. }
  136. $response = $response->withBody($stream);
  137. $headers = $symfonyResponse->headers->all();
  138. $cookies = $symfonyResponse->headers->getCookies();
  139. if (!empty($cookies)) {
  140. $headers['Set-Cookie'] = array();
  141. foreach ($cookies as $cookie) {
  142. $headers['Set-Cookie'][] = $cookie->__toString();
  143. }
  144. }
  145. foreach ($headers as $name => $value) {
  146. $response = $response->withHeader($name, $value);
  147. }
  148. $protocolVersion = $symfonyResponse->getProtocolVersion();
  149. $response = $response->withProtocolVersion($protocolVersion);
  150. return $response;
  151. }
  152. }