HttpFoundationFactory.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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\ServerRequestInterface;
  12. use Psr\Http\Message\ResponseInterface;
  13. use Psr\Http\Message\UploadedFileInterface;
  14. use Psr\Http\Message\UriInterface;
  15. use Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface;
  16. use Symfony\Component\HttpFoundation\Cookie;
  17. use Symfony\Component\HttpFoundation\File\UploadedFile;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. /**
  21. * {@inheritdoc}
  22. *
  23. * @author Kévin Dunglas <dunglas@gmail.com>
  24. */
  25. class HttpFoundationFactory implements HttpFoundationFactoryInterface
  26. {
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function createRequest(ServerRequestInterface $psrRequest)
  31. {
  32. $server = array();
  33. $uri = $psrRequest->getUri();
  34. if ($uri instanceof UriInterface) {
  35. $server['SERVER_NAME'] = $uri->getHost();
  36. $server['SERVER_PORT'] = $uri->getPort();
  37. $server['REQUEST_URI'] = $uri->getPath();
  38. $server['QUERY_STRING'] = $uri->getQuery();
  39. }
  40. $server['REQUEST_METHOD'] = $psrRequest->getMethod();
  41. $server = array_replace($server, $psrRequest->getServerParams());
  42. $parsedBody = $psrRequest->getParsedBody();
  43. $parsedBody = is_array($parsedBody) ? $parsedBody : array();
  44. $request = new Request(
  45. $psrRequest->getQueryParams(),
  46. $parsedBody,
  47. $psrRequest->getAttributes(),
  48. $psrRequest->getCookieParams(),
  49. $this->getFiles($psrRequest->getUploadedFiles()),
  50. $server,
  51. $psrRequest->getBody()->__toString()
  52. );
  53. $request->headers->replace($psrRequest->getHeaders());
  54. return $request;
  55. }
  56. /**
  57. * Converts to the input array to $_FILES structure.
  58. *
  59. * @param array $uploadedFiles
  60. *
  61. * @return array
  62. */
  63. private function getFiles(array $uploadedFiles)
  64. {
  65. $files = array();
  66. foreach ($uploadedFiles as $key => $value) {
  67. if ($value instanceof UploadedFileInterface) {
  68. $files[$key] = $this->createUploadedFile($value);
  69. } else {
  70. $files[$key] = $this->getFiles($value);
  71. }
  72. }
  73. return $files;
  74. }
  75. /**
  76. * Creates Symfony UploadedFile instance from PSR-7 ones.
  77. *
  78. * @param UploadedFileInterface $psrUploadedFile
  79. *
  80. * @return UploadedFile
  81. */
  82. private function createUploadedFile(UploadedFileInterface $psrUploadedFile)
  83. {
  84. $temporaryPath = '';
  85. $clientFileName = '';
  86. if (UPLOAD_ERR_NO_FILE !== $psrUploadedFile->getError()) {
  87. $temporaryPath = $this->getTemporaryPath();
  88. $psrUploadedFile->moveTo($temporaryPath);
  89. $clientFileName = $psrUploadedFile->getClientFilename();
  90. }
  91. if (class_exists('Symfony\Component\HttpFoundation\HeaderUtils')) {
  92. // Symfony 4.1+
  93. return new UploadedFile(
  94. $temporaryPath,
  95. null === $clientFileName ? '' : $clientFileName,
  96. $psrUploadedFile->getClientMediaType(),
  97. $psrUploadedFile->getError(),
  98. true
  99. );
  100. }
  101. return new UploadedFile(
  102. $temporaryPath,
  103. null === $clientFileName ? '' : $clientFileName,
  104. $psrUploadedFile->getClientMediaType(),
  105. $psrUploadedFile->getSize(),
  106. $psrUploadedFile->getError(),
  107. true
  108. );
  109. }
  110. /**
  111. * Gets a temporary file path.
  112. *
  113. * @return string
  114. */
  115. protected function getTemporaryPath()
  116. {
  117. return tempnam(sys_get_temp_dir(), uniqid('symfony', true));
  118. }
  119. /**
  120. * {@inheritdoc}
  121. */
  122. public function createResponse(ResponseInterface $psrResponse)
  123. {
  124. $response = new Response(
  125. $psrResponse->getBody()->__toString(),
  126. $psrResponse->getStatusCode(),
  127. $psrResponse->getHeaders()
  128. );
  129. $response->setProtocolVersion($psrResponse->getProtocolVersion());
  130. foreach ($psrResponse->getHeader('Set-Cookie') as $cookie) {
  131. $response->headers->setCookie($this->createCookie($cookie));
  132. }
  133. return $response;
  134. }
  135. /**
  136. * Creates a Cookie instance from a cookie string.
  137. *
  138. * Some snippets have been taken from the Guzzle project: https://github.com/guzzle/guzzle/blob/5.3/src/Cookie/SetCookie.php#L34
  139. *
  140. * @param string $cookie
  141. *
  142. * @return Cookie
  143. *
  144. * @throws \InvalidArgumentException
  145. */
  146. private function createCookie($cookie)
  147. {
  148. foreach (explode(';', $cookie) as $part) {
  149. $part = trim($part);
  150. $data = explode('=', $part, 2);
  151. $name = $data[0];
  152. $value = isset($data[1]) ? trim($data[1], " \n\r\t\0\x0B\"") : null;
  153. if (!isset($cookieName)) {
  154. $cookieName = $name;
  155. $cookieValue = $value;
  156. continue;
  157. }
  158. if ('expires' === strtolower($name) && null !== $value) {
  159. $cookieExpire = new \DateTime($value);
  160. continue;
  161. }
  162. if ('path' === strtolower($name) && null !== $value) {
  163. $cookiePath = $value;
  164. continue;
  165. }
  166. if ('domain' === strtolower($name) && null !== $value) {
  167. $cookieDomain = $value;
  168. continue;
  169. }
  170. if ('secure' === strtolower($name)) {
  171. $cookieSecure = true;
  172. continue;
  173. }
  174. if ('httponly' === strtolower($name)) {
  175. $cookieHttpOnly = true;
  176. continue;
  177. }
  178. }
  179. if (!isset($cookieName)) {
  180. throw new \InvalidArgumentException('The value of the Set-Cookie header is malformed.');
  181. }
  182. return new Cookie(
  183. $cookieName,
  184. $cookieValue,
  185. isset($cookieExpire) ? $cookieExpire : 0,
  186. isset($cookiePath) ? $cookiePath : '/',
  187. isset($cookieDomain) ? $cookieDomain : null,
  188. isset($cookieSecure),
  189. isset($cookieHttpOnly)
  190. );
  191. }
  192. }