DiactorosFactory.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
  12. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  13. use Symfony\Component\HttpFoundation\File\UploadedFile;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\HttpFoundation\StreamedResponse;
  17. use Zend\Diactoros\Response as DiactorosResponse;
  18. use Zend\Diactoros\ServerRequest;
  19. use Zend\Diactoros\ServerRequestFactory as DiactorosRequestFactory;
  20. use Zend\Diactoros\Stream as DiactorosStream;
  21. use Zend\Diactoros\UploadedFile as DiactorosUploadedFile;
  22. /**
  23. * Builds Psr\HttpMessage instances using the Zend Diactoros implementation.
  24. *
  25. * @author Kévin Dunglas <dunglas@gmail.com>
  26. */
  27. class DiactorosFactory implements HttpMessageFactoryInterface
  28. {
  29. public function __construct()
  30. {
  31. if (!class_exists('Zend\Diactoros\ServerRequestFactory')) {
  32. throw new \RuntimeException('Zend Diactoros must be installed to use the DiactorosFactory.');
  33. }
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function createRequest(Request $symfonyRequest)
  39. {
  40. $server = DiactorosRequestFactory::normalizeServer($symfonyRequest->server->all());
  41. $headers = $symfonyRequest->headers->all();
  42. if (PHP_VERSION_ID < 50600) {
  43. $body = new DiactorosStream('php://temp', 'wb+');
  44. $body->write($symfonyRequest->getContent());
  45. } else {
  46. $body = new DiactorosStream($symfonyRequest->getContent(true));
  47. }
  48. $request = new ServerRequest(
  49. $server,
  50. DiactorosRequestFactory::normalizeFiles($this->getFiles($symfonyRequest->files->all())),
  51. $symfonyRequest->getUri(),
  52. $symfonyRequest->getMethod(),
  53. $body,
  54. $headers
  55. );
  56. $request = $request
  57. ->withCookieParams($symfonyRequest->cookies->all())
  58. ->withQueryParams($symfonyRequest->query->all())
  59. ->withParsedBody($symfonyRequest->request->all())
  60. ;
  61. foreach ($symfonyRequest->attributes->all() as $key => $value) {
  62. $request = $request->withAttribute($key, $value);
  63. }
  64. return $request;
  65. }
  66. /**
  67. * Converts Symfony uploaded files array to the PSR one.
  68. *
  69. * @param array $uploadedFiles
  70. *
  71. * @return array
  72. */
  73. private function getFiles(array $uploadedFiles)
  74. {
  75. $files = array();
  76. foreach ($uploadedFiles as $key => $value) {
  77. if (null === $value) {
  78. $files[$key] = new DiactorosUploadedFile(null, 0, UPLOAD_ERR_NO_FILE, null, null);
  79. continue;
  80. }
  81. if ($value instanceof UploadedFile) {
  82. $files[$key] = $this->createUploadedFile($value);
  83. } else {
  84. $files[$key] = $this->getFiles($value);
  85. }
  86. }
  87. return $files;
  88. }
  89. /**
  90. * Creates a PSR-7 UploadedFile instance from a Symfony one.
  91. *
  92. * @param UploadedFile $symfonyUploadedFile
  93. *
  94. * @return UploadedFileInterface
  95. */
  96. private function createUploadedFile(UploadedFile $symfonyUploadedFile)
  97. {
  98. return new DiactorosUploadedFile(
  99. $symfonyUploadedFile->getRealPath(),
  100. $symfonyUploadedFile->getClientSize(),
  101. $symfonyUploadedFile->getError(),
  102. $symfonyUploadedFile->getClientOriginalName(),
  103. $symfonyUploadedFile->getClientMimeType()
  104. );
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function createResponse(Response $symfonyResponse)
  110. {
  111. if ($symfonyResponse instanceof BinaryFileResponse) {
  112. $stream = new DiactorosStream($symfonyResponse->getFile()->getPathname(), 'r');
  113. } else {
  114. $stream = new DiactorosStream('php://temp', 'wb+');
  115. if ($symfonyResponse instanceof StreamedResponse) {
  116. ob_start(function ($buffer) use ($stream) {
  117. $stream->write($buffer);
  118. return false;
  119. });
  120. $symfonyResponse->sendContent();
  121. ob_end_clean();
  122. } else {
  123. $stream->write($symfonyResponse->getContent());
  124. }
  125. }
  126. $headers = $symfonyResponse->headers->all();
  127. $cookies = $symfonyResponse->headers->getCookies();
  128. if (!empty($cookies)) {
  129. $headers['Set-Cookie'] = array();
  130. foreach ($cookies as $cookie) {
  131. $headers['Set-Cookie'][] = $cookie->__toString();
  132. }
  133. }
  134. $response = new DiactorosResponse(
  135. $stream,
  136. $symfonyResponse->getStatusCode(),
  137. $headers
  138. );
  139. $protocolVersion = $symfonyResponse->getProtocolVersion();
  140. if ('1.1' !== $protocolVersion) {
  141. $response = $response->withProtocolVersion($protocolVersion);
  142. }
  143. return $response;
  144. }
  145. }