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 if (class_exists('Symfony\Component\HttpFoundation\HeaderUtils')) {
109 return new UploadedFile(
111 null === $clientFileName ? '' : $clientFileName,
112 $psrUploadedFile->getClientMediaType(),
113 $psrUploadedFile->getError(),
118 return new UploadedFile(
120 null === $clientFileName ? '' : $clientFileName,
121 $psrUploadedFile->getClientMediaType(),
122 $psrUploadedFile->getSize(),
123 $psrUploadedFile->getError(),
129 * Gets a temporary file path.
133 protected function getTemporaryPath()
135 return tempnam(sys_get_temp_dir(), uniqid('symfony', true));
141 public function createResponse(ResponseInterface $psrResponse)
143 $response = new Response(
144 $psrResponse->getBody()->__toString(),
145 $psrResponse->getStatusCode(),
146 $psrResponse->getHeaders()
148 $response->setProtocolVersion($psrResponse->getProtocolVersion());
150 foreach ($psrResponse->getHeader('Set-Cookie') as $cookie) {
151 $response->headers->setCookie($this->createCookie($cookie));
158 * Creates a Cookie instance from a cookie string.
160 * Some snippets have been taken from the Guzzle project: https://github.com/guzzle/guzzle/blob/5.3/src/Cookie/SetCookie.php#L34
162 * @param string $cookie
166 * @throws \InvalidArgumentException
168 private function createCookie($cookie)
170 foreach (explode(';', $cookie) as $part) {
173 $data = explode('=', $part, 2);
175 $value = isset($data[1]) ? trim($data[1], " \n\r\t\0\x0B\"") : null;
177 if (!isset($cookieName)) {
179 $cookieValue = $value;
184 if ('expires' === strtolower($name) && null !== $value) {
185 $cookieExpire = new \DateTime($value);
190 if ('path' === strtolower($name) && null !== $value) {
191 $cookiePath = $value;
196 if ('domain' === strtolower($name) && null !== $value) {
197 $cookieDomain = $value;
202 if ('secure' === strtolower($name)) {
203 $cookieSecure = true;
208 if ('httponly' === strtolower($name)) {
209 $cookieHttpOnly = true;
215 if (!isset($cookieName)) {
216 throw new \InvalidArgumentException('The value of the Set-Cookie header is malformed.');
222 isset($cookieExpire) ? $cookieExpire : 0,
223 isset($cookiePath) ? $cookiePath : '/',
224 isset($cookieDomain) ? $cookieDomain : null,
225 isset($cookieSecure),
226 isset($cookieHttpOnly)