65289c680658e3684182d4b17acffa5ba843f430
[yaffs-website] / Factory / HttpFoundationFactory.php
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         if (class_exists('Symfony\Component\HttpFoundation\HeaderUtils')) {
108             // Symfony 4.1+
109             return new UploadedFile(
110                 $temporaryPath,
111                 null === $clientFileName ? '' : $clientFileName,
112                 $psrUploadedFile->getClientMediaType(),
113                 $psrUploadedFile->getError(),
114                 true
115             );
116         }
117
118         return new UploadedFile(
119             $temporaryPath,
120             null === $clientFileName ? '' : $clientFileName,
121             $psrUploadedFile->getClientMediaType(),
122             $psrUploadedFile->getSize(),
123             $psrUploadedFile->getError(),
124             true
125         );
126     }
127
128     /**
129      * Gets a temporary file path.
130      *
131      * @return string
132      */
133     protected function getTemporaryPath()
134     {
135         return tempnam(sys_get_temp_dir(), uniqid('symfony', true));
136     }
137
138     /**
139      * {@inheritdoc}
140      */
141     public function createResponse(ResponseInterface $psrResponse)
142     {
143         $response = new Response(
144             $psrResponse->getBody()->__toString(),
145             $psrResponse->getStatusCode(),
146             $psrResponse->getHeaders()
147         );
148         $response->setProtocolVersion($psrResponse->getProtocolVersion());
149
150         foreach ($psrResponse->getHeader('Set-Cookie') as $cookie) {
151             $response->headers->setCookie($this->createCookie($cookie));
152         }
153
154         return $response;
155     }
156
157     /**
158      * Creates a Cookie instance from a cookie string.
159      *
160      * Some snippets have been taken from the Guzzle project: https://github.com/guzzle/guzzle/blob/5.3/src/Cookie/SetCookie.php#L34
161      *
162      * @param string $cookie
163      *
164      * @return Cookie
165      *
166      * @throws \InvalidArgumentException
167      */
168     private function createCookie($cookie)
169     {
170         foreach (explode(';', $cookie) as $part) {
171             $part = trim($part);
172
173             $data = explode('=', $part, 2);
174             $name = $data[0];
175             $value = isset($data[1]) ? trim($data[1], " \n\r\t\0\x0B\"") : null;
176
177             if (!isset($cookieName)) {
178                 $cookieName = $name;
179                 $cookieValue = $value;
180
181                 continue;
182             }
183
184             if ('expires' === strtolower($name) && null !== $value) {
185                 $cookieExpire = new \DateTime($value);
186
187                 continue;
188             }
189
190             if ('path' === strtolower($name) && null !== $value) {
191                 $cookiePath = $value;
192
193                 continue;
194             }
195
196             if ('domain' === strtolower($name) && null !== $value) {
197                 $cookieDomain = $value;
198
199                 continue;
200             }
201
202             if ('secure' === strtolower($name)) {
203                 $cookieSecure = true;
204
205                 continue;
206             }
207
208             if ('httponly' === strtolower($name)) {
209                 $cookieHttpOnly = true;
210
211                 continue;
212             }
213         }
214
215         if (!isset($cookieName)) {
216             throw new \InvalidArgumentException('The value of the Set-Cookie header is malformed.');
217         }
218
219         return new Cookie(
220             $cookieName,
221             $cookieValue,
222             isset($cookieExpire) ? $cookieExpire : 0,
223             isset($cookiePath) ? $cookiePath : '/',
224             isset($cookieDomain) ? $cookieDomain : null,
225             isset($cookieSecure),
226             isset($cookieHttpOnly)
227         );
228     }
229 }