HttpFoundationFactory.php 5.4 KB

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