| Total Complexity | 82 |
| Total Lines | 572 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 1 | 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 | { |
||
| 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 | $this->__bodyParams = Json::decode($this->getRawBody()); |
||
| 251 | } elseif ('POST' === $this->getMethod()) { |
||
| 252 | $this->__bodyParams = $_POST; |
||
| 253 | } else { |
||
| 254 | $this->__bodyParams = []; |
||
| 255 | mb_parse_str($this->getRawBody(), $this->__bodyParams); |
||
| 256 | } |
||
| 257 | } |
||
| 258 | } |
||
| 259 | return $this->__bodyParams; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * 设置请求体参数 |
||
| 264 | * |
||
| 265 | * @param array $bodyParams |
||
| 266 | * |
||
| 267 | * @return static |
||
| 268 | */ |
||
| 269 | public function setBodyParams($bodyParams) |
||
| 270 | { |
||
| 271 | $this->__bodyParams = $bodyParams; |
||
| 272 | return $this; |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * 返回某个请求体参数 |
||
| 277 | * |
||
| 278 | * @param string $name 请求参数名 |
||
| 279 | * @param mixed $defaultValue 默认值 |
||
| 280 | * |
||
| 281 | * @return mixed |
||
| 282 | */ |
||
| 283 | public function getBodyParam($name, $defaultValue = null) |
||
| 284 | { |
||
| 285 | return I::get($this->getBodyParams(), $name, $defaultValue); |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * 返回 POST 请求参数 |
||
| 290 | * |
||
| 291 | * @param string $name POST 参数 |
||
| 292 | * @param mixed $defaultValue 默认值 |
||
| 293 | * |
||
| 294 | * @return mixed |
||
| 295 | */ |
||
| 296 | public function post($name = null, $defaultValue = null) |
||
| 297 | { |
||
| 298 | if (null === $name) { |
||
| 299 | return $this->getBodyParams(); |
||
| 300 | } |
||
| 301 | return $this->getBodyParam($name, $defaultValue); |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * GET 参数 |
||
| 306 | * |
||
| 307 | * @var array |
||
| 308 | */ |
||
| 309 | private $__queryParams; |
||
| 310 | |||
| 311 | /** |
||
| 312 | * 返回 GET 参数 |
||
| 313 | * |
||
| 314 | * @return array |
||
| 315 | */ |
||
| 316 | public function getQueryParams() |
||
| 317 | { |
||
| 318 | if (null === $this->__queryParams) { |
||
| 319 | return $_GET; |
||
| 320 | } |
||
| 321 | return $this->__queryParams; |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * 设置 GET 参数 |
||
| 326 | * |
||
| 327 | * @param array $queryParams |
||
| 328 | * |
||
| 329 | * @return static |
||
| 330 | */ |
||
| 331 | public function setQueryParams($queryParams) |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * 返回某个 GET 参数 |
||
| 339 | * |
||
| 340 | * @param string $name GET 参数名 |
||
| 341 | * @param mixed $defaultValue 默认值 |
||
| 342 | * |
||
| 343 | * @return mixed |
||
| 344 | */ |
||
| 345 | public function getQueryParam($name, $defaultValue = null) |
||
| 346 | { |
||
| 347 | return I::get($this->getQueryParams(), $name, $defaultValue); |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * 返回 GET 参数 |
||
| 352 | * |
||
| 353 | * @param string $name GET 参数名 |
||
| 354 | * @param mixed $defaultValue 默认值 |
||
| 355 | * |
||
| 356 | * @return mixed |
||
| 357 | */ |
||
| 358 | public function get($name = null, $defaultValue = null) |
||
| 359 | { |
||
| 360 | if (null === $name) { |
||
| 361 | return $this->getQueryParams(); |
||
| 362 | } |
||
| 363 | return $this->getQueryParam($name, $defaultValue); |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * 主机 |
||
| 368 | * |
||
| 369 | * @var string |
||
| 370 | */ |
||
| 371 | private $__hostInfo; |
||
| 372 | |||
| 373 | /** |
||
| 374 | * 获取主机 |
||
| 375 | * |
||
| 376 | * @return array |
||
| 377 | */ |
||
| 378 | public function getHostInfo() |
||
| 379 | { |
||
| 380 | if (null === $this->__hostInfo) { |
||
| 381 | $secure = $this->isSecureConnection(); |
||
| 382 | $http = $secure ? 'https' : 'http'; |
||
| 383 | if (I::get($this->getHeaders(), 'x-forwarded-host')) { |
||
| 384 | $this->__hostInfo = $http . '://' . trim(Arrays::first(explode(',', Arrays::first(I::get($this->getHeaders(), 'x-forward-host'))))); |
||
| 385 | } elseif (I::get($this->getHeaders(), 'host')) { |
||
| 386 | $this->__hostInfo = $http . '://' . Arrays::first(I::get($this->getHeaders(), 'host')); |
||
| 387 | } elseif (isset($_SERVER['SERVER_NAME'])) { |
||
| 388 | $this->__hostInfo = $http . '://' . $_SERVER['SERVER_NAME']; |
||
| 389 | $port = $secure ? $this->getSecurePort() : $this->getPort(); |
||
| 390 | if ((80 !== $port && !$secure) || (443 !== $port && $secure)) { |
||
| 391 | $this->__hostInfo .= ':' . $port; |
||
| 392 | } |
||
| 393 | } |
||
| 394 | } |
||
| 395 | return $this->__hostInfo; |
||
|
|
|||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * 主机名 |
||
| 400 | * |
||
| 401 | * @var string |
||
| 402 | */ |
||
| 403 | private $__hostName; |
||
| 404 | |||
| 405 | /** |
||
| 406 | * 获取主机名 |
||
| 407 | * |
||
| 408 | * @return string |
||
| 409 | */ |
||
| 410 | public function getHostName() |
||
| 411 | { |
||
| 412 | if (null === $this->__hostName) { |
||
| 413 | $this->__hostName = parse_url($this->getHostInfo(), PHP_URL_HOST); |
||
| 414 | } |
||
| 415 | return $this->__hostName; |
||
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * 安全请求的端口号 |
||
| 420 | * |
||
| 421 | * @var int |
||
| 422 | */ |
||
| 423 | private $__securePort; |
||
| 424 | |||
| 425 | /** |
||
| 426 | * 获取安全请求的端口号 |
||
| 427 | * |
||
| 428 | * @return int |
||
| 429 | */ |
||
| 430 | public function getSecurePort() |
||
| 431 | { |
||
| 432 | if (null === $this->__securePort) { |
||
| 433 | $serverPort = $this->getServerPort(); |
||
| 434 | $this->__securePort = $this->isSecureConnection() && null !== $serverPort ? $serverPort : 443; |
||
| 435 | } |
||
| 436 | return $this->__securePort; |
||
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * 端口号 |
||
| 441 | * |
||
| 442 | * @var int |
||
| 443 | */ |
||
| 444 | private $__port; |
||
| 445 | |||
| 446 | /** |
||
| 447 | * 获取端口号 |
||
| 448 | * |
||
| 449 | * @return int |
||
| 450 | */ |
||
| 451 | public function getPort() |
||
| 452 | { |
||
| 453 | if (null === $this->__port) { |
||
| 454 | $serverPort = $this->getServerPort(); |
||
| 455 | $this->__port = !$this->isSecureConnection() && null !== $serverPort ? $serverPort : 80; |
||
| 456 | } |
||
| 457 | return $this->__port; |
||
| 458 | } |
||
| 459 | |||
| 460 | /** |
||
| 461 | * 获取 GET 字符串 |
||
| 462 | * |
||
| 463 | * @return string |
||
| 464 | */ |
||
| 465 | public function getQueryString() |
||
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * 判断是否是 HTTPS 连接 |
||
| 472 | * |
||
| 473 | * @return boolean |
||
| 474 | */ |
||
| 475 | public function isSecureConnection() |
||
| 476 | { |
||
| 477 | if (isset($_SERVER['HTTPS']) && (strcasecmp($_SERVER['HTTPS'], 'on') === 0 || $_SERVER['HTTPS'] == 1)) { |
||
| 478 | return true; |
||
| 479 | } |
||
| 480 | foreach ($this->__secureProtocolHeaders as $header => $values) { |
||
| 481 | if (($headerValue = I::get($this->getHeaders(), $header)) !== null) { |
||
| 482 | foreach ($values as $value) { |
||
| 483 | if (strcasecmp($headerValue, $value) === 0) { |
||
| 484 | return true; |
||
| 485 | } |
||
| 486 | } |
||
| 487 | } |
||
| 488 | } |
||
| 489 | |||
| 490 | return false; |
||
| 491 | } |
||
| 492 | |||
| 493 | /** |
||
| 494 | * 获取服务器名 |
||
| 495 | * |
||
| 496 | * @return string |
||
| 497 | */ |
||
| 498 | public function getServerName() |
||
| 499 | { |
||
| 500 | return (string) I::get($_SERVER, 'SERVER_NAME'); |
||
| 501 | } |
||
| 502 | |||
| 503 | /** |
||
| 504 | * 获取服务器端口 |
||
| 505 | * |
||
| 506 | * @return integer |
||
| 507 | */ |
||
| 508 | public function getServerPort() |
||
| 509 | { |
||
| 510 | return (int) I::get($_SERVER, 'SERVER_PORT'); |
||
| 511 | } |
||
| 512 | |||
| 513 | /** |
||
| 514 | * 返回 URL 引用 |
||
| 515 | * |
||
| 516 | * @return string |
||
| 517 | */ |
||
| 518 | public function getReferrer() |
||
| 519 | { |
||
| 520 | return Arrays::first(I::get($this->getHeaders(), 'referer')); |
||
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * 返回 CORS 请求的 URL 源 |
||
| 525 | * |
||
| 526 | * @return string |
||
| 527 | */ |
||
| 528 | public function getOrigin() |
||
| 529 | { |
||
| 530 | return Arrays::first(I::get($this->getHeaders(), 'origin')); |
||
| 531 | } |
||
| 532 | |||
| 533 | /** |
||
| 534 | * 返回用户代理 |
||
| 535 | * |
||
| 536 | * @return string |
||
| 537 | */ |
||
| 538 | public function getUserAgent() |
||
| 539 | { |
||
| 540 | return Arrays::first(I::get($this->getHeaders(), 'user-agent')); |
||
| 541 | } |
||
| 542 | |||
| 543 | /** |
||
| 544 | * 返回客户端 IP |
||
| 545 | * |
||
| 546 | * @return string |
||
| 547 | */ |
||
| 548 | public function getUserIP() |
||
| 549 | { |
||
| 550 | foreach ($this->__ipHeaders as $ipHeader) { |
||
| 551 | if (I::get($this->getHeaders(), $ipHeader)) { |
||
| 552 | return trim(Arrays::first(explode(',', Arrays::first(I::get($this->getHeaders(), $ipHeader))))); |
||
| 553 | } |
||
| 554 | } |
||
| 555 | return $this->getRemoteIP(); |
||
| 556 | } |
||
| 557 | |||
| 558 | /** |
||
| 559 | * 返回远端 IP |
||
| 560 | * |
||
| 561 | * @return string |
||
| 562 | */ |
||
| 563 | public function getRemoteIP() |
||
| 564 | { |
||
| 565 | return (string) I::get($_SERVER, 'REMOTE_ADDR'); |
||
| 566 | } |
||
| 567 | |||
| 568 | /** |
||
| 569 | * 返回此连接另一端的主机名 |
||
| 570 | * |
||
| 571 | * @return string |
||
| 572 | */ |
||
| 573 | public function getRemoteHost() |
||
| 574 | { |
||
| 575 | return (string) I::get($_SERVER, 'REMOTE_HOST'); |
||
| 576 | } |
||
| 577 | |||
| 578 | /** |
||
| 579 | * 返回请求内容类型 |
||
| 580 | * |
||
| 581 | * @return string |
||
| 582 | */ |
||
| 583 | public function getContentType() |
||
| 589 | } |
||
| 590 | |||
| 591 | } |
||
| 592 |