Total Complexity | 83 |
Total Lines | 573 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like Request often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Request, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Request |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * 要检查是否通过 HTTPS 建立连接的头的数组列表 |
||
22 | * |
||
23 | * 数组键是头名称,数组值是指示安全连接的头值列表 |
||
24 | * |
||
25 | * 头名称和值的匹配不区分大小写 |
||
26 | * |
||
27 | * 不建议把不安全的邮件头放在这里 |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | private $__secureProtocolHeaders = [ |
||
32 | 'x-forwarded-proto' => ['https'], // Common |
||
33 | 'front-end-https' => ['on'], // Microsoft |
||
34 | ]; |
||
35 | |||
36 | /** |
||
37 | * 代理存储实际客户端 IP 的头列表 |
||
38 | * |
||
39 | * 不建议把不安全的邮件头放在这里 |
||
40 | * |
||
41 | * 头名称的匹配不区分大小写 |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | private $__ipHeaders = [ |
||
46 | 'x-forwarded-for', // Common |
||
47 | ]; |
||
48 | |||
49 | /** |
||
50 | * 头列表 |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | private $__headers; |
||
55 | |||
56 | /** |
||
57 | * 用于指示请求是 PUT、PATCH、DELETE 的 POST 参数的名称 |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | private $__methodParam = '_method'; |
||
62 | |||
63 | /** |
||
64 | * 返回头列表 |
||
65 | * |
||
66 | * @return array |
||
67 | */ |
||
68 | public function getHeaders() |
||
69 | { |
||
70 | if (null === $this->__headers) { |
||
71 | if (function_exists('getallheaders')) { |
||
72 | $headers = getallheaders(); |
||
73 | foreach ($headers as $name => $value) { |
||
74 | $this->__headers[strtolower($name)][] = $value; |
||
75 | } |
||
76 | } elseif (function_exists('http_get_request_headers')) { |
||
77 | $headers = http_get_request_headers(); |
||
78 | foreach ($headers as $name => $value) { |
||
79 | $this->__headers[strtolower($name)][] = $value; |
||
80 | } |
||
81 | } else { |
||
82 | foreach ($_SERVER as $name => $value) { |
||
83 | if (0 === strncmp($name, 'HTTP_', 5)) { |
||
84 | $name = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5))))); |
||
85 | $this->__headers[strtolower($name)][] = $value; |
||
86 | } |
||
87 | } |
||
88 | } |
||
89 | } |
||
90 | return $this->__headers; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * 返回当前的请求方法,可以是:GET、POST、HEAD、PUT、PATCH、DELETE |
||
95 | * |
||
96 | * @return string |
||
97 | */ |
||
98 | public function getMethod() |
||
99 | { |
||
100 | if (isset($_POST[$this->__methodParam]) && !in_array($method = strtoupper($_POST[$this->__methodParam]), ['GET', 'HEAD', 'OPTIONS'])) { |
||
101 | return $method; |
||
102 | } |
||
103 | if ($method = I::get($this->getHeaders(), 'x-http-method-override')) { |
||
104 | return strtoupper($method); |
||
105 | } |
||
106 | if (isset($_SERVER['REQUEST_METHOD'])) { |
||
107 | return strtoupper($_SERVER['REQUEST_METHOD']); |
||
108 | } |
||
109 | return 'GET'; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * 判断是否是 GET 请求 |
||
114 | * |
||
115 | * @return boolean |
||
116 | */ |
||
117 | public function isGet() |
||
118 | { |
||
119 | return 'GET' === $this->getMethod(); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * 判断是否是 OPTIONS 请求 |
||
124 | * |
||
125 | * @return boolean |
||
126 | */ |
||
127 | public function isOptions() |
||
128 | { |
||
129 | return 'OPTIONS' === $this->getMethod(); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * 判断是否是 HEAD 请求 |
||
134 | * |
||
135 | * @return boolean |
||
136 | */ |
||
137 | public function isHead() |
||
138 | { |
||
139 | return 'HEAD' === $this->getMethod(); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * 判断是否是 POST 请求 |
||
144 | * |
||
145 | * @return boolean |
||
146 | */ |
||
147 | public function isPost() |
||
148 | { |
||
149 | return 'POST' === $this->getMethod(); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * 判断是否是 DELETE 请求 |
||
154 | * |
||
155 | * @return boolean |
||
156 | */ |
||
157 | public function isDelete() |
||
158 | { |
||
159 | return 'DELETE' === $this->getMethod(); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * 判断是否是 PUT 请求 |
||
164 | * |
||
165 | * @return boolean |
||
166 | */ |
||
167 | public function isPut() |
||
168 | { |
||
169 | return 'PUT' === $this->getMethod(); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * 判断是否是 PATCH 请求 |
||
174 | * |
||
175 | * @return boolean |
||
176 | */ |
||
177 | public function isPatch() |
||
178 | { |
||
179 | return 'PATCH' === $this->getMethod(); |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * 判断是否是 AJAX 请求 |
||
184 | * |
||
185 | * @return boolean |
||
186 | */ |
||
187 | public function isAjax() |
||
188 | { |
||
189 | return 'XMLHttpRequest' === Arrays::first(I::get($this->getHeaders(), 'x-requested-with')); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * 请求体 |
||
194 | * |
||
195 | * @var string |
||
196 | */ |
||
197 | private $__rawBody; |
||
198 | |||
199 | /** |
||
200 | * 获取请求体 |
||
201 | * |
||
202 | * @return string |
||
203 | */ |
||
204 | public function getRawBody() |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * 设置请求体 |
||
214 | * |
||
215 | * @param string $rawBody |
||
216 | * |
||
217 | * @return static |
||
218 | */ |
||
219 | public function setRawBody($rawBody) |
||
220 | { |
||
221 | $this->__rawBody = $rawBody; |
||
222 | return $this; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * 请求参数 |
||
227 | * |
||
228 | * @var array |
||
229 | */ |
||
230 | private $__bodyParams; |
||
231 | |||
232 | /** |
||
233 | * 返回请求体参数 |
||
234 | * |
||
235 | * @return array |
||
236 | */ |
||
237 | public function getBodyParams() |
||
238 | { |
||
239 | if (null === $this->__bodyParams) { |
||
240 | if (isset($_POST[$this->__methodParam])) { |
||
241 | $this->__bodyParams = $_POST; |
||
242 | unset($this->__bodyParams[$this->__methodParam]); |
||
243 | } else { |
||
244 | $contentType = $this->getContentType(); |
||
245 | if (($pos = strpos($contentType, ';')) !== false) { |
||
246 | // application/json; charset=UTF-8 |
||
247 | $contentType = substr($contentType, 0, $pos); |
||
248 | } |
||
249 | if ('application/json' == $contentType) { |
||
250 | $params = Json::decode($this->getRawBody()); |
||
251 | $this->__bodyParams = null === $params ? [] : $params; |
||
|
|||
252 | } elseif ('POST' === $this->getMethod()) { |
||
253 | $this->__bodyParams = $_POST; |
||
254 | } else { |
||
255 | $this->__bodyParams = []; |
||
256 | mb_parse_str($this->getRawBody(), $this->__bodyParams); |
||
257 | } |
||
258 | } |
||
259 | } |
||
260 | return $this->__bodyParams; |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * 设置请求体参数 |
||
265 | * |
||
266 | * @param array $bodyParams |
||
267 | * |
||
268 | * @return static |
||
269 | */ |
||
270 | public function setBodyParams($bodyParams) |
||
271 | { |
||
272 | $this->__bodyParams = $bodyParams; |
||
273 | return $this; |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * 返回某个请求体参数 |
||
278 | * |
||
279 | * @param string $name 请求参数名 |
||
280 | * @param mixed $defaultValue 默认值 |
||
281 | * |
||
282 | * @return mixed |
||
283 | */ |
||
284 | public function getBodyParam($name, $defaultValue = null) |
||
285 | { |
||
286 | return I::get($this->getBodyParams(), $name, $defaultValue); |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * 返回 POST 请求参数 |
||
291 | * |
||
292 | * @param string $name POST 参数 |
||
293 | * @param mixed $defaultValue 默认值 |
||
294 | * |
||
295 | * @return mixed |
||
296 | */ |
||
297 | public function post($name = null, $defaultValue = null) |
||
298 | { |
||
299 | if (null === $name) { |
||
300 | return $this->getBodyParams(); |
||
301 | } |
||
302 | return $this->getBodyParam($name, $defaultValue); |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * GET 参数 |
||
307 | * |
||
308 | * @var array |
||
309 | */ |
||
310 | private $__queryParams; |
||
311 | |||
312 | /** |
||
313 | * 返回 GET 参数 |
||
314 | * |
||
315 | * @return array |
||
316 | */ |
||
317 | public function getQueryParams() |
||
318 | { |
||
319 | if (null === $this->__queryParams) { |
||
320 | return $_GET; |
||
321 | } |
||
322 | return $this->__queryParams; |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * 设置 GET 参数 |
||
327 | * |
||
328 | * @param array $queryParams |
||
329 | * |
||
330 | * @return static |
||
331 | */ |
||
332 | public function setQueryParams($queryParams) |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * 返回某个 GET 参数 |
||
340 | * |
||
341 | * @param string $name GET 参数名 |
||
342 | * @param mixed $defaultValue 默认值 |
||
343 | * |
||
344 | * @return mixed |
||
345 | */ |
||
346 | public function getQueryParam($name, $defaultValue = null) |
||
347 | { |
||
348 | return I::get($this->getQueryParams(), $name, $defaultValue); |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * 返回 GET 参数 |
||
353 | * |
||
354 | * @param string $name GET 参数名 |
||
355 | * @param mixed $defaultValue 默认值 |
||
356 | * |
||
357 | * @return mixed |
||
358 | */ |
||
359 | public function get($name = null, $defaultValue = null) |
||
360 | { |
||
361 | if (null === $name) { |
||
362 | return $this->getQueryParams(); |
||
363 | } |
||
364 | return $this->getQueryParam($name, $defaultValue); |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * 主机信息数组 |
||
369 | * |
||
370 | * @var array |
||
371 | */ |
||
372 | private $__hostInfo; |
||
373 | |||
374 | /** |
||
375 | * 获取主机信息 |
||
376 | * |
||
377 | * @return array |
||
378 | */ |
||
379 | public function getHostInfo() |
||
380 | { |
||
381 | if (null === $this->__hostInfo) { |
||
382 | $secure = $this->isSecureConnection(); |
||
383 | $http = $secure ? 'https' : 'http'; |
||
384 | if (I::get($this->getHeaders(), 'x-forwarded-host')) { |
||
385 | $this->__hostInfo = $http . '://' . trim(Arrays::first(explode(',', Arrays::first(I::get($this->getHeaders(), 'x-forward-host'))))); |
||
386 | } elseif (I::get($this->getHeaders(), 'host')) { |
||
387 | $this->__hostInfo = $http . '://' . Arrays::first(I::get($this->getHeaders(), 'host')); |
||
388 | } elseif (isset($_SERVER['SERVER_NAME'])) { |
||
389 | $this->__hostInfo = $http . '://' . $_SERVER['SERVER_NAME']; |
||
390 | $port = $secure ? $this->getSecurePort() : $this->getPort(); |
||
391 | if (80 !== $port && !$secure || 443 !== $port && $secure) { |
||
392 | $this->__hostInfo .= ':' . $port; |
||
393 | } |
||
394 | } |
||
395 | } |
||
396 | return $this->__hostInfo; |
||
397 | } |
||
398 | |||
399 | /** |
||
400 | * 主机名 |
||
401 | * |
||
402 | * @var string |
||
403 | */ |
||
404 | private $__hostName; |
||
405 | |||
406 | /** |
||
407 | * 获取主机名 |
||
408 | * |
||
409 | * @return string |
||
410 | */ |
||
411 | public function getHostName() |
||
412 | { |
||
413 | if (null === $this->__hostName) { |
||
414 | $this->__hostName = parse_url($this->getHostInfo(), PHP_URL_HOST); |
||
415 | } |
||
416 | return $this->__hostName; |
||
417 | } |
||
418 | |||
419 | /** |
||
420 | * 安全请求的端口号 |
||
421 | * |
||
422 | * @var int |
||
423 | */ |
||
424 | private $__securePort; |
||
425 | |||
426 | /** |
||
427 | * 获取安全请求的端口号 |
||
428 | * |
||
429 | * @return int |
||
430 | */ |
||
431 | public function getSecurePort() |
||
432 | { |
||
433 | if (null === $this->__securePort) { |
||
434 | $serverPort = $this->getServerPort(); |
||
435 | $this->__securePort = $this->isSecureConnection() && null !== $serverPort ? $serverPort : 443; |
||
436 | } |
||
437 | return $this->__securePort; |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * 端口号 |
||
442 | * |
||
443 | * @var int |
||
444 | */ |
||
445 | private $__port; |
||
446 | |||
447 | /** |
||
448 | * 获取端口号 |
||
449 | * |
||
450 | * @return int |
||
451 | */ |
||
452 | public function getPort() |
||
453 | { |
||
454 | if (null === $this->__port) { |
||
455 | $serverPort = $this->getServerPort(); |
||
456 | $this->__port = !$this->isSecureConnection() && null !== $serverPort ? $serverPort : 80; |
||
457 | } |
||
458 | return $this->__port; |
||
459 | } |
||
460 | |||
461 | /** |
||
462 | * 获取 GET 字符串 |
||
463 | * |
||
464 | * @return string |
||
465 | */ |
||
466 | public function getQueryString() |
||
469 | } |
||
470 | |||
471 | /** |
||
472 | * 判断是否是 HTTPS 连接 |
||
473 | * |
||
474 | * @return boolean |
||
475 | */ |
||
476 | public function isSecureConnection() |
||
477 | { |
||
478 | if (isset($_SERVER['HTTPS']) && (strcasecmp($_SERVER['HTTPS'], 'on') === 0 || $_SERVER['HTTPS'] == 1)) { |
||
479 | return true; |
||
480 | } |
||
481 | foreach ($this->__secureProtocolHeaders as $header => $values) { |
||
482 | if (($headerValue = I::get($this->getHeaders(), $header)) !== null) { |
||
483 | foreach ($values as $value) { |
||
484 | if (strcasecmp($headerValue, $value) === 0) { |
||
485 | return true; |
||
486 | } |
||
487 | } |
||
488 | } |
||
489 | } |
||
490 | |||
491 | return false; |
||
492 | } |
||
493 | |||
494 | /** |
||
495 | * 获取服务器名 |
||
496 | * |
||
497 | * @return string |
||
498 | */ |
||
499 | public function getServerName() |
||
500 | { |
||
501 | return I::get($_SERVER, 'SERVER_NAME'); |
||
502 | } |
||
503 | |||
504 | /** |
||
505 | * 获取服务器端口 |
||
506 | * |
||
507 | * @return int |
||
508 | */ |
||
509 | public function getServerPort() |
||
510 | { |
||
511 | return I::get($_SERVER, 'SERVER_PORT'); |
||
512 | } |
||
513 | |||
514 | /** |
||
515 | * 返回 URL 引用 |
||
516 | * |
||
517 | * @return string |
||
518 | */ |
||
519 | public function getReferrer() |
||
520 | { |
||
521 | return Arrays::first(I::get($this->getHeaders(), 'referer')); |
||
522 | } |
||
523 | |||
524 | /** |
||
525 | * 返回 CORS 请求的 URL 源 |
||
526 | * |
||
527 | * @return string |
||
528 | */ |
||
529 | public function getOrigin() |
||
530 | { |
||
531 | return Arrays::first(I::get($this->getHeaders(), 'origin')); |
||
532 | } |
||
533 | |||
534 | /** |
||
535 | * 返回用户代理 |
||
536 | * |
||
537 | * @return string |
||
538 | */ |
||
539 | public function getUserAgent() |
||
540 | { |
||
541 | return Arrays::first(I::get($this->getHeaders(), 'user-agent')); |
||
542 | } |
||
543 | |||
544 | /** |
||
545 | * 返回客户端 IP |
||
546 | * |
||
547 | * @return string |
||
548 | */ |
||
549 | public function getUserIP() |
||
550 | { |
||
551 | foreach ($this->__ipHeaders as $ipHeader) { |
||
552 | if (I::get($this->getHeaders(), $ipHeader)) { |
||
553 | return trim(Arrays::first(explode(',', Arrays::first(I::get($this->getHeaders(), $ipHeader))))); |
||
554 | } |
||
555 | } |
||
556 | return $this->getRemoteIP(); |
||
557 | } |
||
558 | |||
559 | /** |
||
560 | * 返回远端 IP |
||
561 | * |
||
562 | * @return string |
||
563 | */ |
||
564 | public function getRemoteIP() |
||
565 | { |
||
566 | return I::get($_SERVER, 'REMOTE_ADDR'); |
||
567 | } |
||
568 | |||
569 | /** |
||
570 | * 返回此连接另一端的主机名 |
||
571 | * |
||
572 | * @return string |
||
573 | */ |
||
574 | public function getRemoteHost() |
||
575 | { |
||
576 | return I::get($_SERVER, 'REMOTE_HOST'); |
||
577 | } |
||
578 | |||
579 | /** |
||
580 | * 返回请求内容类型 |
||
581 | * |
||
582 | * @return string |
||
583 | */ |
||
584 | public function getContentType() |
||
590 | } |
||
591 | |||
592 | } |
||
593 |