Total Complexity | 307 |
Total Lines | 2140 |
Duplicated Lines | 0 % |
Coverage | 10.99% |
Changes | 18 | ||
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 |
||
22 | class Request |
||
23 | { |
||
24 | /** |
||
25 | * 兼容PATH_INFO获取 |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $pathinfoFetch = ['ORIG_PATH_INFO', 'REDIRECT_PATH_INFO', 'REDIRECT_URL']; |
||
29 | |||
30 | /** |
||
31 | * PATHINFO变量名 用于兼容模式 |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $varPathinfo = 's'; |
||
35 | |||
36 | /** |
||
37 | * 请求类型 |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $varMethod = '_method'; |
||
41 | |||
42 | /** |
||
43 | * 表单ajax伪装变量 |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $varAjax = '_ajax'; |
||
47 | |||
48 | /** |
||
49 | * 表单pjax伪装变量 |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $varPjax = '_pjax'; |
||
53 | |||
54 | /** |
||
55 | * 域名根 |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $rootDomain = ''; |
||
59 | |||
60 | /** |
||
61 | * HTTPS代理标识 |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $httpsAgentName = ''; |
||
65 | |||
66 | /** |
||
67 | * 前端代理服务器IP |
||
68 | * @var array |
||
69 | */ |
||
70 | protected $proxyServerIp = []; |
||
71 | |||
72 | /** |
||
73 | * 前端代理服务器真实IP头 |
||
74 | * @var array |
||
75 | */ |
||
76 | protected $proxyServerIpHeader = ['HTTP_X_REAL_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP', 'HTTP_X_CLIENT_IP', 'HTTP_X_CLUSTER_CLIENT_IP']; |
||
77 | |||
78 | /** |
||
79 | * 请求类型 |
||
80 | * @var string |
||
81 | */ |
||
82 | protected $method; |
||
83 | |||
84 | /** |
||
85 | * 域名(含协议及端口) |
||
86 | * @var string |
||
87 | */ |
||
88 | protected $domain; |
||
89 | |||
90 | /** |
||
91 | * HOST(含端口) |
||
92 | * @var string |
||
93 | */ |
||
94 | protected $host; |
||
95 | |||
96 | /** |
||
97 | * 子域名 |
||
98 | * @var string |
||
99 | */ |
||
100 | protected $subDomain; |
||
101 | |||
102 | /** |
||
103 | * 泛域名 |
||
104 | * @var string |
||
105 | */ |
||
106 | protected $panDomain; |
||
107 | |||
108 | /** |
||
109 | * 当前URL地址 |
||
110 | * @var string |
||
111 | */ |
||
112 | protected $url; |
||
113 | |||
114 | /** |
||
115 | * 基础URL |
||
116 | * @var string |
||
117 | */ |
||
118 | protected $baseUrl; |
||
119 | |||
120 | /** |
||
121 | * 当前执行的文件 |
||
122 | * @var string |
||
123 | */ |
||
124 | protected $baseFile; |
||
125 | |||
126 | /** |
||
127 | * 访问的ROOT地址 |
||
128 | * @var string |
||
129 | */ |
||
130 | protected $root; |
||
131 | |||
132 | /** |
||
133 | * pathinfo |
||
134 | * @var string |
||
135 | */ |
||
136 | protected $pathinfo; |
||
137 | |||
138 | /** |
||
139 | * pathinfo(不含后缀) |
||
140 | * @var string |
||
141 | */ |
||
142 | protected $path; |
||
143 | |||
144 | /** |
||
145 | * 当前请求的IP地址 |
||
146 | * @var string |
||
147 | */ |
||
148 | protected $realIP; |
||
149 | |||
150 | /** |
||
151 | * 当前应用名 |
||
152 | * @var string |
||
153 | */ |
||
154 | protected $app; |
||
155 | |||
156 | /** |
||
157 | * 当前控制器名 |
||
158 | * @var string |
||
159 | */ |
||
160 | protected $controller; |
||
161 | |||
162 | /** |
||
163 | * 当前操作名 |
||
164 | * @var string |
||
165 | */ |
||
166 | protected $action; |
||
167 | |||
168 | /** |
||
169 | * 当前请求参数 |
||
170 | * @var array |
||
171 | */ |
||
172 | protected $param = []; |
||
173 | |||
174 | /** |
||
175 | * 当前GET参数 |
||
176 | * @var array |
||
177 | */ |
||
178 | protected $get = []; |
||
179 | |||
180 | /** |
||
181 | * 当前POST参数 |
||
182 | * @var array |
||
183 | */ |
||
184 | protected $post = []; |
||
185 | |||
186 | /** |
||
187 | * 当前REQUEST参数 |
||
188 | * @var array |
||
189 | */ |
||
190 | protected $request = []; |
||
191 | |||
192 | /** |
||
193 | * 当前路由对象 |
||
194 | * @var Rule |
||
195 | */ |
||
196 | protected $rule; |
||
197 | |||
198 | /** |
||
199 | * 当前ROUTE参数 |
||
200 | * @var array |
||
201 | */ |
||
202 | protected $route = []; |
||
203 | |||
204 | /** |
||
205 | * 中间件传递的参数 |
||
206 | * @var array |
||
207 | */ |
||
208 | protected $middleware = []; |
||
209 | |||
210 | /** |
||
211 | * 当前PUT参数 |
||
212 | * @var array |
||
213 | */ |
||
214 | protected $put; |
||
215 | |||
216 | /** |
||
217 | * SESSION对象 |
||
218 | * @var Session |
||
219 | */ |
||
220 | protected $session; |
||
221 | |||
222 | /** |
||
223 | * COOKIE数据 |
||
224 | * @var array |
||
225 | */ |
||
226 | protected $cookie = []; |
||
227 | |||
228 | /** |
||
229 | * ENV对象 |
||
230 | * @var Env |
||
231 | */ |
||
232 | protected $env; |
||
233 | |||
234 | /** |
||
235 | * 当前SERVER参数 |
||
236 | * @var array |
||
237 | */ |
||
238 | protected $server = []; |
||
239 | |||
240 | /** |
||
241 | * 当前FILE参数 |
||
242 | * @var array |
||
243 | */ |
||
244 | protected $file = []; |
||
245 | |||
246 | /** |
||
247 | * 当前HEADER参数 |
||
248 | * @var array |
||
249 | */ |
||
250 | protected $header = []; |
||
251 | |||
252 | /** |
||
253 | * 资源类型定义 |
||
254 | * @var array |
||
255 | */ |
||
256 | protected $mimeType = [ |
||
257 | 'xml' => 'application/xml,text/xml,application/x-xml', |
||
258 | 'json' => 'application/json,text/x-json,application/jsonrequest,text/json', |
||
259 | 'js' => 'text/javascript,application/javascript,application/x-javascript', |
||
260 | 'css' => 'text/css', |
||
261 | 'rss' => 'application/rss+xml', |
||
262 | 'yaml' => 'application/x-yaml,text/yaml', |
||
263 | 'atom' => 'application/atom+xml', |
||
264 | 'pdf' => 'application/pdf', |
||
265 | 'text' => 'text/plain', |
||
266 | 'image' => 'image/png,image/jpg,image/jpeg,image/pjpeg,image/gif,image/webp,image/*', |
||
267 | 'csv' => 'text/csv', |
||
268 | 'html' => 'text/html,application/xhtml+xml,*/*', |
||
269 | ]; |
||
270 | |||
271 | /** |
||
272 | * 当前请求内容 |
||
273 | * @var string |
||
274 | */ |
||
275 | protected $content; |
||
276 | |||
277 | /** |
||
278 | * 全局过滤规则 |
||
279 | * @var array |
||
280 | */ |
||
281 | protected $filter; |
||
282 | |||
283 | /** |
||
284 | * php://input内容 |
||
285 | * @var string |
||
286 | */ |
||
287 | // php://input |
||
288 | protected $input; |
||
289 | |||
290 | /** |
||
291 | * 请求缓存 |
||
292 | * @var array |
||
293 | */ |
||
294 | protected $cache; |
||
295 | |||
296 | /** |
||
297 | * 缓存是否检查 |
||
298 | * @var bool |
||
299 | */ |
||
300 | protected $isCheckCache; |
||
301 | |||
302 | /** |
||
303 | * 请求安全Key |
||
304 | * @var string |
||
305 | */ |
||
306 | protected $secureKey; |
||
307 | |||
308 | /** |
||
309 | * 是否合并Param |
||
310 | * @var bool |
||
311 | */ |
||
312 | protected $mergeParam = false; |
||
313 | |||
314 | /** |
||
315 | * 架构函数 |
||
316 | * @access public |
||
317 | */ |
||
318 | 9 | public function __construct() |
|
319 | { |
||
320 | // 保存 php://input |
||
321 | 9 | $this->input = file_get_contents('php://input'); |
|
322 | 9 | } |
|
323 | |||
324 | 9 | public static function __make(App $app) |
|
2 ignored issues
–
show
|
|||
325 | { |
||
326 | 9 | $request = new static(); |
|
327 | |||
328 | 9 | $request->server = $_SERVER; |
|
329 | 9 | $request->env = $app->env; |
|
330 | 9 | $request->get = $_GET; |
|
331 | 9 | $request->post = $_POST ?: $request->getInputData($request->input); |
|
332 | 9 | $request->put = $request->getInputData($request->input); |
|
333 | 9 | $request->request = $_REQUEST; |
|
334 | 9 | $request->cookie = $_COOKIE; |
|
335 | 9 | $request->file = $_FILES ?? []; |
|
336 | |||
337 | 9 | if (function_exists('apache_request_headers') && $result = apache_request_headers()) { |
|
338 | $header = $result; |
||
339 | } else { |
||
340 | 9 | $header = []; |
|
341 | 9 | $server = $_SERVER; |
|
342 | 9 | foreach ($server as $key => $val) { |
|
343 | 9 | if (0 === strpos($key, 'HTTP_')) { |
|
344 | $key = str_replace('_', '-', strtolower(substr($key, 5))); |
||
345 | 9 | $header[$key] = $val; |
|
346 | } |
||
347 | } |
||
348 | 9 | if (isset($server['CONTENT_TYPE'])) { |
|
349 | $header['content-type'] = $server['CONTENT_TYPE']; |
||
350 | } |
||
351 | 9 | if (isset($server['CONTENT_LENGTH'])) { |
|
352 | $header['content-length'] = $server['CONTENT_LENGTH']; |
||
353 | } |
||
354 | } |
||
355 | |||
356 | 9 | $request->header = array_change_key_case($header); |
|
357 | |||
358 | 9 | return $request; |
|
359 | } |
||
360 | |||
361 | /** |
||
362 | * 设置当前包含协议的域名 |
||
363 | * @access public |
||
364 | * @param string $domain 域名 |
||
365 | * @return $this |
||
366 | */ |
||
367 | public function setDomain(string $domain) |
||
368 | { |
||
369 | $this->domain = $domain; |
||
370 | return $this; |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * 获取当前包含协议的域名 |
||
375 | * @access public |
||
376 | * @param bool $port 是否需要去除端口号 |
||
377 | * @return string |
||
378 | */ |
||
379 | public function domain(bool $port = false): string |
||
380 | { |
||
381 | return $this->scheme() . '://' . $this->host($port); |
||
382 | } |
||
383 | |||
384 | /** |
||
385 | * 获取当前根域名 |
||
386 | * @access public |
||
387 | * @return string |
||
388 | */ |
||
389 | 3 | public function rootDomain(): string |
|
390 | { |
||
391 | 3 | $root = $this->rootDomain; |
|
392 | |||
393 | 3 | if (!$root) { |
|
394 | 3 | $item = explode('.', $this->host()); |
|
395 | 3 | $count = count($item); |
|
396 | 3 | $root = $count > 1 ? $item[$count - 2] . '.' . $item[$count - 1] : $item[0]; |
|
397 | } |
||
398 | |||
399 | 3 | return $root; |
|
400 | } |
||
401 | |||
402 | /** |
||
403 | * 设置当前泛域名的值 |
||
404 | * @access public |
||
405 | * @param string $domain 域名 |
||
406 | * @return $this |
||
407 | */ |
||
408 | public function setSubDomain(string $domain) |
||
409 | { |
||
410 | $this->subDomain = $domain; |
||
411 | return $this; |
||
412 | } |
||
413 | |||
414 | /** |
||
415 | * 获取当前子域名 |
||
416 | * @access public |
||
417 | * @return string |
||
418 | */ |
||
419 | 3 | public function subDomain(): string |
|
420 | { |
||
421 | 3 | if (is_null($this->subDomain)) { |
|
422 | // 获取当前主域名 |
||
423 | 3 | $rootDomain = $this->rootDomain(); |
|
424 | |||
425 | 3 | if ($rootDomain) { |
|
426 | $this->subDomain = rtrim(stristr($this->host(), $rootDomain, true), '.'); |
||
427 | } else { |
||
428 | 3 | $this->subDomain = ''; |
|
429 | } |
||
430 | } |
||
431 | |||
432 | 3 | return $this->subDomain; |
|
433 | } |
||
434 | |||
435 | /** |
||
436 | * 设置当前泛域名的值 |
||
437 | * @access public |
||
438 | * @param string $domain 域名 |
||
439 | * @return $this |
||
440 | */ |
||
441 | public function setPanDomain(string $domain) |
||
442 | { |
||
443 | $this->panDomain = $domain; |
||
444 | return $this; |
||
445 | } |
||
446 | |||
447 | /** |
||
448 | * 获取当前泛域名的值 |
||
449 | * @access public |
||
450 | * @return string |
||
451 | */ |
||
452 | public function panDomain(): string |
||
453 | { |
||
454 | return $this->panDomain ?: ''; |
||
455 | } |
||
456 | |||
457 | /** |
||
458 | * 设置当前完整URL 包括QUERY_STRING |
||
459 | * @access public |
||
460 | * @param string $url URL地址 |
||
461 | * @return $this |
||
462 | */ |
||
463 | public function setUrl(string $url) |
||
464 | { |
||
465 | $this->url = $url; |
||
466 | return $this; |
||
467 | } |
||
468 | |||
469 | /** |
||
470 | * 获取当前完整URL 包括QUERY_STRING |
||
471 | * @access public |
||
472 | * @param bool $complete 是否包含完整域名 |
||
473 | * @return string |
||
474 | */ |
||
475 | public function url(bool $complete = false): string |
||
476 | { |
||
477 | if ($this->url) { |
||
478 | $url = $this->url; |
||
479 | } elseif ($this->server('HTTP_X_REWRITE_URL')) { |
||
480 | $url = $this->server('HTTP_X_REWRITE_URL'); |
||
481 | } elseif ($this->server('REQUEST_URI')) { |
||
482 | $url = $this->server('REQUEST_URI'); |
||
483 | } elseif ($this->server('ORIG_PATH_INFO')) { |
||
484 | $url = $this->server('ORIG_PATH_INFO') . (!empty($this->server('QUERY_STRING')) ? '?' . $this->server('QUERY_STRING') : ''); |
||
485 | } elseif (isset($_SERVER['argv'][1])) { |
||
486 | $url = $_SERVER['argv'][1]; |
||
487 | } else { |
||
488 | $url = ''; |
||
489 | } |
||
490 | |||
491 | return $complete ? $this->domain() . $url : $url; |
||
492 | } |
||
493 | |||
494 | /** |
||
495 | * 设置当前URL 不含QUERY_STRING |
||
496 | * @access public |
||
497 | * @param string $url URL地址 |
||
498 | * @return $this |
||
499 | */ |
||
500 | public function setBaseUrl(string $url) |
||
501 | { |
||
502 | $this->baseUrl = $url; |
||
503 | return $this; |
||
504 | } |
||
505 | |||
506 | /** |
||
507 | * 获取当前URL 不含QUERY_STRING |
||
508 | * @access public |
||
509 | * @param bool $complete 是否包含完整域名 |
||
510 | * @return string |
||
511 | */ |
||
512 | public function baseUrl(bool $complete = false): string |
||
513 | { |
||
514 | if (!$this->baseUrl) { |
||
515 | $str = $this->url(); |
||
516 | $this->baseUrl = strpos($str, '?') ? strstr($str, '?', true) : $str; |
||
517 | } |
||
518 | |||
519 | return $complete ? $this->domain() . $this->baseUrl : $this->baseUrl; |
||
520 | } |
||
521 | |||
522 | /** |
||
523 | * 获取当前执行的文件 SCRIPT_NAME |
||
524 | * @access public |
||
525 | * @param bool $complete 是否包含完整域名 |
||
526 | * @return string |
||
527 | */ |
||
528 | public function baseFile(bool $complete = false): string |
||
529 | { |
||
530 | if (!$this->baseFile) { |
||
531 | $url = ''; |
||
532 | if (!$this->isCli()) { |
||
533 | $script_name = basename($this->server('SCRIPT_FILENAME')); |
||
534 | if (basename($this->server('SCRIPT_NAME')) === $script_name) { |
||
535 | $url = $this->server('SCRIPT_NAME'); |
||
536 | } elseif (basename($this->server('PHP_SELF')) === $script_name) { |
||
537 | $url = $this->server('PHP_SELF'); |
||
538 | } elseif (basename($this->server('ORIG_SCRIPT_NAME')) === $script_name) { |
||
539 | $url = $this->server('ORIG_SCRIPT_NAME'); |
||
540 | } elseif (($pos = strpos($this->server('PHP_SELF'), '/' . $script_name)) !== false) { |
||
541 | $url = substr($this->server('SCRIPT_NAME'), 0, $pos) . '/' . $script_name; |
||
542 | } elseif ($this->server('DOCUMENT_ROOT') && strpos($this->server('SCRIPT_FILENAME'), $this->server('DOCUMENT_ROOT')) === 0) { |
||
543 | $url = str_replace('\\', '/', str_replace($this->server('DOCUMENT_ROOT'), '', $this->server('SCRIPT_FILENAME'))); |
||
544 | } |
||
545 | } |
||
546 | $this->baseFile = $url; |
||
547 | } |
||
548 | |||
549 | return $complete ? $this->domain() . $this->baseFile : $this->baseFile; |
||
550 | } |
||
551 | |||
552 | /** |
||
553 | * 设置URL访问根地址 |
||
554 | * @access public |
||
555 | * @param string $url URL地址 |
||
556 | * @return $this |
||
557 | */ |
||
558 | 2 | public function setRoot(string $url) |
|
559 | { |
||
560 | 2 | $this->root = $url; |
|
561 | 2 | return $this; |
|
562 | } |
||
563 | |||
564 | /** |
||
565 | * 获取URL访问根地址 |
||
566 | * @access public |
||
567 | * @param bool $complete 是否包含完整域名 |
||
568 | * @return string |
||
569 | */ |
||
570 | public function root(bool $complete = false): string |
||
571 | { |
||
572 | if (!$this->root) { |
||
573 | $file = $this->baseFile(); |
||
574 | if ($file && 0 !== strpos($this->url(), $file)) { |
||
575 | $file = str_replace('\\', '/', dirname($file)); |
||
576 | } |
||
577 | $this->root = rtrim($file, '/'); |
||
578 | } |
||
579 | |||
580 | return $complete ? $this->domain() . $this->root : $this->root; |
||
581 | } |
||
582 | |||
583 | /** |
||
584 | * 获取URL访问根目录 |
||
585 | * @access public |
||
586 | * @return string |
||
587 | */ |
||
588 | public function rootUrl(): string |
||
589 | { |
||
590 | $base = $this->root(); |
||
591 | $root = strpos($base, '.') ? ltrim(dirname($base), DIRECTORY_SEPARATOR) : $base; |
||
592 | |||
593 | if ('' != $root) { |
||
594 | $root = '/' . ltrim($root, '/'); |
||
595 | } |
||
596 | |||
597 | return $root; |
||
598 | } |
||
599 | |||
600 | /** |
||
601 | * 设置当前请求的pathinfo |
||
602 | * @access public |
||
603 | * @param string $pathinfo |
||
604 | * @return $this |
||
605 | */ |
||
606 | 2 | public function setPathinfo(string $pathinfo) |
|
607 | { |
||
608 | 2 | $this->pathinfo = $pathinfo; |
|
609 | 2 | return $this; |
|
610 | } |
||
611 | |||
612 | /** |
||
613 | * 获取当前请求URL的pathinfo信息(含URL后缀) |
||
614 | * @access public |
||
615 | * @return string |
||
616 | */ |
||
617 | public function pathinfo(): string |
||
618 | { |
||
619 | if (is_null($this->pathinfo)) { |
||
620 | if (isset($_GET[$this->varPathinfo])) { |
||
621 | // 判断URL里面是否有兼容模式参数 |
||
622 | $pathinfo = $_GET[$this->varPathinfo]; |
||
623 | unset($_GET[$this->varPathinfo]); |
||
624 | unset($this->get[$this->varPathinfo]); |
||
625 | } elseif ($this->server('PATH_INFO')) { |
||
626 | $pathinfo = $this->server('PATH_INFO'); |
||
627 | } elseif ('cli-server' == PHP_SAPI) { |
||
628 | $pathinfo = strpos($this->server('REQUEST_URI'), '?') ? strstr($this->server('REQUEST_URI'), '?', true) : $this->server('REQUEST_URI'); |
||
629 | } |
||
630 | |||
631 | // 分析PATHINFO信息 |
||
632 | if (!isset($pathinfo)) { |
||
633 | foreach ($this->pathinfoFetch as $type) { |
||
634 | if ($this->server($type)) { |
||
635 | $pathinfo = (0 === strpos($this->server($type), $this->server('SCRIPT_NAME'))) ? |
||
636 | substr($this->server($type), strlen($this->server('SCRIPT_NAME'))) : $this->server($type); |
||
637 | break; |
||
638 | } |
||
639 | } |
||
640 | } |
||
641 | |||
642 | if (!empty($pathinfo)) { |
||
643 | unset($this->get[$pathinfo], $this->request[$pathinfo]); |
||
644 | } |
||
645 | |||
646 | $this->pathinfo = empty($pathinfo) || '/' == $pathinfo ? '' : ltrim($pathinfo, '/'); |
||
647 | } |
||
648 | |||
649 | return $this->pathinfo; |
||
650 | } |
||
651 | |||
652 | /** |
||
653 | * 当前URL的访问后缀 |
||
654 | * @access public |
||
655 | * @return string |
||
656 | */ |
||
657 | public function ext(): string |
||
658 | { |
||
659 | return pathinfo($this->pathinfo(), PATHINFO_EXTENSION); |
||
660 | } |
||
661 | |||
662 | /** |
||
663 | * 获取当前请求的时间 |
||
664 | * @access public |
||
665 | * @param bool $float 是否使用浮点类型 |
||
666 | * @return integer|float |
||
667 | */ |
||
668 | public function time(bool $float = false) |
||
669 | { |
||
670 | return $float ? $this->server('REQUEST_TIME_FLOAT') : $this->server('REQUEST_TIME'); |
||
671 | } |
||
672 | |||
673 | /** |
||
674 | * 当前请求的资源类型 |
||
675 | * @access public |
||
676 | * @return string |
||
677 | */ |
||
678 | 9 | public function type(): string |
|
679 | { |
||
680 | 9 | $accept = $this->server('HTTP_ACCEPT'); |
|
681 | |||
682 | 9 | if (empty($accept)) { |
|
683 | 9 | return ''; |
|
684 | } |
||
685 | |||
686 | foreach ($this->mimeType as $key => $val) { |
||
687 | $array = explode(',', $val); |
||
688 | foreach ($array as $k => $v) { |
||
689 | if (stristr($accept, $v)) { |
||
690 | return $key; |
||
691 | } |
||
692 | } |
||
693 | } |
||
694 | |||
695 | return ''; |
||
696 | } |
||
697 | |||
698 | /** |
||
699 | * 设置资源类型 |
||
700 | * @access public |
||
701 | * @param string|array $type 资源类型名 |
||
702 | * @param string $val 资源类型 |
||
703 | * @return void |
||
704 | */ |
||
705 | public function mimeType($type, $val = ''): void |
||
706 | { |
||
707 | if (is_array($type)) { |
||
708 | $this->mimeType = array_merge($this->mimeType, $type); |
||
709 | } else { |
||
710 | $this->mimeType[$type] = $val; |
||
711 | } |
||
712 | } |
||
713 | |||
714 | /** |
||
715 | * 设置请求类型 |
||
716 | * @access public |
||
717 | * @param string $method 请求类型 |
||
718 | * @return $this |
||
719 | */ |
||
720 | public function setMethod(string $method) |
||
721 | { |
||
722 | $this->method = strtoupper($method); |
||
723 | return $this; |
||
724 | } |
||
725 | |||
726 | /** |
||
727 | * 当前的请求类型 |
||
728 | * @access public |
||
729 | * @param bool $origin 是否获取原始请求类型 |
||
730 | * @return string |
||
731 | */ |
||
732 | public function method(bool $origin = false): string |
||
733 | { |
||
734 | if ($origin) { |
||
735 | // 获取原始请求类型 |
||
736 | return $this->server('REQUEST_METHOD') ?: 'GET'; |
||
737 | } elseif (!$this->method) { |
||
738 | if (isset($this->post[$this->varMethod])) { |
||
739 | $method = strtolower($this->post[$this->varMethod]); |
||
740 | if (in_array($method, ['get', 'post', 'put', 'patch', 'delete'])) { |
||
741 | $this->method = strtoupper($method); |
||
742 | $this->{$method} = $this->post; |
||
743 | } else { |
||
744 | $this->method = 'POST'; |
||
745 | } |
||
746 | unset($this->post[$this->varMethod]); |
||
747 | } elseif ($this->server('HTTP_X_HTTP_METHOD_OVERRIDE')) { |
||
748 | $this->method = strtoupper($this->server('HTTP_X_HTTP_METHOD_OVERRIDE')); |
||
749 | } else { |
||
750 | $this->method = $this->server('REQUEST_METHOD') ?: 'GET'; |
||
751 | } |
||
752 | } |
||
753 | |||
754 | return $this->method; |
||
755 | } |
||
756 | |||
757 | /** |
||
758 | * 是否为GET请求 |
||
759 | * @access public |
||
760 | * @return bool |
||
761 | */ |
||
762 | public function isGet(): bool |
||
763 | { |
||
764 | return $this->method() == 'GET'; |
||
765 | } |
||
766 | |||
767 | /** |
||
768 | * 是否为POST请求 |
||
769 | * @access public |
||
770 | * @return bool |
||
771 | */ |
||
772 | public function isPost(): bool |
||
773 | { |
||
774 | return $this->method() == 'POST'; |
||
775 | } |
||
776 | |||
777 | /** |
||
778 | * 是否为PUT请求 |
||
779 | * @access public |
||
780 | * @return bool |
||
781 | */ |
||
782 | public function isPut(): bool |
||
783 | { |
||
784 | return $this->method() == 'PUT'; |
||
785 | } |
||
786 | |||
787 | /** |
||
788 | * 是否为DELTE请求 |
||
789 | * @access public |
||
790 | * @return bool |
||
791 | */ |
||
792 | public function isDelete(): bool |
||
793 | { |
||
794 | return $this->method() == 'DELETE'; |
||
795 | } |
||
796 | |||
797 | /** |
||
798 | * 是否为HEAD请求 |
||
799 | * @access public |
||
800 | * @return bool |
||
801 | */ |
||
802 | public function isHead(): bool |
||
803 | { |
||
804 | return $this->method() == 'HEAD'; |
||
805 | } |
||
806 | |||
807 | /** |
||
808 | * 是否为PATCH请求 |
||
809 | * @access public |
||
810 | * @return bool |
||
811 | */ |
||
812 | public function isPatch(): bool |
||
813 | { |
||
814 | return $this->method() == 'PATCH'; |
||
815 | } |
||
816 | |||
817 | /** |
||
818 | * 是否为OPTIONS请求 |
||
819 | * @access public |
||
820 | * @return bool |
||
821 | */ |
||
822 | public function isOptions(): bool |
||
823 | { |
||
824 | return $this->method() == 'OPTIONS'; |
||
825 | } |
||
826 | |||
827 | /** |
||
828 | * 是否为cli |
||
829 | * @access public |
||
830 | * @return bool |
||
831 | */ |
||
832 | public function isCli(): bool |
||
833 | { |
||
834 | return PHP_SAPI == 'cli'; |
||
835 | } |
||
836 | |||
837 | /** |
||
838 | * 是否为cgi |
||
839 | * @access public |
||
840 | * @return bool |
||
841 | */ |
||
842 | public function isCgi(): bool |
||
843 | { |
||
844 | return strpos(PHP_SAPI, 'cgi') === 0; |
||
845 | } |
||
846 | |||
847 | /** |
||
848 | * 获取当前请求的参数 |
||
849 | * @access public |
||
850 | * @param string|array $name 变量名 |
||
851 | * @param mixed $default 默认值 |
||
852 | * @param string|array $filter 过滤方法 |
||
853 | * @return mixed |
||
854 | */ |
||
855 | public function param($name = '', $default = null, $filter = '') |
||
856 | { |
||
857 | if (empty($this->mergeParam)) { |
||
858 | $method = $this->method(true); |
||
859 | |||
860 | // 自动获取请求变量 |
||
861 | switch ($method) { |
||
862 | case 'POST': |
||
863 | $vars = $this->post(false); |
||
864 | break; |
||
865 | case 'PUT': |
||
866 | case 'DELETE': |
||
867 | case 'PATCH': |
||
868 | $vars = $this->put(false); |
||
869 | break; |
||
870 | default: |
||
871 | $vars = []; |
||
872 | } |
||
873 | |||
874 | // 当前请求参数和URL地址中的参数合并 |
||
875 | $this->param = array_merge($this->param, $this->get(false), $vars, $this->route(false)); |
||
876 | |||
877 | $this->mergeParam = true; |
||
878 | } |
||
879 | |||
880 | if (is_array($name)) { |
||
881 | return $this->only($name, $this->param, $filter); |
||
882 | } |
||
883 | |||
884 | return $this->input($this->param, $name, $default, $filter); |
||
885 | } |
||
886 | |||
887 | /** |
||
888 | * 设置路由变量 |
||
889 | * @access public |
||
890 | * @param Rule $rule 路由对象 |
||
891 | * @return $this |
||
892 | */ |
||
893 | public function setRule(Rule $rule) |
||
894 | { |
||
895 | $this->rule = $rule; |
||
896 | return $this; |
||
897 | } |
||
898 | |||
899 | /** |
||
900 | * 获取当前路由对象 |
||
901 | * @access public |
||
902 | * @return Rule|null |
||
903 | */ |
||
904 | public function rule() |
||
905 | { |
||
906 | return $this->rule; |
||
907 | } |
||
908 | |||
909 | /** |
||
910 | * 设置路由变量 |
||
911 | * @access public |
||
912 | * @param array $route 路由变量 |
||
913 | * @return $this |
||
914 | */ |
||
915 | public function setRoute(array $route) |
||
916 | { |
||
917 | $this->route = array_merge($this->route, $route); |
||
918 | return $this; |
||
919 | } |
||
920 | |||
921 | /** |
||
922 | * 获取路由参数 |
||
923 | * @access public |
||
924 | * @param mixed $name 变量名 |
||
925 | * @param mixed $default 默认值 |
||
926 | * @param string|array $filter 过滤方法 |
||
927 | * @return mixed |
||
928 | */ |
||
929 | public function route($name = '', $default = null, $filter = '') |
||
930 | { |
||
931 | if (is_array($name)) { |
||
932 | return $this->only($name, $this->route, $filter); |
||
933 | } |
||
934 | |||
935 | return $this->input($this->route, $name, $default, $filter); |
||
936 | } |
||
937 | |||
938 | /** |
||
939 | * 获取GET参数 |
||
940 | * @access public |
||
941 | * @param mixed $name 变量名 |
||
942 | * @param mixed $default 默认值 |
||
943 | * @param string|array $filter 过滤方法 |
||
944 | * @return mixed |
||
945 | */ |
||
946 | public function get($name = '', $default = null, $filter = '') |
||
947 | { |
||
948 | if (is_array($name)) { |
||
949 | return $this->only($name, $this->get, $filter); |
||
950 | } |
||
951 | |||
952 | return $this->input($this->get, $name, $default, $filter); |
||
953 | } |
||
954 | |||
955 | /** |
||
956 | * 获取中间件传递的参数 |
||
957 | * @access public |
||
958 | * @param mixed $name 变量名 |
||
959 | * @param mixed $default 默认值 |
||
960 | * @return mixed |
||
961 | */ |
||
962 | public function middleware($name, $default = null) |
||
963 | { |
||
964 | return $this->middleware[$name] ?? $default; |
||
965 | } |
||
966 | |||
967 | /** |
||
968 | * 获取POST参数 |
||
969 | * @access public |
||
970 | * @param mixed $name 变量名 |
||
971 | * @param mixed $default 默认值 |
||
972 | * @param string|array $filter 过滤方法 |
||
973 | * @return mixed |
||
974 | */ |
||
975 | public function post($name = '', $default = null, $filter = '') |
||
976 | { |
||
977 | if (is_array($name)) { |
||
978 | return $this->only($name, $this->post, $filter); |
||
979 | } |
||
980 | |||
981 | return $this->input($this->post, $name, $default, $filter); |
||
982 | } |
||
983 | |||
984 | /** |
||
985 | * 获取PUT参数 |
||
986 | * @access public |
||
987 | * @param mixed $name 变量名 |
||
988 | * @param mixed $default 默认值 |
||
989 | * @param string|array $filter 过滤方法 |
||
990 | * @return mixed |
||
991 | */ |
||
992 | public function put($name = '', $default = null, $filter = '') |
||
993 | { |
||
994 | if (is_array($name)) { |
||
995 | return $this->only($name, $this->put, $filter); |
||
996 | } |
||
997 | |||
998 | return $this->input($this->put, $name, $default, $filter); |
||
999 | } |
||
1000 | |||
1001 | 9 | protected function getInputData($content): array |
|
1002 | { |
||
1003 | 9 | if ($this->isJson()) { |
|
1004 | return (array) json_decode($content, true); |
||
1005 | 9 | } elseif (strpos($content, '=')) { |
|
1006 | parse_str($content, $data); |
||
1007 | return $data; |
||
1008 | } |
||
1009 | |||
1010 | 9 | return []; |
|
1011 | } |
||
1012 | |||
1013 | /** |
||
1014 | * 设置获取DELETE参数 |
||
1015 | * @access public |
||
1016 | * @param mixed $name 变量名 |
||
1017 | * @param mixed $default 默认值 |
||
1018 | * @param string|array $filter 过滤方法 |
||
1019 | * @return mixed |
||
1020 | */ |
||
1021 | public function delete($name = '', $default = null, $filter = '') |
||
1022 | { |
||
1023 | return $this->put($name, $default, $filter); |
||
1024 | } |
||
1025 | |||
1026 | /** |
||
1027 | * 设置获取PATCH参数 |
||
1028 | * @access public |
||
1029 | * @param mixed $name 变量名 |
||
1030 | * @param mixed $default 默认值 |
||
1031 | * @param string|array $filter 过滤方法 |
||
1032 | * @return mixed |
||
1033 | */ |
||
1034 | public function patch($name = '', $default = null, $filter = '') |
||
1035 | { |
||
1036 | return $this->put($name, $default, $filter); |
||
1037 | } |
||
1038 | |||
1039 | /** |
||
1040 | * 获取request变量 |
||
1041 | * @access public |
||
1042 | * @param mixed $name 数据名称 |
||
1043 | * @param mixed $default 默认值 |
||
1044 | * @param string|array $filter 过滤方法 |
||
1045 | * @return mixed |
||
1046 | */ |
||
1047 | public function request($name = '', $default = null, $filter = '') |
||
1048 | { |
||
1049 | if (is_array($name)) { |
||
1050 | return $this->only($name, $this->request, $filter); |
||
1051 | } |
||
1052 | |||
1053 | return $this->input($this->request, $name, $default, $filter); |
||
1054 | } |
||
1055 | |||
1056 | /** |
||
1057 | * 获取环境变量 |
||
1058 | * @access public |
||
1059 | * @param string $name 数据名称 |
||
1060 | * @param string $default 默认值 |
||
1061 | * @return mixed |
||
1062 | */ |
||
1063 | public function env(string $name = '', string $default = null) |
||
1064 | { |
||
1065 | if (empty($name)) { |
||
1066 | return $this->env->get(); |
||
1067 | } else { |
||
1068 | $name = strtoupper($name); |
||
1069 | } |
||
1070 | |||
1071 | return $this->env->get($name, $default); |
||
1072 | } |
||
1073 | |||
1074 | /** |
||
1075 | * 获取session数据 |
||
1076 | * @access public |
||
1077 | * @param string $name 数据名称 |
||
1078 | * @param string $default 默认值 |
||
1079 | * @return mixed |
||
1080 | */ |
||
1081 | public function session(string $name = '', $default = null) |
||
1082 | { |
||
1083 | if ('' === $name) { |
||
1084 | return $this->session->get(); |
||
1085 | } |
||
1086 | |||
1087 | return $this->getData($this->session->get(), $name, $default); |
||
1088 | } |
||
1089 | |||
1090 | /** |
||
1091 | * 获取cookie参数 |
||
1092 | * @access public |
||
1093 | * @param mixed $name 数据名称 |
||
1094 | * @param string $default 默认值 |
||
1095 | * @param string|array $filter 过滤方法 |
||
1096 | * @return mixed |
||
1097 | */ |
||
1098 | public function cookie(string $name = '', $default = null, $filter = '') |
||
1099 | { |
||
1100 | if (!empty($name)) { |
||
1101 | $data = $this->getData($this->cookie, $name, $default); |
||
1102 | } else { |
||
1103 | $data = $this->cookie; |
||
1104 | } |
||
1105 | |||
1106 | // 解析过滤器 |
||
1107 | $filter = $this->getFilter($filter, $default); |
||
1108 | |||
1109 | if (is_array($data)) { |
||
1110 | array_walk_recursive($data, [$this, 'filterValue'], $filter); |
||
1111 | reset($data); |
||
1112 | } else { |
||
1113 | $this->filterValue($data, $name, $filter); |
||
1114 | } |
||
1115 | |||
1116 | return $data; |
||
1117 | } |
||
1118 | |||
1119 | /** |
||
1120 | * 获取server参数 |
||
1121 | * @access public |
||
1122 | * @param string $name 数据名称 |
||
1123 | * @param string $default 默认值 |
||
1124 | * @return mixed |
||
1125 | */ |
||
1126 | 9 | public function server(string $name = '', string $default = '') |
|
1127 | { |
||
1128 | 9 | if (empty($name)) { |
|
1129 | return $this->server; |
||
1130 | } else { |
||
1131 | 9 | $name = strtoupper($name); |
|
1132 | } |
||
1133 | |||
1134 | 9 | return $this->server[$name] ?? $default; |
|
1135 | } |
||
1136 | |||
1137 | /** |
||
1138 | * 获取上传的文件信息 |
||
1139 | * @access public |
||
1140 | * @param string $name 名称 |
||
1141 | * @return null|array|\think\file\UploadedFile |
||
1142 | */ |
||
1143 | public function file(string $name = '') |
||
1144 | { |
||
1145 | $files = $this->file; |
||
1146 | if (!empty($files)) { |
||
1147 | |||
1148 | if (strpos($name, '.')) { |
||
1149 | list($name, $sub) = explode('.', $name); |
||
1150 | } |
||
1151 | |||
1152 | // 处理上传文件 |
||
1153 | $array = $this->dealUploadFile($files, $name); |
||
1154 | |||
1155 | if ('' === $name) { |
||
1156 | // 获取全部文件 |
||
1157 | return $array; |
||
1158 | } elseif (isset($sub) && isset($array[$name][$sub])) { |
||
1159 | return $array[$name][$sub]; |
||
1160 | } elseif (isset($array[$name])) { |
||
1161 | return $array[$name]; |
||
1162 | } |
||
1163 | } |
||
1164 | } |
||
1165 | |||
1166 | protected function dealUploadFile(array $files, string $name): array |
||
1167 | { |
||
1168 | $array = []; |
||
1169 | foreach ($files as $key => $file) { |
||
1170 | if (is_array($file['name'])) { |
||
1171 | $item = []; |
||
1172 | $keys = array_keys($file); |
||
1173 | $count = count($file['name']); |
||
1174 | |||
1175 | for ($i = 0; $i < $count; $i++) { |
||
1176 | if ($file['error'][$i] > 0) { |
||
1177 | if ($name == $key) { |
||
1178 | $this->throwUploadFileError($file['error'][$i]); |
||
1179 | } else { |
||
1180 | continue; |
||
1181 | } |
||
1182 | } |
||
1183 | |||
1184 | $temp['key'] = $key; |
||
1185 | |||
1186 | foreach ($keys as $_key) { |
||
1187 | $temp[$_key] = $file[$_key][$i]; |
||
1188 | } |
||
1189 | |||
1190 | $item[] = new UploadedFile($temp['tmp_name'], $temp['name'], $temp['type'], $temp['error']); |
||
1191 | } |
||
1192 | |||
1193 | $array[$key] = $item; |
||
1194 | } else { |
||
1195 | if ($file instanceof File) { |
||
1196 | $array[$key] = $file; |
||
1197 | } else { |
||
1198 | if ($file['error'] > 0) { |
||
1199 | if ($key == $name) { |
||
1200 | $this->throwUploadFileError($file['error']); |
||
1201 | } else { |
||
1202 | continue; |
||
1203 | } |
||
1204 | } |
||
1205 | |||
1206 | $array[$key] = new UploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['error']); |
||
1207 | } |
||
1208 | } |
||
1209 | } |
||
1210 | |||
1211 | return $array; |
||
1212 | } |
||
1213 | |||
1214 | protected function throwUploadFileError($error) |
||
1215 | { |
||
1216 | static $fileUploadErrors = [ |
||
1217 | 1 => 'upload File size exceeds the maximum value', |
||
1218 | 2 => 'upload File size exceeds the maximum value', |
||
1219 | 3 => 'only the portion of file is uploaded', |
||
1220 | 4 => 'no file to uploaded', |
||
1221 | 6 => 'upload temp dir not found', |
||
1222 | 7 => 'file write error', |
||
1223 | ]; |
||
1224 | |||
1225 | $msg = $fileUploadErrors[$error]; |
||
1226 | throw new Exception($msg, $error); |
||
1227 | } |
||
1228 | |||
1229 | /** |
||
1230 | * 设置或者获取当前的Header |
||
1231 | * @access public |
||
1232 | * @param string $name header名称 |
||
1233 | * @param string $default 默认值 |
||
1234 | * @return string|array |
||
1235 | */ |
||
1236 | public function header(string $name = '', string $default = null) |
||
1237 | { |
||
1238 | if ('' === $name) { |
||
1239 | return $this->header; |
||
1240 | } |
||
1241 | |||
1242 | $name = str_replace('_', '-', strtolower($name)); |
||
1243 | |||
1244 | return $this->header[$name] ?? $default; |
||
1245 | } |
||
1246 | |||
1247 | /** |
||
1248 | * 获取变量 支持过滤和默认值 |
||
1249 | * @access public |
||
1250 | * @param array $data 数据源 |
||
1251 | * @param string|false $name 字段名 |
||
1252 | * @param mixed $default 默认值 |
||
1253 | * @param string|array $filter 过滤函数 |
||
1254 | * @return mixed |
||
1255 | */ |
||
1256 | public function input(array $data = [], $name = '', $default = null, $filter = '') |
||
1257 | { |
||
1258 | if (false === $name) { |
||
1259 | // 获取原始数据 |
||
1260 | return $data; |
||
1261 | } |
||
1262 | |||
1263 | $name = (string) $name; |
||
1264 | if ('' != $name) { |
||
1265 | // 解析name |
||
1266 | if (strpos($name, '/')) { |
||
1267 | list($name, $type) = explode('/', $name); |
||
1268 | } |
||
1269 | |||
1270 | $data = $this->getData($data, $name); |
||
1271 | |||
1272 | if (is_null($data)) { |
||
1273 | return $default; |
||
1274 | } |
||
1275 | |||
1276 | if (is_object($data)) { |
||
1277 | return $data; |
||
1278 | } |
||
1279 | } |
||
1280 | |||
1281 | $data = $this->filterData($data, $filter, $name, $default); |
||
1282 | |||
1283 | if (isset($type) && $data !== $default) { |
||
1284 | // 强制类型转换 |
||
1285 | $this->typeCast($data, $type); |
||
1286 | } |
||
1287 | |||
1288 | return $data; |
||
1289 | } |
||
1290 | |||
1291 | protected function filterData($data, $filter, $name, $default) |
||
1292 | { |
||
1293 | // 解析过滤器 |
||
1294 | $filter = $this->getFilter($filter, $default); |
||
1295 | |||
1296 | if (is_array($data)) { |
||
1297 | array_walk_recursive($data, [$this, 'filterValue'], $filter); |
||
1298 | reset($data); |
||
1299 | } else { |
||
1300 | $this->filterValue($data, $name, $filter); |
||
1301 | } |
||
1302 | |||
1303 | return $data; |
||
1304 | } |
||
1305 | |||
1306 | /** |
||
1307 | * 强制类型转换 |
||
1308 | * @access public |
||
1309 | * @param mixed $data |
||
1310 | * @param string $type |
||
1311 | * @return mixed |
||
1312 | */ |
||
1313 | private function typeCast(&$data, string $type) |
||
1314 | { |
||
1315 | switch (strtolower($type)) { |
||
1316 | // 数组 |
||
1317 | case 'a': |
||
1318 | $data = (array) $data; |
||
1319 | break; |
||
1320 | // 数字 |
||
1321 | case 'd': |
||
1322 | $data = (int) $data; |
||
1323 | break; |
||
1324 | // 浮点 |
||
1325 | case 'f': |
||
1326 | $data = (float) $data; |
||
1327 | break; |
||
1328 | // 布尔 |
||
1329 | case 'b': |
||
1330 | $data = (boolean) $data; |
||
1331 | break; |
||
1332 | // 字符串 |
||
1333 | case 's': |
||
1334 | if (is_scalar($data)) { |
||
1335 | $data = (string) $data; |
||
1336 | } else { |
||
1337 | throw new \InvalidArgumentException('variable type error:' . gettype($data)); |
||
1338 | } |
||
1339 | break; |
||
1340 | } |
||
1341 | } |
||
1342 | |||
1343 | /** |
||
1344 | * 获取数据 |
||
1345 | * @access public |
||
1346 | * @param array $data 数据源 |
||
1347 | * @param string $name 字段名 |
||
1348 | * @param mixed $default 默认值 |
||
1349 | * @return mixed |
||
1350 | */ |
||
1351 | protected function getData(array $data, string $name, $default = null) |
||
1352 | { |
||
1353 | foreach (explode('.', $name) as $val) { |
||
1354 | if (isset($data[$val])) { |
||
1355 | $data = $data[$val]; |
||
1356 | } else { |
||
1357 | return $default; |
||
1358 | } |
||
1359 | } |
||
1360 | |||
1361 | return $data; |
||
1362 | } |
||
1363 | |||
1364 | /** |
||
1365 | * 设置或获取当前的过滤规则 |
||
1366 | * @access public |
||
1367 | * @param mixed $filter 过滤规则 |
||
1368 | * @return mixed |
||
1369 | */ |
||
1370 | public function filter($filter = null) |
||
1371 | { |
||
1372 | if (is_null($filter)) { |
||
1373 | return $this->filter; |
||
1374 | } |
||
1375 | |||
1376 | $this->filter = $filter; |
||
1377 | |||
1378 | return $this; |
||
1379 | } |
||
1380 | |||
1381 | protected function getFilter($filter, $default): array |
||
1382 | { |
||
1383 | if (is_null($filter)) { |
||
1384 | $filter = []; |
||
1385 | } else { |
||
1386 | $filter = $filter ?: $this->filter; |
||
1387 | if (is_string($filter) && false === strpos($filter, '/')) { |
||
1388 | $filter = explode(',', $filter); |
||
1389 | } else { |
||
1390 | $filter = (array) $filter; |
||
1391 | } |
||
1392 | } |
||
1393 | |||
1394 | $filter[] = $default; |
||
1395 | |||
1396 | return $filter; |
||
1397 | } |
||
1398 | |||
1399 | /** |
||
1400 | * 递归过滤给定的值 |
||
1401 | * @access public |
||
1402 | * @param mixed $value 键值 |
||
1403 | * @param mixed $key 键名 |
||
1404 | * @param array $filters 过滤方法+默认值 |
||
1405 | * @return mixed |
||
1406 | */ |
||
1407 | private function filterValue(&$value, $key, $filters) |
||
1408 | { |
||
1409 | $default = array_pop($filters); |
||
1410 | |||
1411 | foreach ($filters as $filter) { |
||
1412 | if (is_callable($filter)) { |
||
1413 | // 调用函数或者方法过滤 |
||
1414 | $value = call_user_func($filter, $value); |
||
1415 | } elseif (is_scalar($value)) { |
||
1416 | if (is_string($filter) && false !== strpos($filter, '/')) { |
||
1417 | // 正则过滤 |
||
1418 | if (!preg_match($filter, $value)) { |
||
1419 | // 匹配不成功返回默认值 |
||
1420 | $value = $default; |
||
1421 | break; |
||
1422 | } |
||
1423 | } elseif (!empty($filter)) { |
||
1424 | // filter函数不存在时, 则使用filter_var进行过滤 |
||
1425 | // filter为非整形值时, 调用filter_id取得过滤id |
||
1426 | $value = filter_var($value, is_int($filter) ? $filter : filter_id($filter)); |
||
1427 | if (false === $value) { |
||
1428 | $value = $default; |
||
1429 | break; |
||
1430 | } |
||
1431 | } |
||
1432 | } |
||
1433 | } |
||
1434 | |||
1435 | return $value; |
||
1436 | } |
||
1437 | |||
1438 | /** |
||
1439 | * 是否存在某个请求参数 |
||
1440 | * @access public |
||
1441 | * @param string $name 变量名 |
||
1442 | * @param string $type 变量类型 |
||
1443 | * @param bool $checkEmpty 是否检测空值 |
||
1444 | * @return bool |
||
1445 | */ |
||
1446 | public function has(string $name, string $type = 'param', bool $checkEmpty = false): bool |
||
1447 | { |
||
1448 | if (!in_array($type, ['param', 'get', 'post', 'put', 'patch', 'route', 'delete', 'cookie', 'session', 'env', 'request', 'server', 'header', 'file'])) { |
||
1449 | return false; |
||
1450 | } |
||
1451 | |||
1452 | $param = empty($this->$type) ? $this->$type() : $this->$type; |
||
1453 | |||
1454 | if (is_object($param)) { |
||
1455 | return $param->has($name); |
||
1456 | } |
||
1457 | |||
1458 | // 按.拆分成多维数组进行判断 |
||
1459 | foreach (explode('.', $name) as $val) { |
||
1460 | if (isset($param[$val])) { |
||
1461 | $param = $param[$val]; |
||
1462 | } else { |
||
1463 | return false; |
||
1464 | } |
||
1465 | } |
||
1466 | |||
1467 | return ($checkEmpty && '' === $param) ? false : true; |
||
1468 | } |
||
1469 | |||
1470 | /** |
||
1471 | * 获取指定的参数 |
||
1472 | * @access public |
||
1473 | * @param array $name 变量名 |
||
1474 | * @param mixed $data 数据或者变量类型 |
||
1475 | * @param string|array $filter 过滤方法 |
||
1476 | * @return array |
||
1477 | */ |
||
1478 | public function only(array $name, $data = 'param', $filter = ''): array |
||
1479 | { |
||
1480 | $data = is_array($data) ? $data : $this->$data(); |
||
1481 | |||
1482 | $item = []; |
||
1483 | foreach ($name as $key => $val) { |
||
1484 | |||
1485 | if (is_int($key)) { |
||
1486 | $default = null; |
||
1487 | $key = $val; |
||
1488 | if (!isset($data[$key])) { |
||
1489 | continue; |
||
1490 | } |
||
1491 | } else { |
||
1492 | $default = $val; |
||
1493 | } |
||
1494 | |||
1495 | $item[$key] = $this->filterData($data[$key] ?? $default, $filter, $key, $default); |
||
1496 | } |
||
1497 | |||
1498 | return $item; |
||
1499 | } |
||
1500 | |||
1501 | /** |
||
1502 | * 排除指定参数获取 |
||
1503 | * @access public |
||
1504 | * @param array $name 变量名 |
||
1505 | * @param string $type 变量类型 |
||
1506 | * @return mixed |
||
1507 | */ |
||
1508 | public function except(array $name, string $type = 'param'): array |
||
1509 | { |
||
1510 | $param = $this->$type(); |
||
1511 | |||
1512 | foreach ($name as $key) { |
||
1513 | if (isset($param[$key])) { |
||
1514 | unset($param[$key]); |
||
1515 | } |
||
1516 | } |
||
1517 | |||
1518 | return $param; |
||
1519 | } |
||
1520 | |||
1521 | /** |
||
1522 | * 当前是否ssl |
||
1523 | * @access public |
||
1524 | * @return bool |
||
1525 | */ |
||
1526 | public function isSsl(): bool |
||
1527 | { |
||
1528 | if ($this->server('HTTPS') && ('1' == $this->server('HTTPS') || 'on' == strtolower($this->server('HTTPS')))) { |
||
1529 | return true; |
||
1530 | } elseif ('https' == $this->server('REQUEST_SCHEME')) { |
||
1531 | return true; |
||
1532 | } elseif ('443' == $this->server('SERVER_PORT')) { |
||
1533 | return true; |
||
1534 | } elseif ('https' == $this->server('HTTP_X_FORWARDED_PROTO')) { |
||
1535 | return true; |
||
1536 | } elseif ($this->httpsAgentName && $this->server($this->httpsAgentName)) { |
||
1537 | return true; |
||
1538 | } |
||
1539 | |||
1540 | return false; |
||
1541 | } |
||
1542 | |||
1543 | /** |
||
1544 | * 当前是否JSON请求 |
||
1545 | * @access public |
||
1546 | * @return bool |
||
1547 | */ |
||
1548 | 9 | public function isJson(): bool |
|
1549 | { |
||
1550 | 9 | $contentType = $this->contentType(); |
|
1551 | 9 | $acceptType = $this->type(); |
|
1552 | |||
1553 | 9 | return false !== strpos($contentType, 'json') || false !== strpos($acceptType, 'json'); |
|
1554 | } |
||
1555 | |||
1556 | /** |
||
1557 | * 当前是否Ajax请求 |
||
1558 | * @access public |
||
1559 | * @param bool $ajax true 获取原始ajax请求 |
||
1560 | * @return bool |
||
1561 | */ |
||
1562 | public function isAjax(bool $ajax = false): bool |
||
1563 | { |
||
1564 | $value = $this->server('HTTP_X_REQUESTED_WITH'); |
||
1565 | $result = $value && 'xmlhttprequest' == strtolower($value) ? true : false; |
||
1566 | |||
1567 | if (true === $ajax) { |
||
1568 | return $result; |
||
1569 | } |
||
1570 | |||
1571 | return $this->param($this->varAjax) ? true : $result; |
||
1572 | } |
||
1573 | |||
1574 | /** |
||
1575 | * 当前是否Pjax请求 |
||
1576 | * @access public |
||
1577 | * @param bool $pjax true 获取原始pjax请求 |
||
1578 | * @return bool |
||
1579 | */ |
||
1580 | public function isPjax(bool $pjax = false): bool |
||
1581 | { |
||
1582 | $result = !is_null($this->server('HTTP_X_PJAX')) ? true : false; |
||
1583 | |||
1584 | if (true === $pjax) { |
||
1585 | return $result; |
||
1586 | } |
||
1587 | |||
1588 | return $this->param($this->varPjax) ? true : $result; |
||
1589 | } |
||
1590 | |||
1591 | /** |
||
1592 | * 获取客户端IP地址 |
||
1593 | * @access public |
||
1594 | * @return string |
||
1595 | */ |
||
1596 | public function ip(): string |
||
1597 | { |
||
1598 | if (!empty($this->realIP)) { |
||
1599 | return $this->realIP; |
||
1600 | } |
||
1601 | |||
1602 | $this->realIP = $this->server('REMOTE_ADDR', ''); |
||
1603 | |||
1604 | // 如果指定了前端代理服务器IP以及其会发送的IP头 |
||
1605 | // 则尝试获取前端代理服务器发送过来的真实IP |
||
1606 | $proxyIp = $this->proxyServerIp; |
||
1607 | $proxyIpHeader = $this->proxyServerIpHeader; |
||
1608 | |||
1609 | if (count($proxyIp) > 0 && count($proxyIpHeader) > 0) { |
||
1610 | // 从指定的HTTP头中依次尝试获取IP地址 |
||
1611 | // 直到获取到一个合法的IP地址 |
||
1612 | foreach ($proxyIpHeader as $header) { |
||
1613 | $tempIP = $this->server($header); |
||
1614 | |||
1615 | if (empty($tempIP)) { |
||
1616 | continue; |
||
1617 | } |
||
1618 | |||
1619 | $tempIP = trim(explode(',', $tempIP)[0]); |
||
1620 | |||
1621 | if (!$this->isValidIP($tempIP)) { |
||
1622 | $tempIP = null; |
||
1623 | } else { |
||
1624 | break; |
||
1625 | } |
||
1626 | } |
||
1627 | |||
1628 | // tempIP不为空,说明获取到了一个IP地址 |
||
1629 | // 这时我们检查 REMOTE_ADDR 是不是指定的前端代理服务器之一 |
||
1630 | // 如果是的话说明该 IP头 是由前端代理服务器设置的 |
||
1631 | // 否则则是伪装的 |
||
1632 | if ($tempIP) { |
||
1633 | $realIPBin = $this->ip2bin($this->realIP); |
||
1634 | |||
1635 | foreach ($proxyIp as $ip) { |
||
1636 | $serverIPElements = explode('/', $ip); |
||
1637 | $serverIP = $serverIPElements[0]; |
||
1638 | $serverIPPrefix = $serverIPElements[1] ?? 128; |
||
1639 | $serverIPBin = $this->ip2bin($serverIP); |
||
1640 | |||
1641 | // IP类型不符 |
||
1642 | if (strlen($realIPBin) !== strlen($serverIPBin)) { |
||
1643 | continue; |
||
1644 | } |
||
1645 | |||
1646 | if (strncmp($realIPBin, $serverIPBin, (int) $serverIPPrefix) === 0) { |
||
1647 | $this->realIP = $tempIP; |
||
1648 | break; |
||
1649 | } |
||
1650 | } |
||
1651 | } |
||
1652 | } |
||
1653 | |||
1654 | if (!$this->isValidIP($this->realIP)) { |
||
1655 | $this->realIP = '0.0.0.0'; |
||
1656 | } |
||
1657 | |||
1658 | return $this->realIP; |
||
1659 | } |
||
1660 | |||
1661 | /** |
||
1662 | * 检测是否是合法的IP地址 |
||
1663 | * |
||
1664 | * @param string $ip IP地址 |
||
1665 | * @param string $type IP地址类型 (ipv4, ipv6) |
||
1666 | * |
||
1667 | * @return boolean |
||
1668 | */ |
||
1669 | public function isValidIP(string $ip, string $type = ''): bool |
||
1670 | { |
||
1671 | switch (strtolower($type)) { |
||
1672 | case 'ipv4': |
||
1673 | $flag = FILTER_FLAG_IPV4; |
||
1674 | break; |
||
1675 | case 'ipv6': |
||
1676 | $flag = FILTER_FLAG_IPV6; |
||
1677 | break; |
||
1678 | default: |
||
1679 | $flag = null; |
||
1680 | break; |
||
1681 | } |
||
1682 | |||
1683 | return boolval(filter_var($ip, FILTER_VALIDATE_IP, $flag)); |
||
1684 | } |
||
1685 | |||
1686 | /** |
||
1687 | * 将IP地址转换为二进制字符串 |
||
1688 | * |
||
1689 | * @param string $ip |
||
1690 | * |
||
1691 | * @return string |
||
1692 | */ |
||
1693 | public function ip2bin(string $ip): string |
||
1694 | { |
||
1695 | if ($this->isValidIP($ip, 'ipv6')) { |
||
1696 | $IPHex = str_split(bin2hex(inet_pton($ip)), 4); |
||
1697 | foreach ($IPHex as $key => $value) { |
||
1698 | $IPHex[$key] = intval($value, 16); |
||
1699 | } |
||
1700 | $IPBin = vsprintf('%016b%016b%016b%016b%016b%016b%016b%016b', $IPHex); |
||
1701 | } else { |
||
1702 | $IPHex = str_split(bin2hex(inet_pton($ip)), 2); |
||
1703 | foreach ($IPHex as $key => $value) { |
||
1704 | $IPHex[$key] = intval($value, 16); |
||
1705 | } |
||
1706 | $IPBin = vsprintf('%08b%08b%08b%08b', $IPHex); |
||
1707 | } |
||
1708 | |||
1709 | return $IPBin; |
||
1710 | } |
||
1711 | |||
1712 | /** |
||
1713 | * 检测是否使用手机访问 |
||
1714 | * @access public |
||
1715 | * @return bool |
||
1716 | */ |
||
1717 | public function isMobile(): bool |
||
1718 | { |
||
1719 | if ($this->server('HTTP_VIA') && stristr($this->server('HTTP_VIA'), "wap")) { |
||
1720 | return true; |
||
1721 | } elseif ($this->server('HTTP_ACCEPT') && strpos(strtoupper($this->server('HTTP_ACCEPT')), "VND.WAP.WML")) { |
||
1722 | return true; |
||
1723 | } elseif ($this->server('HTTP_X_WAP_PROFILE') || $this->server('HTTP_PROFILE')) { |
||
1724 | return true; |
||
1725 | } elseif ($this->server('HTTP_USER_AGENT') && preg_match('/(blackberry|configuration\/cldc|hp |hp-|htc |htc_|htc-|iemobile|kindle|midp|mmp|motorola|mobile|nokia|opera mini|opera |Googlebot-Mobile|YahooSeeker\/M1A1-R2D2|android|iphone|ipod|mobi|palm|palmos|pocket|portalmmm|ppc;|smartphone|sonyericsson|sqh|spv|symbian|treo|up.browser|up.link|vodafone|windows ce|xda |xda_)/i', $this->server('HTTP_USER_AGENT'))) { |
||
1726 | return true; |
||
1727 | } |
||
1728 | |||
1729 | return false; |
||
1730 | } |
||
1731 | |||
1732 | /** |
||
1733 | * 当前URL地址中的scheme参数 |
||
1734 | * @access public |
||
1735 | * @return string |
||
1736 | */ |
||
1737 | public function scheme(): string |
||
1738 | { |
||
1739 | return $this->isSsl() ? 'https' : 'http'; |
||
1740 | } |
||
1741 | |||
1742 | /** |
||
1743 | * 当前请求URL地址中的query参数 |
||
1744 | * @access public |
||
1745 | * @return string |
||
1746 | */ |
||
1747 | public function query(): string |
||
1748 | { |
||
1749 | return $this->server('QUERY_STRING', ''); |
||
1750 | } |
||
1751 | |||
1752 | /** |
||
1753 | * 设置当前请求的host(包含端口) |
||
1754 | * @access public |
||
1755 | * @param string $host 主机名(含端口) |
||
1756 | * @return $this |
||
1757 | */ |
||
1758 | public function setHost(string $host) |
||
1759 | { |
||
1760 | $this->host = $host; |
||
1761 | |||
1762 | return $this; |
||
1763 | } |
||
1764 | |||
1765 | /** |
||
1766 | * 当前请求的host |
||
1767 | * @access public |
||
1768 | * @param bool $strict true 仅仅获取HOST |
||
1769 | * @return string |
||
1770 | */ |
||
1771 | 3 | public function host(bool $strict = false): string |
|
1772 | { |
||
1773 | 3 | if ($this->host) { |
|
1774 | $host = $this->host; |
||
1775 | } else { |
||
1776 | 3 | $host = strval($this->server('HTTP_X_REAL_HOST') ?: $this->server('HTTP_HOST')); |
|
1777 | } |
||
1778 | |||
1779 | 3 | return true === $strict && strpos($host, ':') ? strstr($host, ':', true) : $host; |
|
1780 | } |
||
1781 | |||
1782 | /** |
||
1783 | * 当前请求URL地址中的port参数 |
||
1784 | * @access public |
||
1785 | * @return string |
||
1786 | */ |
||
1787 | public function port(): string |
||
1788 | { |
||
1789 | return $this->server('SERVER_PORT', ''); |
||
1790 | } |
||
1791 | |||
1792 | /** |
||
1793 | * 当前请求 SERVER_PROTOCOL |
||
1794 | * @access public |
||
1795 | * @return string |
||
1796 | */ |
||
1797 | public function protocol(): string |
||
1800 | } |
||
1801 | |||
1802 | /** |
||
1803 | * 当前请求 REMOTE_PORT |
||
1804 | * @access public |
||
1805 | * @return string |
||
1806 | */ |
||
1807 | public function remotePort(): string |
||
1810 | } |
||
1811 | |||
1812 | /** |
||
1813 | * 当前请求 HTTP_CONTENT_TYPE |
||
1814 | * @access public |
||
1815 | * @return string |
||
1816 | */ |
||
1817 | 9 | public function contentType(): string |
|
1831 | } |
||
1832 | |||
1833 | /** |
||
1834 | * 获取当前请求的安全Key |
||
1835 | * @access public |
||
1836 | * @return string |
||
1837 | */ |
||
1838 | public function secureKey(): string |
||
1839 | { |
||
1840 | if (is_null($this->secureKey)) { |
||
1841 | $this->secureKey = uniqid('', true); |
||
1842 | } |
||
1843 | |||
1844 | return $this->secureKey; |
||
1845 | } |
||
1846 | |||
1847 | /** |
||
1848 | * 设置当前的应用名 |
||
1849 | * @access public |
||
1850 | * @param string $app 应用名 |
||
1851 | * @return $this |
||
1852 | */ |
||
1853 | 5 | public function setApp(string $app) |
|
1854 | { |
||
1855 | 5 | $this->app = $app; |
|
1856 | 5 | return $this; |
|
1857 | } |
||
1858 | |||
1859 | /** |
||
1860 | * 设置当前的控制器名 |
||
1861 | * @access public |
||
1862 | * @param string $controller 控制器名 |
||
1863 | * @return $this |
||
1864 | */ |
||
1865 | public function setController(string $controller) |
||
1866 | { |
||
1867 | $this->controller = $controller; |
||
1868 | return $this; |
||
1869 | } |
||
1870 | |||
1871 | /** |
||
1872 | * 设置当前的操作名 |
||
1873 | * @access public |
||
1874 | * @param string $action 操作名 |
||
1875 | * @return $this |
||
1876 | */ |
||
1877 | public function setAction(string $action) |
||
1878 | { |
||
1879 | $this->action = $action; |
||
1880 | return $this; |
||
1881 | } |
||
1882 | |||
1883 | /** |
||
1884 | * 获取当前的应用名 |
||
1885 | * @access public |
||
1886 | * @return string |
||
1887 | */ |
||
1888 | public function app(): string |
||
1889 | { |
||
1890 | return $this->app ?: ''; |
||
1891 | } |
||
1892 | |||
1893 | /** |
||
1894 | * 获取当前的控制器名 |
||
1895 | * @access public |
||
1896 | * @param bool $convert 转换为小写 |
||
1897 | * @return string |
||
1898 | */ |
||
1899 | public function controller(bool $convert = false): string |
||
1900 | { |
||
1901 | $name = $this->controller ?: ''; |
||
1902 | return $convert ? strtolower($name) : $name; |
||
1903 | } |
||
1904 | |||
1905 | /** |
||
1906 | * 获取当前的操作名 |
||
1907 | * @access public |
||
1908 | * @param bool $convert 转换为小写 |
||
1909 | * @return string |
||
1910 | */ |
||
1911 | public function action(bool $convert = false): string |
||
1912 | { |
||
1913 | $name = $this->action ?: ''; |
||
1914 | return $convert ? strtolower($name) : $name; |
||
1915 | } |
||
1916 | |||
1917 | /** |
||
1918 | * 设置或者获取当前请求的content |
||
1919 | * @access public |
||
1920 | * @return string |
||
1921 | */ |
||
1922 | public function getContent(): string |
||
1929 | } |
||
1930 | |||
1931 | /** |
||
1932 | * 获取当前请求的php://input |
||
1933 | * @access public |
||
1934 | * @return string |
||
1935 | */ |
||
1936 | public function getInput(): string |
||
1937 | { |
||
1938 | return $this->input; |
||
1939 | } |
||
1940 | |||
1941 | /** |
||
1942 | * 生成请求令牌 |
||
1943 | * @access public |
||
1944 | * @param string $name 令牌名称 |
||
1945 | * @param mixed $type 令牌生成方法 |
||
1946 | * @return string |
||
1947 | */ |
||
1948 | public function buildToken(string $name = '__token__', $type = 'md5'): string |
||
1949 | { |
||
1950 | $type = is_callable($type) ? $type : 'md5'; |
||
1951 | $token = call_user_func($type, $this->server('REQUEST_TIME_FLOAT')); |
||
1952 | |||
1953 | $this->session->set($name, $token); |
||
1954 | |||
1955 | return $token; |
||
1956 | } |
||
1957 | |||
1958 | /** |
||
1959 | * 检查请求令牌 |
||
1960 | * @access public |
||
1961 | * @param string $name 令牌名称 |
||
1962 | * @param array $data 表单数据 |
||
1963 | * @return bool |
||
1964 | */ |
||
1965 | public function checkToken(string $token = '__token__', array $data = []): bool |
||
1966 | { |
||
1967 | if (in_array($this->method(), ['GET', 'HEAD', 'OPTIONS'], true)) { |
||
1968 | return true; |
||
1969 | } |
||
1970 | |||
1971 | if (!$this->session->has($token)) { |
||
1972 | // 令牌数据无效 |
||
1973 | return false; |
||
1974 | } |
||
1975 | |||
1976 | // Header验证 |
||
1977 | if ($this->header('X-CSRF-TOKEN') && $this->session->get($token) === $this->header('X-CSRF-TOKEN')) { |
||
1978 | // 防止重复提交 |
||
1979 | $this->session->delete($token); // 验证完成销毁session |
||
1980 | return true; |
||
1981 | } |
||
1982 | |||
1983 | if (empty($data)) { |
||
1984 | $data = $this->post(); |
||
1985 | } |
||
1986 | |||
1987 | // 令牌验证 |
||
1988 | if (isset($data[$token]) && $this->session->get($token) === $data[$token]) { |
||
1989 | // 防止重复提交 |
||
1990 | $this->session->delete($token); // 验证完成销毁session |
||
1991 | return true; |
||
1992 | } |
||
1993 | |||
1994 | // 开启TOKEN重置 |
||
1995 | $this->session->delete($token); |
||
1996 | return false; |
||
1997 | } |
||
1998 | |||
1999 | /** |
||
2000 | * 设置在中间件传递的数据 |
||
2001 | * @access public |
||
2002 | * @param array $middleware 数据 |
||
2003 | * @return $this |
||
2004 | */ |
||
2005 | public function withMiddleware(array $middleware) |
||
2006 | { |
||
2007 | $this->middleware = array_merge($this->middleware, $middleware); |
||
2008 | return $this; |
||
2009 | } |
||
2010 | |||
2011 | /** |
||
2012 | * 设置GET数据 |
||
2013 | * @access public |
||
2014 | * @param array $get 数据 |
||
2015 | * @return $this |
||
2016 | */ |
||
2017 | public function withGet(array $get) |
||
2018 | { |
||
2019 | $this->get = $get; |
||
2020 | return $this; |
||
2021 | } |
||
2022 | |||
2023 | /** |
||
2024 | * 设置POST数据 |
||
2025 | * @access public |
||
2026 | * @param array $post 数据 |
||
2027 | * @return $this |
||
2028 | */ |
||
2029 | public function withPost(array $post) |
||
2033 | } |
||
2034 | |||
2035 | /** |
||
2036 | * 设置COOKIE数据 |
||
2037 | * @access public |
||
2038 | * @param array $cookie 数据 |
||
2039 | * @return $this |
||
2040 | */ |
||
2041 | public function withCookie(array $cookie) |
||
2042 | { |
||
2043 | $this->cookie = $cookie; |
||
2044 | return $this; |
||
2045 | } |
||
2046 | |||
2047 | /** |
||
2048 | * 设置SESSION数据 |
||
2049 | * @access public |
||
2050 | * @param Session $session 数据 |
||
2051 | * @return $this |
||
2052 | */ |
||
2053 | public function withSession(Session $session) |
||
2054 | { |
||
2055 | $this->session = $session; |
||
2056 | return $this; |
||
2057 | } |
||
2058 | |||
2059 | /** |
||
2060 | * 设置SERVER数据 |
||
2061 | * @access public |
||
2062 | * @param array $server 数据 |
||
2063 | * @return $this |
||
2064 | */ |
||
2065 | public function withServer(array $server) |
||
2066 | { |
||
2067 | $this->server = array_change_key_case($server, CASE_UPPER); |
||
2068 | return $this; |
||
2069 | } |
||
2070 | |||
2071 | /** |
||
2072 | * 设置HEADER数据 |
||
2073 | * @access public |
||
2074 | * @param array $header 数据 |
||
2075 | * @return $this |
||
2076 | */ |
||
2077 | public function withHeader(array $header) |
||
2078 | { |
||
2079 | $this->header = array_change_key_case($header); |
||
2080 | return $this; |
||
2081 | } |
||
2082 | |||
2083 | /** |
||
2084 | * 设置ENV数据 |
||
2085 | * @access public |
||
2086 | * @param Env $env 数据 |
||
2087 | * @return $this |
||
2088 | */ |
||
2089 | public function withEnv(Env $env) |
||
2090 | { |
||
2091 | $this->env = $env; |
||
2092 | return $this; |
||
2093 | } |
||
2094 | |||
2095 | /** |
||
2096 | * 设置php://input数据 |
||
2097 | * @access public |
||
2098 | * @param string $input RAW数据 |
||
2099 | * @return $this |
||
2100 | */ |
||
2101 | public function withInput(string $input) |
||
2102 | { |
||
2103 | $this->input = $input; |
||
2104 | return $this; |
||
2105 | } |
||
2106 | |||
2107 | /** |
||
2108 | * 设置文件上传数据 |
||
2109 | * @access public |
||
2110 | * @param array $files 上传信息 |
||
2111 | * @return $this |
||
2112 | */ |
||
2113 | public function withFiles(array $files) |
||
2114 | { |
||
2115 | $this->file = $files; |
||
2116 | return $this; |
||
2117 | } |
||
2118 | |||
2119 | /** |
||
2120 | * 设置ROUTE变量 |
||
2121 | * @access public |
||
2122 | * @param array $route 数据 |
||
2123 | * @return $this |
||
2124 | */ |
||
2125 | public function withRoute(array $route) |
||
2126 | { |
||
2127 | $this->route = $route; |
||
2128 | return $this; |
||
2129 | } |
||
2130 | |||
2131 | /** |
||
2132 | * 设置中间传递数据 |
||
2133 | * @access public |
||
2134 | * @param string $name 参数名 |
||
2135 | * @param mixed $value 值 |
||
2136 | */ |
||
2137 | public function __set(string $name, $value) |
||
2140 | } |
||
2141 | |||
2142 | /** |
||
2143 | * 获取中间传递数据的值 |
||
2144 | * @access public |
||
2145 | * @param string $name 名称 |
||
2146 | * @return mixed |
||
2147 | */ |
||
2148 | public function __get(string $name) |
||
2149 | { |
||
2150 | return $this->middleware($name); |
||
2151 | } |
||
2152 | |||
2153 | /** |
||
2154 | * 检测请求数据的值 |
||
2155 | * @access public |
||
2156 | * @param string $name 名称 |
||
2157 | * @return boolean |
||
2158 | */ |
||
2159 | public function __isset(string $name): bool |
||
2160 | { |
||
2161 | return isset($this->param[$name]); |
||
2162 | } |
||
2163 | } |
||
2164 |