4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Bridge\PsrHttpMessage\Factory;
14 use Psr\Http\Message\ServerRequestInterface;
15 use Psr\Http\Message\ResponseInterface;
16 use Psr\Http\Message\UploadedFileInterface;
17 use Psr\Http\Message\UriInterface;
18 use Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface;
19 use Symfony\Component\HttpFoundation\Cookie;
20 use Symfony\Component\HttpFoundation\File\UploadedFile;
21 use Symfony\Component\HttpFoundation\Request;
22 use Symfony\Component\HttpFoundation\Response;
27 * @author Kévin Dunglas <dunglas@gmail.com>
29 class HttpFoundationFactory implements HttpFoundationFactoryInterface
34 public function createRequest(ServerRequestInterface $psrRequest)
37 $uri = $psrRequest->getUri();
39 if ($uri instanceof UriInterface) {
40 $server['SERVER_NAME'] = $uri->getHost();
41 $server['SERVER_PORT'] = $uri->getPort();
42 $server['REQUEST_URI'] = $uri->getPath();
43 $server['QUERY_STRING'] = $uri->getQuery();
46 $server['REQUEST_METHOD'] = $psrRequest->getMethod();
48 $server = array_replace($server, $psrRequest->getServerParams());
50 $parsedBody = $psrRequest->getParsedBody();
51 $parsedBody = is_array($parsedBody) ? $parsedBody : array();
53 $request = new Request(
54 $psrRequest->getQueryParams(),
56 $psrRequest->getAttributes(),
57 $psrRequest->getCookieParams(),
58 $this->getFiles($psrRequest->getUploadedFiles()),
60 $psrRequest->getBody()->__toString()
62 $request->headers->replace($psrRequest->getHeaders());
68 * Converts to the input array to $_FILES structure.
70 * @param array $uploadedFiles
74 private function getFiles(array $uploadedFiles)
78 foreach ($uploadedFiles as $key => $value) {
79 if ($value instanceof UploadedFileInterface) {
80 $files[$key] = $this->createUploadedFile($value);
82 $files[$key] = $this->getFiles($value);
90 * Creates Symfony UploadedFile instance from PSR-7 ones.
92 * @param UploadedFileInterface $psrUploadedFile
94 * @return UploadedFile
96 private function createUploadedFile(UploadedFileInterface $psrUploadedFile)
100 if (UPLOAD_ERR_NO_FILE !== $psrUploadedFile->getError()) {
101 $temporaryPath = $this->getTemporaryPath();
102 $psrUploadedFile->moveTo($temporaryPath);
104 $clientFileName = $psrUploadedFile->getClientFilename();
107 return new UploadedFile(
109 null === $clientFileName ? '' : $clientFileName,
110 $psrUploadedFile->getClientMediaType(),
111 $psrUploadedFile->getSize(),
112 $psrUploadedFile->getError(),
118 * Gets a temporary file path.
122 protected function getTemporaryPath()
124 return tempnam(sys_get_temp_dir(), uniqid('symfony', true));
130 public function createResponse(ResponseInterface $psrResponse)
132 $response = new Response(
133 $psrResponse->getBody()->__toString(),
134 $psrResponse->getStatusCode(),
135 $psrResponse->getHeaders()
137 $response->setProtocolVersion($psrResponse->getProtocolVersion());
139 foreach ($psrResponse->getHeader('Set-Cookie') as $cookie) {
140 $response->headers->setCookie($this->createCookie($cookie));
147 * Creates a Cookie instance from a cookie string.
149 * Some snippets have been taken from the Guzzle project: https://github.com/guzzle/guzzle/blob/5.3/src/Cookie/SetCookie.php#L34
151 * @param string $cookie
155 * @throws \InvalidArgumentException
157 private function createCookie($cookie)
159 foreach (explode(';', $cookie) as $part) {
162 $data = explode('=', $part, 2);
164 $value = isset($data[1]) ? trim($data[1], " \n\r\t\0\x0B\"") : null;
166 if (!isset($cookieName)) {
168 $cookieValue = $value;
173 if ('expires' === strtolower($name) && null !== $value) {
174 $cookieExpire = new \DateTime($value);
179 if ('path' === strtolower($name) && null !== $value) {
180 $cookiePath = $value;
185 if ('domain' === strtolower($name) && null !== $value) {
186 $cookieDomain = $value;
191 if ('secure' === strtolower($name)) {
192 $cookieSecure = true;
197 if ('httponly' === strtolower($name)) {
198 $cookieHttpOnly = true;
204 if (!isset($cookieName)) {
205 throw new \InvalidArgumentException('The value of the Set-Cookie header is malformed.');
211 isset($cookieExpire) ? $cookieExpire : 0,
212 isset($cookiePath) ? $cookiePath : '/',
213 isset($cookieDomain) ? $cookieDomain : null,
214 isset($cookieSecure),
215 isset($cookieHttpOnly)