@@ -18,154 +18,154 @@ |
||
18 | 18 | */ |
19 | 19 | class Session implements \IteratorAggregate |
20 | 20 | { |
21 | - use SingletonTrait; |
|
22 | - |
|
23 | - /** @var array The session. */ |
|
24 | - private $session; |
|
25 | - |
|
26 | - /** |
|
27 | - * Construct a Sesssion object. |
|
28 | - * |
|
29 | - * @global array $_SESSION The session parameters. |
|
30 | - */ |
|
31 | - protected function __construct() |
|
32 | - { |
|
33 | - $this->session = &$_SESSION; |
|
34 | - } |
|
35 | - |
|
36 | - /** |
|
37 | - * Initialize the session, if none exists. |
|
38 | - * |
|
39 | - * @param $name = null |
|
40 | - * @param $domain bool |
|
41 | - * @return null |
|
42 | - * @throws \RuntimeException on failure. |
|
43 | - */ |
|
44 | - public static function start($name = null, $domain = false) |
|
45 | - { |
|
46 | - if (session_status() !== PHP_SESSION_NONE) { |
|
47 | - throw new \RuntimeException('Can\'t start a new session.'); |
|
48 | - } |
|
49 | - if ($name !== null) { |
|
50 | - session_name($name); |
|
51 | - } |
|
52 | - if ($domain){ |
|
53 | - preg_match("/[^\.\/]+\.[^\.\/]+$/", $_SERVER['HTTP_HOST'], $matches); |
|
54 | - session_set_cookie_params(0, '/', $matches[0]); |
|
55 | - } |
|
56 | - session_start(); |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * Destroy the session, if one exists. |
|
61 | - * |
|
62 | - * @return null |
|
63 | - * @throws \RuntimeException on failure. |
|
64 | - */ |
|
65 | - public static function destroy() |
|
66 | - { |
|
67 | - if (session_status() !== PHP_SESSION_ACTIVE) { |
|
68 | - throw new \RuntimeException('Can\'t destroy the session. There is no active session.'); |
|
69 | - } |
|
70 | - |
|
71 | - if (!session_destroy()) { |
|
72 | - throw new \RuntimeException('Failed to destroy the active session.'); |
|
73 | - } |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * {@inheritdoc} |
|
78 | - */ |
|
79 | - public function getIterator() |
|
80 | - { |
|
81 | - return new \ArrayIterator(static::getInstance()->session); |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Returns the number of key-value mappings in the session map. |
|
86 | - * |
|
87 | - * @return int the number of key-value mappings in the session map. |
|
88 | - */ |
|
89 | - public static function count() |
|
90 | - { |
|
91 | - return count(static::getInstance()->session); |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * Returns true if the session map contains no key-value mappings. |
|
96 | - * |
|
97 | - * @return bool true if the session map contains no key-value mappings. |
|
98 | - */ |
|
99 | - public static function isEmpty() |
|
100 | - { |
|
101 | - return empty(static::getInstance()->session); |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Returns true if the session map contains a mapping for the specified key. |
|
106 | - * |
|
107 | - * @param string $key |
|
108 | - * @return bool true if the session map contains a mapping for the specified key. |
|
109 | - */ |
|
110 | - public static function containsKey($key) |
|
111 | - { |
|
112 | - return isset(static::getInstance()->session[$key]); |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Returns true if the session map maps one or more keys to the specified value. |
|
117 | - * |
|
118 | - * @param string $value |
|
119 | - * @return bool true if the session map maps one or more keys to the specified value. |
|
120 | - */ |
|
121 | - public static function containsValue($value) |
|
122 | - { |
|
123 | - return in_array($value, static::getInstance()->session); |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Returns the value to which the specified key is mapped, or null if the session map contains no mapping for the key. |
|
128 | - * |
|
129 | - * @param string $key |
|
130 | - * @return string|null the value to which the specified key is mapped, or null if the session map contains no mapping for the key. |
|
131 | - */ |
|
132 | - public static function get($key) |
|
133 | - { |
|
134 | - return static::containsKey($key) ? static::getInstance()->session[$key] : null; |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * Associates the specified value with the specified key in the session map. |
|
139 | - * |
|
140 | - * @param string $key |
|
141 | - * @param string $value |
|
142 | - * @return null |
|
143 | - */ |
|
144 | - public static function set($key, $value) |
|
145 | - { |
|
146 | - static::getInstance()->session[$key] = $value; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Removes the mapping for the specified key from the session map if present. |
|
151 | - * |
|
152 | - * @param string $key |
|
153 | - * @return null |
|
154 | - */ |
|
155 | - public static function remove($key) |
|
156 | - { |
|
157 | - if (static::containsKey($key)) { |
|
158 | - unset(static::getInstance()->session[$key]); |
|
159 | - } |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * Removes all of the mappings from the session map. |
|
164 | - * |
|
165 | - * @return null |
|
166 | - */ |
|
167 | - public static function clear() |
|
168 | - { |
|
169 | - static::getInstance()->session = []; |
|
170 | - } |
|
21 | + use SingletonTrait; |
|
22 | + |
|
23 | + /** @var array The session. */ |
|
24 | + private $session; |
|
25 | + |
|
26 | + /** |
|
27 | + * Construct a Sesssion object. |
|
28 | + * |
|
29 | + * @global array $_SESSION The session parameters. |
|
30 | + */ |
|
31 | + protected function __construct() |
|
32 | + { |
|
33 | + $this->session = &$_SESSION; |
|
34 | + } |
|
35 | + |
|
36 | + /** |
|
37 | + * Initialize the session, if none exists. |
|
38 | + * |
|
39 | + * @param $name = null |
|
40 | + * @param $domain bool |
|
41 | + * @return null |
|
42 | + * @throws \RuntimeException on failure. |
|
43 | + */ |
|
44 | + public static function start($name = null, $domain = false) |
|
45 | + { |
|
46 | + if (session_status() !== PHP_SESSION_NONE) { |
|
47 | + throw new \RuntimeException('Can\'t start a new session.'); |
|
48 | + } |
|
49 | + if ($name !== null) { |
|
50 | + session_name($name); |
|
51 | + } |
|
52 | + if ($domain){ |
|
53 | + preg_match("/[^\.\/]+\.[^\.\/]+$/", $_SERVER['HTTP_HOST'], $matches); |
|
54 | + session_set_cookie_params(0, '/', $matches[0]); |
|
55 | + } |
|
56 | + session_start(); |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * Destroy the session, if one exists. |
|
61 | + * |
|
62 | + * @return null |
|
63 | + * @throws \RuntimeException on failure. |
|
64 | + */ |
|
65 | + public static function destroy() |
|
66 | + { |
|
67 | + if (session_status() !== PHP_SESSION_ACTIVE) { |
|
68 | + throw new \RuntimeException('Can\'t destroy the session. There is no active session.'); |
|
69 | + } |
|
70 | + |
|
71 | + if (!session_destroy()) { |
|
72 | + throw new \RuntimeException('Failed to destroy the active session.'); |
|
73 | + } |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * {@inheritdoc} |
|
78 | + */ |
|
79 | + public function getIterator() |
|
80 | + { |
|
81 | + return new \ArrayIterator(static::getInstance()->session); |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Returns the number of key-value mappings in the session map. |
|
86 | + * |
|
87 | + * @return int the number of key-value mappings in the session map. |
|
88 | + */ |
|
89 | + public static function count() |
|
90 | + { |
|
91 | + return count(static::getInstance()->session); |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * Returns true if the session map contains no key-value mappings. |
|
96 | + * |
|
97 | + * @return bool true if the session map contains no key-value mappings. |
|
98 | + */ |
|
99 | + public static function isEmpty() |
|
100 | + { |
|
101 | + return empty(static::getInstance()->session); |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Returns true if the session map contains a mapping for the specified key. |
|
106 | + * |
|
107 | + * @param string $key |
|
108 | + * @return bool true if the session map contains a mapping for the specified key. |
|
109 | + */ |
|
110 | + public static function containsKey($key) |
|
111 | + { |
|
112 | + return isset(static::getInstance()->session[$key]); |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Returns true if the session map maps one or more keys to the specified value. |
|
117 | + * |
|
118 | + * @param string $value |
|
119 | + * @return bool true if the session map maps one or more keys to the specified value. |
|
120 | + */ |
|
121 | + public static function containsValue($value) |
|
122 | + { |
|
123 | + return in_array($value, static::getInstance()->session); |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Returns the value to which the specified key is mapped, or null if the session map contains no mapping for the key. |
|
128 | + * |
|
129 | + * @param string $key |
|
130 | + * @return string|null the value to which the specified key is mapped, or null if the session map contains no mapping for the key. |
|
131 | + */ |
|
132 | + public static function get($key) |
|
133 | + { |
|
134 | + return static::containsKey($key) ? static::getInstance()->session[$key] : null; |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * Associates the specified value with the specified key in the session map. |
|
139 | + * |
|
140 | + * @param string $key |
|
141 | + * @param string $value |
|
142 | + * @return null |
|
143 | + */ |
|
144 | + public static function set($key, $value) |
|
145 | + { |
|
146 | + static::getInstance()->session[$key] = $value; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Removes the mapping for the specified key from the session map if present. |
|
151 | + * |
|
152 | + * @param string $key |
|
153 | + * @return null |
|
154 | + */ |
|
155 | + public static function remove($key) |
|
156 | + { |
|
157 | + if (static::containsKey($key)) { |
|
158 | + unset(static::getInstance()->session[$key]); |
|
159 | + } |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * Removes all of the mappings from the session map. |
|
164 | + * |
|
165 | + * @return null |
|
166 | + */ |
|
167 | + public static function clear() |
|
168 | + { |
|
169 | + static::getInstance()->session = []; |
|
170 | + } |
|
171 | 171 | } |
@@ -49,7 +49,7 @@ |
||
49 | 49 | if ($name !== null) { |
50 | 50 | session_name($name); |
51 | 51 | } |
52 | - if ($domain){ |
|
52 | + if ($domain) { |
|
53 | 53 | preg_match("/[^\.\/]+\.[^\.\/]+$/", $_SERVER['HTTP_HOST'], $matches); |
54 | 54 | session_set_cookie_params(0, '/', $matches[0]); |
55 | 55 | } |
@@ -21,410 +21,410 @@ |
||
21 | 21 | */ |
22 | 22 | class ServerRequest extends Request implements ServerRequestInterface |
23 | 23 | { |
24 | - /** @var array The server parameters. */ |
|
25 | - private $serverParams; |
|
26 | - |
|
27 | - /** @var array The cookie parameters. */ |
|
28 | - private $cookieParams; |
|
29 | - |
|
30 | - /** @var array The query parameters. */ |
|
31 | - private $queryParams; |
|
32 | - |
|
33 | - /** @var array The post parameters. */ |
|
34 | - private $postParams; |
|
35 | - |
|
36 | - /** @var array The files parameters. */ |
|
37 | - private $filesParams; |
|
38 | - |
|
39 | - /** @var array The uploaded files. */ |
|
40 | - private $uploadedFiles; |
|
41 | - |
|
42 | - /** @var null|array|object The parsed body. */ |
|
43 | - private $parsedBody; |
|
44 | - |
|
45 | - /** @var array The attributes. */ |
|
46 | - private $attributes; |
|
47 | - |
|
48 | - /** |
|
49 | - * Construct a Request object with the given method, uri, version, headers & body. |
|
50 | - * |
|
51 | - * @global array $_SERVER The server parameters. |
|
52 | - * @global array $_COOKIE The cookie parameters. |
|
53 | - * @global array $_GET The query parameters. |
|
54 | - * @global array $_POST The post parameters. |
|
55 | - * @global array $_FILES The files parameters. |
|
56 | - * |
|
57 | - * @param string $method = '' |
|
58 | - * @param UriInterface|null $uri = null |
|
59 | - * @param string $version = self::DEFAULT_VERSION |
|
60 | - * @param array $headers = [] |
|
61 | - * @param StreamInterface|null $body = null |
|
62 | - */ |
|
63 | - public function __construct($method = '', UriInterface $uri = null, $version = self::DEFAULT_VERSION, array $headers = [], StreamInterface $body = null) |
|
64 | - { |
|
65 | - if ($body === null) { |
|
66 | - $body = new Stream(fopen('php://input', 'r')); |
|
67 | - } |
|
68 | - |
|
69 | - $this->serverParams = $_SERVER; |
|
70 | - $this->cookieParams = $_COOKIE; |
|
71 | - $this->queryParams = $this->initQueryParams($this->serverParams); |
|
72 | - $this->postParams = $_POST; |
|
73 | - $this->filesParams = $_FILES; |
|
74 | - $this->uploadedFiles = $this->initUploadedFiles($this->filesParams); |
|
75 | - $this->attributes = []; |
|
76 | - |
|
77 | - parent::__construct($this->initMethod($method), $this->initUri($uri), $version, $this->initHeaders($headers), $body); |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * Initialize the method. |
|
82 | - * |
|
83 | - * @param string $method |
|
84 | - * @return string the method. |
|
85 | - */ |
|
86 | - private function initMethod($method) |
|
87 | - { |
|
88 | - return $method === '' && isset($this->getServerParams()['REQUEST_METHOD']) ? $this->getServerParams()['REQUEST_METHOD'] : $method; |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * Initialize the URI. |
|
93 | - * |
|
94 | - * @param UriInterface|null $uri |
|
95 | - * @return UriInterface the URI. |
|
96 | - */ |
|
97 | - private function initUri($uri) |
|
98 | - { |
|
99 | - if ($uri !== null) { |
|
100 | - return $uri; |
|
101 | - } |
|
102 | - |
|
103 | - $scheme = isset($this->getServerParams()['HTTPS']) ? 'https://' : 'http://'; |
|
104 | - $host = isset($this->getServerParams()['HTTP_HOST']) ? $scheme . $this->getServerParams()['HTTP_HOST'] : ''; |
|
105 | - $path = isset($this->getServerParams()['REQUEST_URI']) ? $this->getServerParams()['REQUEST_URI'] : ''; |
|
106 | - |
|
107 | - return new URI($host . $path); |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Initialize the headers. |
|
112 | - * |
|
113 | - * @param array $headers |
|
114 | - * @return array the headers. |
|
115 | - */ |
|
116 | - private function initHeaders($headers) |
|
117 | - { |
|
118 | - return $headers ?: getallheaders(); |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * Initialize the headers. |
|
123 | - * |
|
124 | - * @param string $serverParams |
|
125 | - * @return array the headers. |
|
126 | - */ |
|
127 | - private function initQueryParams($serverParams) |
|
128 | - { |
|
129 | - $result = []; |
|
130 | - |
|
131 | - if (isset($serverParams['REQUEST_URI']) && ($query = parse_url($serverParams['REQUEST_URI'], \PHP_URL_QUERY))) { |
|
132 | - parse_str($query, $result); |
|
133 | - } |
|
134 | - |
|
135 | - return $result ?? []; |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * Initialize the uploaded files. |
|
140 | - * |
|
141 | - * @param array $files |
|
142 | - * @return array the uploaded files. |
|
143 | - */ |
|
144 | - private function initUploadedFiles(array $files) |
|
145 | - { |
|
146 | - $result = []; |
|
147 | - |
|
148 | - foreach ($files as $key => $value) { |
|
149 | - $result[$key] = $this->parseUploadedFiles($value); |
|
150 | - } |
|
151 | - |
|
152 | - return $result; |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * Parse uploaded files. |
|
157 | - * |
|
158 | - * @param array $files |
|
159 | - * @return UploadedFile|array uploaded files. |
|
160 | - */ |
|
161 | - private function parseUploadedFiles($files) |
|
162 | - { |
|
163 | - // Empty |
|
164 | - $first = reset($files); |
|
165 | - |
|
166 | - // Single |
|
167 | - if (!is_array($first)) { |
|
168 | - return $this->parseSingleUploadedFiles($files); |
|
169 | - } |
|
170 | - |
|
171 | - // Multiple |
|
172 | - if (count(array_filter(array_keys($first), 'is_string')) === 0) { |
|
173 | - return $this->parseMultipleUploadedFiles($files); |
|
174 | - } |
|
175 | - |
|
176 | - // Namespace |
|
177 | - return $this->initUploadedFiles($files); |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * Parse single uploaded file. |
|
182 | - * |
|
183 | - * @param array $file |
|
184 | - * @return UploadedFile single uploaded file. |
|
185 | - */ |
|
186 | - private function parseSingleUploadedFiles(array $file) |
|
187 | - { |
|
188 | - return new UploadedFile($file['name'], $file['type'], $file['tmp_name'], $file['error'], $file['size']); |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * Parse multiple uploaded files. |
|
193 | - * |
|
194 | - * @param array $files |
|
195 | - * @return UploadedFiles[] multiple uploaded files. |
|
196 | - */ |
|
197 | - private function parseMultipleUploadedFiles(array $files) |
|
198 | - { |
|
199 | - $count = count($files['name']); |
|
200 | - $result = []; |
|
201 | - |
|
202 | - for ($i = 0; $i < $count; $i++) { |
|
203 | - $result[] = new UploadedFile($files['name'][$i], $files['type'][$i], $files['tmp_name'][$i], $files['error'][$i], $files['size'][$i]); |
|
204 | - } |
|
205 | - |
|
206 | - return $result; |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * {@inheritdoc} |
|
211 | - */ |
|
212 | - public function getServerParams() |
|
213 | - { |
|
214 | - return $this->serverParams; |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * {@inheritdoc} |
|
219 | - */ |
|
220 | - public function getCookieParams() |
|
221 | - { |
|
222 | - return $this->cookieParams; |
|
223 | - } |
|
224 | - |
|
225 | - /** |
|
226 | - * Set the cookie params. |
|
227 | - * |
|
228 | - * @param array $cookieParams |
|
229 | - * @return $this |
|
230 | - */ |
|
231 | - private function setCookieParams(array $cookieParams) |
|
232 | - { |
|
233 | - $this->cookieParams = $cookieParams; |
|
234 | - |
|
235 | - return $this; |
|
236 | - } |
|
237 | - |
|
238 | - /** |
|
239 | - * {@inheritdoc} |
|
240 | - */ |
|
241 | - public function withCookieParams(array $cookieParams) |
|
242 | - { |
|
243 | - $result = clone $this; |
|
244 | - |
|
245 | - return $result->setCookieParams($cookieParams); |
|
246 | - } |
|
247 | - |
|
248 | - /** |
|
249 | - * {@inheritdoc} |
|
250 | - */ |
|
251 | - public function getQueryParams() |
|
252 | - { |
|
253 | - return $this->queryParams; |
|
254 | - } |
|
255 | - |
|
256 | - /** |
|
257 | - * Set the query params. |
|
258 | - * |
|
259 | - * @param array $queryParams |
|
260 | - * @return $this |
|
261 | - */ |
|
262 | - private function setQueryParams(array $queryParams) |
|
263 | - { |
|
264 | - $this->queryParams = $queryParams; |
|
265 | - |
|
266 | - return $this; |
|
267 | - } |
|
268 | - |
|
269 | - /** |
|
270 | - * {@inheritdoc} |
|
271 | - */ |
|
272 | - public function withQueryParams(array $queryParams) |
|
273 | - { |
|
274 | - $result = clone $this; |
|
275 | - |
|
276 | - return $result->setQueryParams($queryParams); |
|
277 | - } |
|
278 | - |
|
279 | - /** |
|
280 | - * {@inheritdoc} |
|
281 | - */ |
|
282 | - public function getUploadedFiles() |
|
283 | - { |
|
284 | - return $this->uploadedFiles; |
|
285 | - } |
|
286 | - |
|
287 | - /** |
|
288 | - * Set the uploaded files. |
|
289 | - * |
|
290 | - * @param array $uploadedFiles |
|
291 | - * @return $this |
|
292 | - */ |
|
293 | - private function setUploadedFiles(array $uploadedFiles) |
|
294 | - { |
|
295 | - $this->uploadedFiles = $uploadedFiles; |
|
296 | - |
|
297 | - return $this; |
|
298 | - } |
|
299 | - |
|
300 | - /** |
|
301 | - * {@inheritdoc} |
|
302 | - */ |
|
303 | - public function withUploadedFiles(array $uploadedFiles) |
|
304 | - { |
|
305 | - $result = clone $this; |
|
306 | - |
|
307 | - return $result->setUploadedFiles($uploadedFiles); |
|
308 | - } |
|
309 | - |
|
310 | - /** |
|
311 | - * {@inheritdoc} |
|
312 | - */ |
|
313 | - public function getParsedBody() |
|
314 | - { |
|
315 | - if ($this->parsedBody !== null) { |
|
316 | - return $this->parsedBody; |
|
317 | - } |
|
318 | - if ($this->getMethod() === 'POST' && ($this->hasContentType('application/x-www-form-urlencoded') || $this->hasContentType('multipart/form-data'))) { |
|
319 | - return $this->postParams; |
|
320 | - } |
|
321 | - if ($this->hasContentType('application/json') || $this->hasContentType('text/plain')) { |
|
322 | - return json_decode((string) $this->getBody(), true); |
|
323 | - } |
|
324 | - return null; |
|
325 | - } |
|
326 | - |
|
327 | - |
|
328 | - /** |
|
329 | - * Checks if a content type header exists with the given content type. |
|
330 | - * |
|
331 | - * @param string $contentType |
|
332 | - * @return bool true if a content type header exists with the given content type. |
|
333 | - */ |
|
334 | - private function hasContentType($contentType) |
|
335 | - { |
|
336 | - foreach ($this->getHeader('Content-Type') as $key => $value) { |
|
337 | - if (mb_substr($value, 0, strlen($contentType)) == $contentType) { |
|
338 | - return true; |
|
339 | - } |
|
340 | - } |
|
341 | - |
|
342 | - return false; |
|
343 | - } |
|
344 | - |
|
345 | - /** |
|
346 | - * Set the parsed body. |
|
347 | - * |
|
348 | - * @param null|array|object $parsedBody |
|
349 | - * @return $this |
|
350 | - */ |
|
351 | - private function setParsedBody($parsedBody) |
|
352 | - { |
|
353 | - $this->parsedBody = $parsedBody; |
|
354 | - |
|
355 | - return $this; |
|
356 | - } |
|
357 | - |
|
358 | - /** |
|
359 | - * {@inheritdoc} |
|
360 | - */ |
|
361 | - public function withParsedBody($parsedBody) |
|
362 | - { |
|
363 | - $result = clone $this; |
|
364 | - |
|
365 | - return $result->setParsedBody($parsedBody); |
|
366 | - } |
|
367 | - |
|
368 | - /** |
|
369 | - * {@inheritdoc} |
|
370 | - */ |
|
371 | - public function getAttributes() |
|
372 | - { |
|
373 | - return $this->attributes; |
|
374 | - } |
|
375 | - |
|
376 | - /** |
|
377 | - * {@inheritdoc} |
|
378 | - */ |
|
379 | - public function getAttribute($name, $default = null) |
|
380 | - { |
|
381 | - return isset($this->attributes[$name]) ? $this->attributes[$name] : $default; |
|
382 | - } |
|
383 | - |
|
384 | - /** |
|
385 | - * Set the attribute. |
|
386 | - * |
|
387 | - * @param string $name |
|
388 | - * @param mixed $value |
|
389 | - * @return $this |
|
390 | - */ |
|
391 | - private function setAttribute($name, $value) |
|
392 | - { |
|
393 | - $this->attributes[$name] = $value; |
|
394 | - |
|
395 | - return $this; |
|
396 | - } |
|
397 | - |
|
398 | - /** |
|
399 | - * {@inheritdoc} |
|
400 | - */ |
|
401 | - public function withAttribute($name, $value) |
|
402 | - { |
|
403 | - $result = clone $this; |
|
404 | - |
|
405 | - return $result->setAttribute($name, $value); |
|
406 | - } |
|
407 | - |
|
408 | - /** |
|
409 | - * Remove the attribute. |
|
410 | - * |
|
411 | - * @param string $name |
|
412 | - * @return $this |
|
413 | - */ |
|
414 | - private function removeAttribute($name) |
|
415 | - { |
|
416 | - unset($this->attributes[$name]); |
|
417 | - |
|
418 | - return $this; |
|
419 | - } |
|
420 | - |
|
421 | - /** |
|
422 | - * {@inheritdoc} |
|
423 | - */ |
|
424 | - public function withoutAttribute($name) |
|
425 | - { |
|
426 | - $result = clone $this; |
|
427 | - |
|
428 | - return $result->removeAttribute($name); |
|
429 | - } |
|
24 | + /** @var array The server parameters. */ |
|
25 | + private $serverParams; |
|
26 | + |
|
27 | + /** @var array The cookie parameters. */ |
|
28 | + private $cookieParams; |
|
29 | + |
|
30 | + /** @var array The query parameters. */ |
|
31 | + private $queryParams; |
|
32 | + |
|
33 | + /** @var array The post parameters. */ |
|
34 | + private $postParams; |
|
35 | + |
|
36 | + /** @var array The files parameters. */ |
|
37 | + private $filesParams; |
|
38 | + |
|
39 | + /** @var array The uploaded files. */ |
|
40 | + private $uploadedFiles; |
|
41 | + |
|
42 | + /** @var null|array|object The parsed body. */ |
|
43 | + private $parsedBody; |
|
44 | + |
|
45 | + /** @var array The attributes. */ |
|
46 | + private $attributes; |
|
47 | + |
|
48 | + /** |
|
49 | + * Construct a Request object with the given method, uri, version, headers & body. |
|
50 | + * |
|
51 | + * @global array $_SERVER The server parameters. |
|
52 | + * @global array $_COOKIE The cookie parameters. |
|
53 | + * @global array $_GET The query parameters. |
|
54 | + * @global array $_POST The post parameters. |
|
55 | + * @global array $_FILES The files parameters. |
|
56 | + * |
|
57 | + * @param string $method = '' |
|
58 | + * @param UriInterface|null $uri = null |
|
59 | + * @param string $version = self::DEFAULT_VERSION |
|
60 | + * @param array $headers = [] |
|
61 | + * @param StreamInterface|null $body = null |
|
62 | + */ |
|
63 | + public function __construct($method = '', UriInterface $uri = null, $version = self::DEFAULT_VERSION, array $headers = [], StreamInterface $body = null) |
|
64 | + { |
|
65 | + if ($body === null) { |
|
66 | + $body = new Stream(fopen('php://input', 'r')); |
|
67 | + } |
|
68 | + |
|
69 | + $this->serverParams = $_SERVER; |
|
70 | + $this->cookieParams = $_COOKIE; |
|
71 | + $this->queryParams = $this->initQueryParams($this->serverParams); |
|
72 | + $this->postParams = $_POST; |
|
73 | + $this->filesParams = $_FILES; |
|
74 | + $this->uploadedFiles = $this->initUploadedFiles($this->filesParams); |
|
75 | + $this->attributes = []; |
|
76 | + |
|
77 | + parent::__construct($this->initMethod($method), $this->initUri($uri), $version, $this->initHeaders($headers), $body); |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * Initialize the method. |
|
82 | + * |
|
83 | + * @param string $method |
|
84 | + * @return string the method. |
|
85 | + */ |
|
86 | + private function initMethod($method) |
|
87 | + { |
|
88 | + return $method === '' && isset($this->getServerParams()['REQUEST_METHOD']) ? $this->getServerParams()['REQUEST_METHOD'] : $method; |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * Initialize the URI. |
|
93 | + * |
|
94 | + * @param UriInterface|null $uri |
|
95 | + * @return UriInterface the URI. |
|
96 | + */ |
|
97 | + private function initUri($uri) |
|
98 | + { |
|
99 | + if ($uri !== null) { |
|
100 | + return $uri; |
|
101 | + } |
|
102 | + |
|
103 | + $scheme = isset($this->getServerParams()['HTTPS']) ? 'https://' : 'http://'; |
|
104 | + $host = isset($this->getServerParams()['HTTP_HOST']) ? $scheme . $this->getServerParams()['HTTP_HOST'] : ''; |
|
105 | + $path = isset($this->getServerParams()['REQUEST_URI']) ? $this->getServerParams()['REQUEST_URI'] : ''; |
|
106 | + |
|
107 | + return new URI($host . $path); |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Initialize the headers. |
|
112 | + * |
|
113 | + * @param array $headers |
|
114 | + * @return array the headers. |
|
115 | + */ |
|
116 | + private function initHeaders($headers) |
|
117 | + { |
|
118 | + return $headers ?: getallheaders(); |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * Initialize the headers. |
|
123 | + * |
|
124 | + * @param string $serverParams |
|
125 | + * @return array the headers. |
|
126 | + */ |
|
127 | + private function initQueryParams($serverParams) |
|
128 | + { |
|
129 | + $result = []; |
|
130 | + |
|
131 | + if (isset($serverParams['REQUEST_URI']) && ($query = parse_url($serverParams['REQUEST_URI'], \PHP_URL_QUERY))) { |
|
132 | + parse_str($query, $result); |
|
133 | + } |
|
134 | + |
|
135 | + return $result ?? []; |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * Initialize the uploaded files. |
|
140 | + * |
|
141 | + * @param array $files |
|
142 | + * @return array the uploaded files. |
|
143 | + */ |
|
144 | + private function initUploadedFiles(array $files) |
|
145 | + { |
|
146 | + $result = []; |
|
147 | + |
|
148 | + foreach ($files as $key => $value) { |
|
149 | + $result[$key] = $this->parseUploadedFiles($value); |
|
150 | + } |
|
151 | + |
|
152 | + return $result; |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * Parse uploaded files. |
|
157 | + * |
|
158 | + * @param array $files |
|
159 | + * @return UploadedFile|array uploaded files. |
|
160 | + */ |
|
161 | + private function parseUploadedFiles($files) |
|
162 | + { |
|
163 | + // Empty |
|
164 | + $first = reset($files); |
|
165 | + |
|
166 | + // Single |
|
167 | + if (!is_array($first)) { |
|
168 | + return $this->parseSingleUploadedFiles($files); |
|
169 | + } |
|
170 | + |
|
171 | + // Multiple |
|
172 | + if (count(array_filter(array_keys($first), 'is_string')) === 0) { |
|
173 | + return $this->parseMultipleUploadedFiles($files); |
|
174 | + } |
|
175 | + |
|
176 | + // Namespace |
|
177 | + return $this->initUploadedFiles($files); |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * Parse single uploaded file. |
|
182 | + * |
|
183 | + * @param array $file |
|
184 | + * @return UploadedFile single uploaded file. |
|
185 | + */ |
|
186 | + private function parseSingleUploadedFiles(array $file) |
|
187 | + { |
|
188 | + return new UploadedFile($file['name'], $file['type'], $file['tmp_name'], $file['error'], $file['size']); |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * Parse multiple uploaded files. |
|
193 | + * |
|
194 | + * @param array $files |
|
195 | + * @return UploadedFiles[] multiple uploaded files. |
|
196 | + */ |
|
197 | + private function parseMultipleUploadedFiles(array $files) |
|
198 | + { |
|
199 | + $count = count($files['name']); |
|
200 | + $result = []; |
|
201 | + |
|
202 | + for ($i = 0; $i < $count; $i++) { |
|
203 | + $result[] = new UploadedFile($files['name'][$i], $files['type'][$i], $files['tmp_name'][$i], $files['error'][$i], $files['size'][$i]); |
|
204 | + } |
|
205 | + |
|
206 | + return $result; |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * {@inheritdoc} |
|
211 | + */ |
|
212 | + public function getServerParams() |
|
213 | + { |
|
214 | + return $this->serverParams; |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * {@inheritdoc} |
|
219 | + */ |
|
220 | + public function getCookieParams() |
|
221 | + { |
|
222 | + return $this->cookieParams; |
|
223 | + } |
|
224 | + |
|
225 | + /** |
|
226 | + * Set the cookie params. |
|
227 | + * |
|
228 | + * @param array $cookieParams |
|
229 | + * @return $this |
|
230 | + */ |
|
231 | + private function setCookieParams(array $cookieParams) |
|
232 | + { |
|
233 | + $this->cookieParams = $cookieParams; |
|
234 | + |
|
235 | + return $this; |
|
236 | + } |
|
237 | + |
|
238 | + /** |
|
239 | + * {@inheritdoc} |
|
240 | + */ |
|
241 | + public function withCookieParams(array $cookieParams) |
|
242 | + { |
|
243 | + $result = clone $this; |
|
244 | + |
|
245 | + return $result->setCookieParams($cookieParams); |
|
246 | + } |
|
247 | + |
|
248 | + /** |
|
249 | + * {@inheritdoc} |
|
250 | + */ |
|
251 | + public function getQueryParams() |
|
252 | + { |
|
253 | + return $this->queryParams; |
|
254 | + } |
|
255 | + |
|
256 | + /** |
|
257 | + * Set the query params. |
|
258 | + * |
|
259 | + * @param array $queryParams |
|
260 | + * @return $this |
|
261 | + */ |
|
262 | + private function setQueryParams(array $queryParams) |
|
263 | + { |
|
264 | + $this->queryParams = $queryParams; |
|
265 | + |
|
266 | + return $this; |
|
267 | + } |
|
268 | + |
|
269 | + /** |
|
270 | + * {@inheritdoc} |
|
271 | + */ |
|
272 | + public function withQueryParams(array $queryParams) |
|
273 | + { |
|
274 | + $result = clone $this; |
|
275 | + |
|
276 | + return $result->setQueryParams($queryParams); |
|
277 | + } |
|
278 | + |
|
279 | + /** |
|
280 | + * {@inheritdoc} |
|
281 | + */ |
|
282 | + public function getUploadedFiles() |
|
283 | + { |
|
284 | + return $this->uploadedFiles; |
|
285 | + } |
|
286 | + |
|
287 | + /** |
|
288 | + * Set the uploaded files. |
|
289 | + * |
|
290 | + * @param array $uploadedFiles |
|
291 | + * @return $this |
|
292 | + */ |
|
293 | + private function setUploadedFiles(array $uploadedFiles) |
|
294 | + { |
|
295 | + $this->uploadedFiles = $uploadedFiles; |
|
296 | + |
|
297 | + return $this; |
|
298 | + } |
|
299 | + |
|
300 | + /** |
|
301 | + * {@inheritdoc} |
|
302 | + */ |
|
303 | + public function withUploadedFiles(array $uploadedFiles) |
|
304 | + { |
|
305 | + $result = clone $this; |
|
306 | + |
|
307 | + return $result->setUploadedFiles($uploadedFiles); |
|
308 | + } |
|
309 | + |
|
310 | + /** |
|
311 | + * {@inheritdoc} |
|
312 | + */ |
|
313 | + public function getParsedBody() |
|
314 | + { |
|
315 | + if ($this->parsedBody !== null) { |
|
316 | + return $this->parsedBody; |
|
317 | + } |
|
318 | + if ($this->getMethod() === 'POST' && ($this->hasContentType('application/x-www-form-urlencoded') || $this->hasContentType('multipart/form-data'))) { |
|
319 | + return $this->postParams; |
|
320 | + } |
|
321 | + if ($this->hasContentType('application/json') || $this->hasContentType('text/plain')) { |
|
322 | + return json_decode((string) $this->getBody(), true); |
|
323 | + } |
|
324 | + return null; |
|
325 | + } |
|
326 | + |
|
327 | + |
|
328 | + /** |
|
329 | + * Checks if a content type header exists with the given content type. |
|
330 | + * |
|
331 | + * @param string $contentType |
|
332 | + * @return bool true if a content type header exists with the given content type. |
|
333 | + */ |
|
334 | + private function hasContentType($contentType) |
|
335 | + { |
|
336 | + foreach ($this->getHeader('Content-Type') as $key => $value) { |
|
337 | + if (mb_substr($value, 0, strlen($contentType)) == $contentType) { |
|
338 | + return true; |
|
339 | + } |
|
340 | + } |
|
341 | + |
|
342 | + return false; |
|
343 | + } |
|
344 | + |
|
345 | + /** |
|
346 | + * Set the parsed body. |
|
347 | + * |
|
348 | + * @param null|array|object $parsedBody |
|
349 | + * @return $this |
|
350 | + */ |
|
351 | + private function setParsedBody($parsedBody) |
|
352 | + { |
|
353 | + $this->parsedBody = $parsedBody; |
|
354 | + |
|
355 | + return $this; |
|
356 | + } |
|
357 | + |
|
358 | + /** |
|
359 | + * {@inheritdoc} |
|
360 | + */ |
|
361 | + public function withParsedBody($parsedBody) |
|
362 | + { |
|
363 | + $result = clone $this; |
|
364 | + |
|
365 | + return $result->setParsedBody($parsedBody); |
|
366 | + } |
|
367 | + |
|
368 | + /** |
|
369 | + * {@inheritdoc} |
|
370 | + */ |
|
371 | + public function getAttributes() |
|
372 | + { |
|
373 | + return $this->attributes; |
|
374 | + } |
|
375 | + |
|
376 | + /** |
|
377 | + * {@inheritdoc} |
|
378 | + */ |
|
379 | + public function getAttribute($name, $default = null) |
|
380 | + { |
|
381 | + return isset($this->attributes[$name]) ? $this->attributes[$name] : $default; |
|
382 | + } |
|
383 | + |
|
384 | + /** |
|
385 | + * Set the attribute. |
|
386 | + * |
|
387 | + * @param string $name |
|
388 | + * @param mixed $value |
|
389 | + * @return $this |
|
390 | + */ |
|
391 | + private function setAttribute($name, $value) |
|
392 | + { |
|
393 | + $this->attributes[$name] = $value; |
|
394 | + |
|
395 | + return $this; |
|
396 | + } |
|
397 | + |
|
398 | + /** |
|
399 | + * {@inheritdoc} |
|
400 | + */ |
|
401 | + public function withAttribute($name, $value) |
|
402 | + { |
|
403 | + $result = clone $this; |
|
404 | + |
|
405 | + return $result->setAttribute($name, $value); |
|
406 | + } |
|
407 | + |
|
408 | + /** |
|
409 | + * Remove the attribute. |
|
410 | + * |
|
411 | + * @param string $name |
|
412 | + * @return $this |
|
413 | + */ |
|
414 | + private function removeAttribute($name) |
|
415 | + { |
|
416 | + unset($this->attributes[$name]); |
|
417 | + |
|
418 | + return $this; |
|
419 | + } |
|
420 | + |
|
421 | + /** |
|
422 | + * {@inheritdoc} |
|
423 | + */ |
|
424 | + public function withoutAttribute($name) |
|
425 | + { |
|
426 | + $result = clone $this; |
|
427 | + |
|
428 | + return $result->removeAttribute($name); |
|
429 | + } |
|
430 | 430 | } |