Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/psr-http-message-bridge/Factory/HttpFoundationFactory.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 129ea1e6d783 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 /* | |
4 * This file is part of the Symfony package. | |
5 * | |
6 * (c) Fabien Potencier <fabien@symfony.com> | |
7 * | |
8 * For the full copyright and license information, please view the LICENSE | |
9 * file that was distributed with this source code. | |
10 */ | |
11 | |
12 namespace Symfony\Bridge\PsrHttpMessage\Factory; | |
13 | |
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; | |
23 | |
24 /** | |
25 * {@inheritdoc} | |
26 * | |
27 * @author Kévin Dunglas <dunglas@gmail.com> | |
28 */ | |
29 class HttpFoundationFactory implements HttpFoundationFactoryInterface | |
30 { | |
31 /** | |
32 * {@inheritdoc} | |
33 */ | |
34 public function createRequest(ServerRequestInterface $psrRequest) | |
35 { | |
36 $server = array(); | |
37 $uri = $psrRequest->getUri(); | |
38 | |
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(); | |
44 } | |
45 | |
46 $server['REQUEST_METHOD'] = $psrRequest->getMethod(); | |
47 | |
48 $server = array_replace($server, $psrRequest->getServerParams()); | |
49 | |
50 $parsedBody = $psrRequest->getParsedBody(); | |
51 $parsedBody = is_array($parsedBody) ? $parsedBody : array(); | |
52 | |
53 $request = new Request( | |
54 $psrRequest->getQueryParams(), | |
55 $parsedBody, | |
56 $psrRequest->getAttributes(), | |
57 $psrRequest->getCookieParams(), | |
58 $this->getFiles($psrRequest->getUploadedFiles()), | |
59 $server, | |
60 $psrRequest->getBody()->__toString() | |
61 ); | |
62 $request->headers->replace($psrRequest->getHeaders()); | |
63 | |
64 return $request; | |
65 } | |
66 | |
67 /** | |
68 * Converts to the input array to $_FILES structure. | |
69 * | |
70 * @param array $uploadedFiles | |
71 * | |
72 * @return array | |
73 */ | |
74 private function getFiles(array $uploadedFiles) | |
75 { | |
76 $files = array(); | |
77 | |
78 foreach ($uploadedFiles as $key => $value) { | |
79 if ($value instanceof UploadedFileInterface) { | |
80 $files[$key] = $this->createUploadedFile($value); | |
81 } else { | |
82 $files[$key] = $this->getFiles($value); | |
83 } | |
84 } | |
85 | |
86 return $files; | |
87 } | |
88 | |
89 /** | |
90 * Creates Symfony UploadedFile instance from PSR-7 ones. | |
91 * | |
92 * @param UploadedFileInterface $psrUploadedFile | |
93 * | |
94 * @return UploadedFile | |
95 */ | |
96 private function createUploadedFile(UploadedFileInterface $psrUploadedFile) | |
97 { | |
98 $temporaryPath = ''; | |
99 $clientFileName = ''; | |
100 if (UPLOAD_ERR_NO_FILE !== $psrUploadedFile->getError()) { | |
101 $temporaryPath = $this->getTemporaryPath(); | |
102 $psrUploadedFile->moveTo($temporaryPath); | |
103 | |
104 $clientFileName = $psrUploadedFile->getClientFilename(); | |
105 } | |
106 | |
107 return new UploadedFile( | |
108 $temporaryPath, | |
109 null === $clientFileName ? '' : $clientFileName, | |
110 $psrUploadedFile->getClientMediaType(), | |
111 $psrUploadedFile->getSize(), | |
112 $psrUploadedFile->getError(), | |
113 true | |
114 ); | |
115 } | |
116 | |
117 /** | |
118 * Gets a temporary file path. | |
119 * | |
120 * @return string | |
121 */ | |
122 protected function getTemporaryPath() | |
123 { | |
124 return tempnam(sys_get_temp_dir(), uniqid('symfony', true)); | |
125 } | |
126 | |
127 /** | |
128 * {@inheritdoc} | |
129 */ | |
130 public function createResponse(ResponseInterface $psrResponse) | |
131 { | |
132 $response = new Response( | |
133 $psrResponse->getBody()->__toString(), | |
134 $psrResponse->getStatusCode(), | |
135 $psrResponse->getHeaders() | |
136 ); | |
137 $response->setProtocolVersion($psrResponse->getProtocolVersion()); | |
138 | |
139 foreach ($psrResponse->getHeader('Set-Cookie') as $cookie) { | |
140 $response->headers->setCookie($this->createCookie($cookie)); | |
141 } | |
142 | |
143 return $response; | |
144 } | |
145 | |
146 /** | |
147 * Creates a Cookie instance from a cookie string. | |
148 * | |
149 * Some snippets have been taken from the Guzzle project: https://github.com/guzzle/guzzle/blob/5.3/src/Cookie/SetCookie.php#L34 | |
150 * | |
151 * @param string $cookie | |
152 * | |
153 * @return Cookie | |
154 * | |
155 * @throws \InvalidArgumentException | |
156 */ | |
157 private function createCookie($cookie) | |
158 { | |
159 foreach (explode(';', $cookie) as $part) { | |
160 $part = trim($part); | |
161 | |
162 $data = explode('=', $part, 2); | |
163 $name = $data[0]; | |
164 $value = isset($data[1]) ? trim($data[1], " \n\r\t\0\x0B\"") : null; | |
165 | |
166 if (!isset($cookieName)) { | |
167 $cookieName = $name; | |
168 $cookieValue = $value; | |
169 | |
170 continue; | |
171 } | |
172 | |
173 if ('expires' === strtolower($name) && null !== $value) { | |
174 $cookieExpire = new \DateTime($value); | |
175 | |
176 continue; | |
177 } | |
178 | |
179 if ('path' === strtolower($name) && null !== $value) { | |
180 $cookiePath = $value; | |
181 | |
182 continue; | |
183 } | |
184 | |
185 if ('domain' === strtolower($name) && null !== $value) { | |
186 $cookieDomain = $value; | |
187 | |
188 continue; | |
189 } | |
190 | |
191 if ('secure' === strtolower($name)) { | |
192 $cookieSecure = true; | |
193 | |
194 continue; | |
195 } | |
196 | |
197 if ('httponly' === strtolower($name)) { | |
198 $cookieHttpOnly = true; | |
199 | |
200 continue; | |
201 } | |
202 } | |
203 | |
204 if (!isset($cookieName)) { | |
205 throw new \InvalidArgumentException('The value of the Set-Cookie header is malformed.'); | |
206 } | |
207 | |
208 return new Cookie( | |
209 $cookieName, | |
210 $cookieValue, | |
211 isset($cookieExpire) ? $cookieExpire : 0, | |
212 isset($cookiePath) ? $cookiePath : '/', | |
213 isset($cookieDomain) ? $cookieDomain : null, | |
214 isset($cookieSecure), | |
215 isset($cookieHttpOnly) | |
216 ); | |
217 } | |
218 } |