Total Complexity | 325 |
Total Lines | 2198 |
Duplicated Lines | 0 % |
Coverage | 3.61% |
Changes | 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 |
||
20 | class Request |
||
21 | { |
||
22 | /** |
||
23 | * 配置 |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $config = [ |
||
27 | // PATHINFO变量名 用于兼容模式 |
||
28 | 'var_pathinfo' => 's', |
||
29 | // 兼容PATH_INFO获取 |
||
30 | 'pathinfo_fetch' => ['ORIG_PATH_INFO', 'REDIRECT_PATH_INFO', 'REDIRECT_URL'], |
||
31 | // 表单请求类型伪装变量 |
||
32 | 'var_method' => '_method', |
||
33 | // 表单ajax伪装变量 |
||
34 | 'var_ajax' => '_ajax', |
||
35 | // 表单pjax伪装变量 |
||
36 | 'var_pjax' => '_pjax', |
||
37 | // 默认全局过滤方法 用逗号分隔多个 |
||
38 | 'default_filter' => '', |
||
39 | // 域名根,如thinkphp.cn |
||
40 | 'url_domain_root' => '', |
||
41 | // HTTPS代理标识 |
||
42 | 'https_agent_name' => '', |
||
43 | // 前端代理服务器IP |
||
44 | 'proxy_server_ip' => [], |
||
45 | // 前端代理服务器真实IP头 |
||
46 | 'proxy_server_ip_header' => ['HTTP_X_REAL_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP', 'HTTP_X_CLIENT_IP', 'HTTP_X_CLUSTER_CLIENT_IP'], |
||
47 | // URL伪静态后缀 |
||
48 | 'url_html_suffix' => 'html', |
||
49 | // 是否开启请求缓存 true自动缓存 支持设置请求缓存规则 |
||
50 | 'request_cache' => false, |
||
51 | // 请求缓存有效期 |
||
52 | 'request_cache_expire' => null, |
||
53 | // 全局请求缓存排除规则 |
||
54 | 'request_cache_except' => [], |
||
55 | // 请求缓存的Tag |
||
56 | 'request_cache_tag' => '', |
||
57 | ]; |
||
58 | |||
59 | /** |
||
60 | * 请求类型 |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $method; |
||
64 | |||
65 | /** |
||
66 | * 域名(含协议及端口) |
||
67 | * @var string |
||
68 | */ |
||
69 | protected $domain; |
||
70 | |||
71 | /** |
||
72 | * HOST(含端口) |
||
73 | * @var string |
||
74 | */ |
||
75 | protected $host; |
||
76 | |||
77 | /** |
||
78 | * 子域名 |
||
79 | * @var string |
||
80 | */ |
||
81 | protected $subDomain; |
||
82 | |||
83 | /** |
||
84 | * 泛域名 |
||
85 | * @var string |
||
86 | */ |
||
87 | protected $panDomain; |
||
88 | |||
89 | /** |
||
90 | * 当前URL地址 |
||
91 | * @var string |
||
92 | */ |
||
93 | protected $url; |
||
94 | |||
95 | /** |
||
96 | * 基础URL |
||
97 | * @var string |
||
98 | */ |
||
99 | protected $baseUrl; |
||
100 | |||
101 | /** |
||
102 | * 当前执行的文件 |
||
103 | * @var string |
||
104 | */ |
||
105 | protected $baseFile; |
||
106 | |||
107 | /** |
||
108 | * 访问的ROOT地址 |
||
109 | * @var string |
||
110 | */ |
||
111 | protected $root; |
||
112 | |||
113 | /** |
||
114 | * pathinfo |
||
115 | * @var string |
||
116 | */ |
||
117 | protected $pathinfo; |
||
118 | |||
119 | /** |
||
120 | * pathinfo(不含后缀) |
||
121 | * @var string |
||
122 | */ |
||
123 | protected $path; |
||
124 | |||
125 | /** |
||
126 | * 当前请求的IP地址 |
||
127 | * @var string |
||
128 | */ |
||
129 | protected $realIP; |
||
130 | |||
131 | /** |
||
132 | * 当前调度信息 |
||
133 | * @var Dispatch |
||
134 | */ |
||
135 | protected $dispatch; |
||
136 | |||
137 | /** |
||
138 | * 当前应用名 |
||
139 | * @var string |
||
140 | */ |
||
141 | protected $app; |
||
142 | |||
143 | /** |
||
144 | * 当前控制器名 |
||
145 | * @var string |
||
146 | */ |
||
147 | protected $controller; |
||
148 | |||
149 | /** |
||
150 | * 当前操作名 |
||
151 | * @var string |
||
152 | */ |
||
153 | protected $action; |
||
154 | |||
155 | /** |
||
156 | * 当前请求参数 |
||
157 | * @var array |
||
158 | */ |
||
159 | protected $param = []; |
||
160 | |||
161 | /** |
||
162 | * 当前GET参数 |
||
163 | * @var array |
||
164 | */ |
||
165 | protected $get = []; |
||
166 | |||
167 | /** |
||
168 | * 当前POST参数 |
||
169 | * @var array |
||
170 | */ |
||
171 | protected $post = []; |
||
172 | |||
173 | /** |
||
174 | * 当前REQUEST参数 |
||
175 | * @var array |
||
176 | */ |
||
177 | protected $request = []; |
||
178 | |||
179 | /** |
||
180 | * 当前ROUTE参数 |
||
181 | * @var array |
||
182 | */ |
||
183 | protected $route = []; |
||
184 | |||
185 | /** |
||
186 | * 中间件传递的参数 |
||
187 | * @var array |
||
188 | */ |
||
189 | protected $middleware = []; |
||
190 | |||
191 | /** |
||
192 | * 当前PUT参数 |
||
193 | * @var array |
||
194 | */ |
||
195 | protected $put; |
||
196 | |||
197 | /** |
||
198 | * SESSION对象 |
||
199 | * @var Session |
||
200 | */ |
||
201 | protected $session; |
||
202 | |||
203 | /** |
||
204 | * COOKIE数据 |
||
205 | * @var array |
||
206 | */ |
||
207 | protected $cookie = []; |
||
208 | |||
209 | /** |
||
210 | * ENV对象 |
||
211 | * @var Env |
||
212 | */ |
||
213 | protected $env; |
||
214 | |||
215 | /** |
||
216 | * 当前SERVER参数 |
||
217 | * @var array |
||
218 | */ |
||
219 | protected $server = []; |
||
220 | |||
221 | /** |
||
222 | * 当前FILE参数 |
||
223 | * @var array |
||
224 | */ |
||
225 | protected $file = []; |
||
226 | |||
227 | /** |
||
228 | * 当前HEADER参数 |
||
229 | * @var array |
||
230 | */ |
||
231 | protected $header = []; |
||
232 | |||
233 | /** |
||
234 | * 资源类型定义 |
||
235 | * @var array |
||
236 | */ |
||
237 | protected $mimeType = [ |
||
238 | 'xml' => 'application/xml,text/xml,application/x-xml', |
||
239 | 'json' => 'application/json,text/x-json,application/jsonrequest,text/json', |
||
240 | 'js' => 'text/javascript,application/javascript,application/x-javascript', |
||
241 | 'css' => 'text/css', |
||
242 | 'rss' => 'application/rss+xml', |
||
243 | 'yaml' => 'application/x-yaml,text/yaml', |
||
244 | 'atom' => 'application/atom+xml', |
||
245 | 'pdf' => 'application/pdf', |
||
246 | 'text' => 'text/plain', |
||
247 | 'image' => 'image/png,image/jpg,image/jpeg,image/pjpeg,image/gif,image/webp,image/*', |
||
248 | 'csv' => 'text/csv', |
||
249 | 'html' => 'text/html,application/xhtml+xml,*/*', |
||
250 | ]; |
||
251 | |||
252 | /** |
||
253 | * 当前请求内容 |
||
254 | * @var string |
||
255 | */ |
||
256 | protected $content; |
||
257 | |||
258 | /** |
||
259 | * 全局过滤规则 |
||
260 | * @var array |
||
261 | */ |
||
262 | protected $filter; |
||
263 | |||
264 | /** |
||
265 | * php://input内容 |
||
266 | * @var string |
||
267 | */ |
||
268 | // php://input |
||
269 | protected $input; |
||
270 | |||
271 | /** |
||
272 | * 请求缓存 |
||
273 | * @var array |
||
274 | */ |
||
275 | protected $cache; |
||
276 | |||
277 | /** |
||
278 | * 缓存是否检查 |
||
279 | * @var bool |
||
280 | */ |
||
281 | protected $isCheckCache; |
||
282 | |||
283 | /** |
||
284 | * 请求安全Key |
||
285 | * @var string |
||
286 | */ |
||
287 | protected $secureKey; |
||
288 | |||
289 | /** |
||
290 | * 是否合并Param |
||
291 | * @var bool |
||
292 | */ |
||
293 | protected $mergeParam = false; |
||
294 | |||
295 | /** |
||
296 | * 架构函数 |
||
297 | * @access public |
||
298 | * @param array $options 参数 |
||
299 | */ |
||
300 | public function __construct(array $options = []) |
||
301 | { |
||
302 | $this->init($options); |
||
303 | |||
304 | // 保存 php://input |
||
305 | $this->input = file_get_contents('php://input'); |
||
306 | } |
||
307 | |||
308 | public function init(array $options = []): void |
||
309 | { |
||
310 | $this->config = array_merge($this->config, $options); |
||
311 | |||
312 | if (is_null($this->filter) && !empty($this->config['default_filter'])) { |
||
313 | $this->filter = $this->config['default_filter']; |
||
314 | } |
||
315 | } |
||
316 | |||
317 | public function config($name = null) |
||
318 | { |
||
319 | if (is_null($name)) { |
||
320 | return $this->config; |
||
321 | } |
||
322 | |||
323 | return $this->config[$name] ?? null; |
||
324 | } |
||
325 | |||
326 | public static function __make(App $app, Config $config) |
||
2 ignored issues
–
show
|
|||
327 | { |
||
328 | $request = new static($config->get('route')); |
||
329 | |||
330 | $request->server = $_SERVER; |
||
331 | $request->env = $app->env; |
||
332 | $request->get = $_GET; |
||
333 | $request->post = $_POST ?: $request->getInputData($request->input); |
||
334 | $request->put = $request->getInputData($request->input); |
||
335 | $request->request = $_REQUEST; |
||
336 | $request->cookie = $_COOKIE; |
||
337 | $request->file = $_FILES ?? []; |
||
338 | |||
339 | if (function_exists('apache_request_headers') && $result = apache_request_headers()) { |
||
340 | $header = $result; |
||
341 | } else { |
||
342 | $header = []; |
||
343 | $server = $_SERVER; |
||
344 | foreach ($server as $key => $val) { |
||
345 | if (0 === strpos($key, 'HTTP_')) { |
||
346 | $key = str_replace('_', '-', strtolower(substr($key, 5))); |
||
347 | $header[$key] = $val; |
||
348 | } |
||
349 | } |
||
350 | if (isset($server['CONTENT_TYPE'])) { |
||
351 | $header['content-type'] = $server['CONTENT_TYPE']; |
||
352 | } |
||
353 | if (isset($server['CONTENT_LENGTH'])) { |
||
354 | $header['content-length'] = $server['CONTENT_LENGTH']; |
||
355 | } |
||
356 | } |
||
357 | |||
358 | $request->header = array_change_key_case($header); |
||
359 | |||
360 | return $request; |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * 设置当前包含协议的域名 |
||
365 | * @access public |
||
366 | * @param string $domain 域名 |
||
367 | * @return $this |
||
368 | */ |
||
369 | public function setDomain(string $domain) |
||
370 | { |
||
371 | $this->domain = $domain; |
||
372 | return $this; |
||
373 | } |
||
374 | |||
375 | /** |
||
376 | * 获取当前包含协议的域名 |
||
377 | * @access public |
||
378 | * @param bool $port 是否需要去除端口号 |
||
379 | * @return string |
||
380 | */ |
||
381 | public function domain(bool $port = false): string |
||
382 | { |
||
383 | return $this->scheme() . '://' . $this->host($port); |
||
384 | } |
||
385 | |||
386 | /** |
||
387 | * 获取当前根域名 |
||
388 | * @access public |
||
389 | * @return string |
||
390 | */ |
||
391 | public function rootDomain(): string |
||
392 | { |
||
393 | $root = $this->config['url_domain_root']; |
||
394 | |||
395 | if (!$root) { |
||
396 | $item = explode('.', $this->host()); |
||
397 | $count = count($item); |
||
398 | $root = $count > 1 ? $item[$count - 2] . '.' . $item[$count - 1] : $item[0]; |
||
399 | } |
||
400 | |||
401 | return $root; |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * 设置当前泛域名的值 |
||
406 | * @access public |
||
407 | * @param string $domain 域名 |
||
408 | * @return $this |
||
409 | */ |
||
410 | public function setSubDomain(string $domain) |
||
411 | { |
||
412 | $this->subDomain = $domain; |
||
413 | return $this; |
||
414 | } |
||
415 | |||
416 | /** |
||
417 | * 获取当前子域名 |
||
418 | * @access public |
||
419 | * @return string |
||
420 | */ |
||
421 | 3 | public function subDomain(): string |
|
422 | { |
||
423 | 3 | if (is_null($this->subDomain)) { |
|
424 | // 获取当前主域名 |
||
425 | 3 | $rootDomain = $this->config['url_domain_root']; |
|
426 | |||
427 | 3 | if ($rootDomain) { |
|
428 | // 配置域名根 例如 thinkphp.cn 163.com.cn 如果是国家级域名 com.cn net.cn 之类的域名需要配置 |
||
429 | $domain = explode('.', rtrim(stristr($this->host(), $rootDomain, true), '.')); |
||
430 | } else { |
||
431 | 3 | $domain = explode('.', $this->host(), -2); |
|
432 | } |
||
433 | |||
434 | 3 | $this->subDomain = implode('.', $domain); |
|
435 | } |
||
436 | |||
437 | 3 | return $this->subDomain; |
|
438 | } |
||
439 | |||
440 | /** |
||
441 | * 设置当前泛域名的值 |
||
442 | * @access public |
||
443 | * @param string $domain 域名 |
||
444 | * @return $this |
||
445 | */ |
||
446 | public function setPanDomain(string $domain) |
||
447 | { |
||
448 | $this->panDomain = $domain; |
||
449 | return $this; |
||
450 | } |
||
451 | |||
452 | /** |
||
453 | * 获取当前泛域名的值 |
||
454 | * @access public |
||
455 | * @return string |
||
456 | */ |
||
457 | public function panDomain(): string |
||
458 | { |
||
459 | return $this->panDomain ?: ''; |
||
460 | } |
||
461 | |||
462 | /** |
||
463 | * 设置当前完整URL 包括QUERY_STRING |
||
464 | * @access public |
||
465 | * @param string $url URL地址 |
||
466 | * @return $this |
||
467 | */ |
||
468 | public function setUrl(string $url) |
||
469 | { |
||
470 | $this->url = $url; |
||
471 | return $this; |
||
472 | } |
||
473 | |||
474 | /** |
||
475 | * 获取当前完整URL 包括QUERY_STRING |
||
476 | * @access public |
||
477 | * @param bool $complete 是否包含完整域名 |
||
478 | * @return string |
||
479 | */ |
||
480 | public function url(bool $complete = false): string |
||
481 | { |
||
482 | if ($this->url) { |
||
483 | $url = $this->url; |
||
484 | } elseif ($this->server('HTTP_X_REWRITE_URL')) { |
||
485 | $url = $this->server('HTTP_X_REWRITE_URL'); |
||
486 | } elseif ($this->server('REQUEST_URI')) { |
||
487 | $url = $this->server('REQUEST_URI'); |
||
488 | } elseif ($this->server('ORIG_PATH_INFO')) { |
||
489 | $url = $this->server('ORIG_PATH_INFO') . (!empty($this->server('QUERY_STRING')) ? '?' . $this->server('QUERY_STRING') : ''); |
||
490 | } elseif (isset($_SERVER['argv'][1])) { |
||
491 | $url = $_SERVER['argv'][1]; |
||
492 | } else { |
||
493 | $url = ''; |
||
494 | } |
||
495 | |||
496 | return $complete ? $this->domain() . $url : $url; |
||
497 | } |
||
498 | |||
499 | /** |
||
500 | * 设置当前URL 不含QUERY_STRING |
||
501 | * @access public |
||
502 | * @param string $url URL地址 |
||
503 | * @return $this |
||
504 | */ |
||
505 | public function setBaseUrl(string $url) |
||
506 | { |
||
507 | $this->baseUrl = $url; |
||
508 | return $this; |
||
509 | } |
||
510 | |||
511 | /** |
||
512 | * 获取当前URL 不含QUERY_STRING |
||
513 | * @access public |
||
514 | * @param bool $complete 是否包含完整域名 |
||
515 | * @return string |
||
516 | */ |
||
517 | public function baseUrl(bool $complete = false): string |
||
518 | { |
||
519 | if (!$this->baseUrl) { |
||
520 | $str = $this->url(); |
||
521 | $this->baseUrl = strpos($str, '?') ? strstr($str, '?', true) : $str; |
||
522 | } |
||
523 | |||
524 | return $complete ? $this->domain() . $this->baseUrl : $this->baseUrl; |
||
525 | } |
||
526 | |||
527 | /** |
||
528 | * 获取当前执行的文件 SCRIPT_NAME |
||
529 | * @access public |
||
530 | * @param bool $complete 是否包含完整域名 |
||
531 | * @return string |
||
532 | */ |
||
533 | public function baseFile(bool $complete = false): string |
||
534 | { |
||
535 | if (!$this->baseFile) { |
||
536 | $url = ''; |
||
537 | if (!$this->isCli()) { |
||
538 | $script_name = basename($this->server('SCRIPT_FILENAME')); |
||
539 | if (basename($this->server('SCRIPT_NAME')) === $script_name) { |
||
540 | $url = $this->server('SCRIPT_NAME'); |
||
541 | } elseif (basename($this->server('PHP_SELF')) === $script_name) { |
||
542 | $url = $this->server('PHP_SELF'); |
||
543 | } elseif (basename($this->server('ORIG_SCRIPT_NAME')) === $script_name) { |
||
544 | $url = $this->server('ORIG_SCRIPT_NAME'); |
||
545 | } elseif (($pos = strpos($this->server('PHP_SELF'), '/' . $script_name)) !== false) { |
||
546 | $url = substr($this->server('SCRIPT_NAME'), 0, $pos) . '/' . $script_name; |
||
547 | } elseif ($this->server('DOCUMENT_ROOT') && strpos($this->server('SCRIPT_FILENAME'), $this->server('DOCUMENT_ROOT')) === 0) { |
||
548 | $url = str_replace('\\', '/', str_replace($this->server('DOCUMENT_ROOT'), '', $this->server('SCRIPT_FILENAME'))); |
||
549 | } |
||
550 | } |
||
551 | $this->baseFile = $url; |
||
552 | } |
||
553 | |||
554 | return $complete ? $this->domain() . $this->baseFile : $this->baseFile; |
||
555 | } |
||
556 | |||
557 | /** |
||
558 | * 设置URL访问根地址 |
||
559 | * @access public |
||
560 | * @param string $url URL地址 |
||
561 | * @return $this |
||
562 | */ |
||
563 | 2 | public function setRoot(string $url) |
|
564 | { |
||
565 | 2 | $this->root = $url; |
|
566 | 2 | return $this; |
|
567 | } |
||
568 | |||
569 | /** |
||
570 | * 获取URL访问根地址 |
||
571 | * @access public |
||
572 | * @param bool $complete 是否包含完整域名 |
||
573 | * @return string |
||
574 | */ |
||
575 | public function root(bool $complete = false): string |
||
576 | { |
||
577 | if (!$this->root) { |
||
578 | $file = $this->baseFile(); |
||
579 | if ($file && 0 !== strpos($this->url(), $file)) { |
||
580 | $file = str_replace('\\', '/', dirname($file)); |
||
581 | } |
||
582 | $this->root = rtrim($file, '/'); |
||
583 | } |
||
584 | |||
585 | return $complete ? $this->domain() . $this->root : $this->root; |
||
586 | } |
||
587 | |||
588 | /** |
||
589 | * 获取URL访问根目录 |
||
590 | * @access public |
||
591 | * @return string |
||
592 | */ |
||
593 | public function rootUrl(): string |
||
594 | { |
||
595 | $base = $this->root(); |
||
596 | $root = strpos($base, '.') ? ltrim(dirname($base), DIRECTORY_SEPARATOR) : $base; |
||
597 | |||
598 | if ('' != $root) { |
||
599 | $root = '/' . ltrim($root, '/'); |
||
600 | } |
||
601 | |||
602 | return $root; |
||
603 | } |
||
604 | |||
605 | /** |
||
606 | * 设置当前请求的pathinfo |
||
607 | * @access public |
||
608 | * @param string $pathinfo |
||
609 | * @return $this |
||
610 | */ |
||
611 | 2 | public function setPathinfo(string $pathinfo) |
|
612 | { |
||
613 | 2 | $this->pathinfo = $pathinfo; |
|
614 | 2 | return $this; |
|
615 | } |
||
616 | |||
617 | /** |
||
618 | * 获取当前请求URL的pathinfo信息(含URL后缀) |
||
619 | * @access public |
||
620 | * @return string |
||
621 | */ |
||
622 | public function pathinfo(): string |
||
623 | { |
||
624 | if (is_null($this->pathinfo)) { |
||
625 | if (isset($_GET[$this->config['var_pathinfo']])) { |
||
626 | // 判断URL里面是否有兼容模式参数 |
||
627 | $pathinfo = $_GET[$this->config['var_pathinfo']]; |
||
628 | unset($_GET[$this->config['var_pathinfo']]); |
||
629 | } elseif ($this->server('PATH_INFO')) { |
||
630 | $pathinfo = $this->server('PATH_INFO'); |
||
631 | } elseif ($this->server('REQUEST_URI')) { |
||
632 | $pathinfo = strpos($this->server('REQUEST_URI'), '?') ? strstr($this->server('REQUEST_URI'), '?', true) : $this->server('REQUEST_URI'); |
||
633 | if (0 === strpos($pathinfo, $this->server('SCRIPT_NAME'))) { |
||
634 | $pathinfo = substr($pathinfo, strlen($this->server('SCRIPT_NAME'))); |
||
635 | } |
||
636 | } elseif (isset($_SERVER['argv'][1])) { |
||
637 | // CLI模式下 index.php controller/action/params/... |
||
638 | $pathinfo = $_SERVER['argv'][1]; |
||
639 | } |
||
640 | |||
641 | // 分析PATHINFO信息 |
||
642 | if (!isset($pathinfo)) { |
||
643 | foreach ($this->config['pathinfo_fetch'] as $type) { |
||
644 | if ($this->server($type)) { |
||
645 | $pathinfo = (0 === strpos($this->server($type), $this->server('SCRIPT_NAME'))) ? |
||
646 | substr($this->server($type), strlen($this->server('SCRIPT_NAME'))) : $this->server($type); |
||
647 | break; |
||
648 | } |
||
649 | } |
||
650 | } |
||
651 | $this->pathinfo = empty($pathinfo) || '/' == $pathinfo ? '' : ltrim($pathinfo, '/'); |
||
652 | } |
||
653 | |||
654 | return $this->pathinfo; |
||
655 | } |
||
656 | |||
657 | /** |
||
658 | * 获取当前请求URL的pathinfo信息(不含URL后缀) |
||
659 | * @access public |
||
660 | * @return string |
||
661 | */ |
||
662 | public function path(): string |
||
663 | { |
||
664 | $suffix = $this->config['url_html_suffix']; |
||
665 | $pathinfo = $this->pathinfo(); |
||
666 | |||
667 | if (false === $suffix) { |
||
668 | // 禁止伪静态访问 |
||
669 | $path = $pathinfo; |
||
670 | } elseif ($suffix) { |
||
671 | // 去除正常的URL后缀 |
||
672 | $path = preg_replace('/\.(' . ltrim($suffix, '.') . ')$/i', '', $pathinfo); |
||
673 | } else { |
||
674 | // 允许任何后缀访问 |
||
675 | $path = preg_replace('/\.' . $this->ext() . '$/i', '', $pathinfo); |
||
676 | } |
||
677 | |||
678 | return $path; |
||
679 | } |
||
680 | |||
681 | /** |
||
682 | * 当前URL的访问后缀 |
||
683 | * @access public |
||
684 | * @return string |
||
685 | */ |
||
686 | public function ext(): string |
||
687 | { |
||
688 | return pathinfo($this->pathinfo(), PATHINFO_EXTENSION); |
||
689 | } |
||
690 | |||
691 | /** |
||
692 | * 获取当前请求的时间 |
||
693 | * @access public |
||
694 | * @param bool $float 是否使用浮点类型 |
||
695 | * @return integer|float |
||
696 | */ |
||
697 | public function time(bool $float = false) |
||
698 | { |
||
699 | return $float ? $this->server('REQUEST_TIME_FLOAT') : $this->server('REQUEST_TIME'); |
||
700 | } |
||
701 | |||
702 | /** |
||
703 | * 当前请求的资源类型 |
||
704 | * @access public |
||
705 | * @return false|string |
||
706 | */ |
||
707 | public function type() |
||
708 | { |
||
709 | $accept = $this->server('HTTP_ACCEPT'); |
||
710 | |||
711 | if (empty($accept)) { |
||
712 | return false; |
||
713 | } |
||
714 | |||
715 | foreach ($this->mimeType as $key => $val) { |
||
716 | $array = explode(',', $val); |
||
717 | foreach ($array as $k => $v) { |
||
718 | if (stristr($accept, $v)) { |
||
719 | return $key; |
||
720 | } |
||
721 | } |
||
722 | } |
||
723 | |||
724 | return false; |
||
725 | } |
||
726 | |||
727 | /** |
||
728 | * 设置资源类型 |
||
729 | * @access public |
||
730 | * @param string|array $type 资源类型名 |
||
731 | * @param string $val 资源类型 |
||
732 | * @return void |
||
733 | */ |
||
734 | public function mimeType($type, $val = ''): void |
||
735 | { |
||
736 | if (is_array($type)) { |
||
737 | $this->mimeType = array_merge($this->mimeType, $type); |
||
738 | } else { |
||
739 | $this->mimeType[$type] = $val; |
||
740 | } |
||
741 | } |
||
742 | |||
743 | /** |
||
744 | * 设置请求类型 |
||
745 | * @access public |
||
746 | * @param string $method 请求类型 |
||
747 | * @return $this |
||
748 | */ |
||
749 | public function setMethod(string $method) |
||
750 | { |
||
751 | $this->method = strtoupper($method); |
||
752 | return $this; |
||
753 | } |
||
754 | |||
755 | /** |
||
756 | * 当前的请求类型 |
||
757 | * @access public |
||
758 | * @param bool $origin 是否获取原始请求类型 |
||
759 | * @return string |
||
760 | */ |
||
761 | public function method(bool $origin = false): string |
||
762 | { |
||
763 | if ($origin) { |
||
764 | // 获取原始请求类型 |
||
765 | return $this->server('REQUEST_METHOD') ?: 'GET'; |
||
766 | } elseif (!$this->method) { |
||
767 | if (isset($_POST[$this->config['var_method']])) { |
||
768 | $method = strtolower($_POST[$this->config['var_method']]); |
||
769 | if (in_array($method, ['get', 'post', 'put', 'patch', 'delete'])) { |
||
770 | $this->method = strtoupper($method); |
||
771 | $this->{$method} = $_POST; |
||
772 | } else { |
||
773 | $this->method = 'POST'; |
||
774 | } |
||
775 | unset($_POST[$this->config['var_method']]); |
||
776 | } elseif ($this->server('HTTP_X_HTTP_METHOD_OVERRIDE')) { |
||
777 | $this->method = strtoupper($this->server('HTTP_X_HTTP_METHOD_OVERRIDE')); |
||
778 | } else { |
||
779 | $this->method = $this->server('REQUEST_METHOD') ?: 'GET'; |
||
780 | } |
||
781 | } |
||
782 | |||
783 | return $this->method; |
||
784 | } |
||
785 | |||
786 | /** |
||
787 | * 是否为GET请求 |
||
788 | * @access public |
||
789 | * @return bool |
||
790 | */ |
||
791 | public function isGet(): bool |
||
792 | { |
||
793 | return $this->method() == 'GET'; |
||
794 | } |
||
795 | |||
796 | /** |
||
797 | * 是否为POST请求 |
||
798 | * @access public |
||
799 | * @return bool |
||
800 | */ |
||
801 | public function isPost(): bool |
||
802 | { |
||
803 | return $this->method() == 'POST'; |
||
804 | } |
||
805 | |||
806 | /** |
||
807 | * 是否为PUT请求 |
||
808 | * @access public |
||
809 | * @return bool |
||
810 | */ |
||
811 | public function isPut(): bool |
||
812 | { |
||
813 | return $this->method() == 'PUT'; |
||
814 | } |
||
815 | |||
816 | /** |
||
817 | * 是否为DELTE请求 |
||
818 | * @access public |
||
819 | * @return bool |
||
820 | */ |
||
821 | public function isDelete(): bool |
||
822 | { |
||
823 | return $this->method() == 'DELETE'; |
||
824 | } |
||
825 | |||
826 | /** |
||
827 | * 是否为HEAD请求 |
||
828 | * @access public |
||
829 | * @return bool |
||
830 | */ |
||
831 | public function isHead(): bool |
||
832 | { |
||
833 | return $this->method() == 'HEAD'; |
||
834 | } |
||
835 | |||
836 | /** |
||
837 | * 是否为PATCH请求 |
||
838 | * @access public |
||
839 | * @return bool |
||
840 | */ |
||
841 | public function isPatch(): bool |
||
842 | { |
||
843 | return $this->method() == 'PATCH'; |
||
844 | } |
||
845 | |||
846 | /** |
||
847 | * 是否为OPTIONS请求 |
||
848 | * @access public |
||
849 | * @return bool |
||
850 | */ |
||
851 | public function isOptions(): bool |
||
852 | { |
||
853 | return $this->method() == 'OPTIONS'; |
||
854 | } |
||
855 | |||
856 | /** |
||
857 | * 是否为cli |
||
858 | * @access public |
||
859 | * @return bool |
||
860 | */ |
||
861 | public function isCli(): bool |
||
862 | { |
||
863 | return PHP_SAPI == 'cli'; |
||
864 | } |
||
865 | |||
866 | /** |
||
867 | * 是否为cgi |
||
868 | * @access public |
||
869 | * @return bool |
||
870 | */ |
||
871 | public function isCgi(): bool |
||
872 | { |
||
873 | return strpos(PHP_SAPI, 'cgi') === 0; |
||
874 | } |
||
875 | |||
876 | /** |
||
877 | * 获取当前请求的参数 |
||
878 | * @access public |
||
879 | * @param string|array $name 变量名 |
||
880 | * @param mixed $default 默认值 |
||
881 | * @param string|array $filter 过滤方法 |
||
882 | * @return mixed |
||
883 | */ |
||
884 | public function param($name = '', $default = null, $filter = '') |
||
885 | { |
||
886 | if (empty($this->mergeParam)) { |
||
887 | $method = $this->method(true); |
||
888 | |||
889 | // 自动获取请求变量 |
||
890 | switch ($method) { |
||
891 | case 'POST': |
||
1 ignored issue
–
show
|
|||
892 | $vars = $this->post(false); |
||
893 | break; |
||
894 | case 'PUT': |
||
1 ignored issue
–
show
|
|||
895 | case 'DELETE': |
||
1 ignored issue
–
show
|
|||
896 | case 'PATCH': |
||
1 ignored issue
–
show
|
|||
897 | $vars = $this->put(false); |
||
898 | break; |
||
899 | default: |
||
1 ignored issue
–
show
|
|||
900 | $vars = []; |
||
901 | } |
||
902 | |||
903 | // 当前请求参数和URL地址中的参数合并 |
||
904 | $this->param = array_merge($this->param, $this->get(false), $vars, $this->route(false)); |
||
905 | |||
906 | $this->mergeParam = true; |
||
907 | } |
||
908 | |||
909 | if (is_array($name)) { |
||
910 | return $this->only($name, $this->param, $filter); |
||
911 | } |
||
912 | |||
913 | return $this->input($this->param, $name, $default, $filter); |
||
914 | } |
||
915 | |||
916 | /** |
||
917 | * 设置路由变量 |
||
918 | * @access public |
||
919 | * @param array $route 路由变量 |
||
920 | * @return $this |
||
921 | */ |
||
922 | public function setRoute(array $route) |
||
923 | { |
||
924 | $this->route = array_merge($this->route, $route); |
||
925 | return $this; |
||
926 | } |
||
927 | |||
928 | /** |
||
929 | * 获取路由参数 |
||
930 | * @access public |
||
931 | * @param mixed $name 变量名 |
||
932 | * @param mixed $default 默认值 |
||
933 | * @param string|array $filter 过滤方法 |
||
934 | * @return mixed |
||
935 | */ |
||
936 | public function route($name = '', $default = null, $filter = '') |
||
937 | { |
||
938 | if (is_array($name)) { |
||
939 | return $this->only($name, $this->route, $filter); |
||
940 | } |
||
941 | |||
942 | return $this->input($this->route, $name, $default, $filter); |
||
943 | } |
||
944 | |||
945 | /** |
||
946 | * 获取GET参数 |
||
947 | * @access public |
||
948 | * @param mixed $name 变量名 |
||
949 | * @param mixed $default 默认值 |
||
950 | * @param string|array $filter 过滤方法 |
||
951 | * @return mixed |
||
952 | */ |
||
953 | public function get($name = '', $default = null, $filter = '') |
||
954 | { |
||
955 | if (is_array($name)) { |
||
956 | return $this->only($name, $this->get, $filter); |
||
957 | } |
||
958 | |||
959 | return $this->input($this->get, $name, $default, $filter); |
||
960 | } |
||
961 | |||
962 | /** |
||
963 | * 获取中间件传递的参数 |
||
964 | * @access public |
||
965 | * @param mixed $name 变量名 |
||
966 | * @param mixed $default 默认值 |
||
967 | * @return mixed |
||
968 | */ |
||
969 | public function middleware($name, $default = null) |
||
970 | { |
||
971 | return $this->middleware[$name] ?? $default; |
||
972 | } |
||
973 | |||
974 | /** |
||
975 | * 获取POST参数 |
||
976 | * @access public |
||
977 | * @param mixed $name 变量名 |
||
978 | * @param mixed $default 默认值 |
||
979 | * @param string|array $filter 过滤方法 |
||
980 | * @return mixed |
||
981 | */ |
||
982 | public function post($name = '', $default = null, $filter = '') |
||
983 | { |
||
984 | if (is_array($name)) { |
||
985 | return $this->only($name, $this->post, $filter); |
||
986 | } |
||
987 | |||
988 | return $this->input($this->post, $name, $default, $filter); |
||
989 | } |
||
990 | |||
991 | /** |
||
992 | * 获取PUT参数 |
||
993 | * @access public |
||
994 | * @param mixed $name 变量名 |
||
995 | * @param mixed $default 默认值 |
||
996 | * @param string|array $filter 过滤方法 |
||
997 | * @return mixed |
||
998 | */ |
||
999 | public function put($name = '', $default = null, $filter = '') |
||
1000 | { |
||
1001 | if (is_array($name)) { |
||
1002 | return $this->only($name, $this->put, $filter); |
||
1003 | } |
||
1004 | |||
1005 | return $this->input($this->put, $name, $default, $filter); |
||
1006 | } |
||
1007 | |||
1008 | protected function getInputData($content) |
||
1018 | } |
||
1019 | |||
1020 | /** |
||
1021 | * 设置获取DELETE参数 |
||
1022 | * @access public |
||
1023 | * @param mixed $name 变量名 |
||
1024 | * @param mixed $default 默认值 |
||
1025 | * @param string|array $filter 过滤方法 |
||
1026 | * @return mixed |
||
1027 | */ |
||
1028 | public function delete($name = '', $default = null, $filter = '') |
||
1029 | { |
||
1030 | return $this->put($name, $default, $filter); |
||
1031 | } |
||
1032 | |||
1033 | /** |
||
1034 | * 设置获取PATCH参数 |
||
1035 | * @access public |
||
1036 | * @param mixed $name 变量名 |
||
1037 | * @param mixed $default 默认值 |
||
1038 | * @param string|array $filter 过滤方法 |
||
1039 | * @return mixed |
||
1040 | */ |
||
1041 | public function patch($name = '', $default = null, $filter = '') |
||
1042 | { |
||
1043 | return $this->put($name, $default, $filter); |
||
1044 | } |
||
1045 | |||
1046 | /** |
||
1047 | * 获取request变量 |
||
1048 | * @access public |
||
1049 | * @param mixed $name 数据名称 |
||
1050 | * @param mixed $default 默认值 |
||
1051 | * @param string|array $filter 过滤方法 |
||
1052 | * @return mixed |
||
1053 | */ |
||
1054 | public function request($name = '', $default = null, $filter = '') |
||
1055 | { |
||
1056 | if (is_array($name)) { |
||
1057 | return $this->only($name, $this->request, $filter); |
||
1058 | } |
||
1059 | |||
1060 | return $this->input($this->request, $name, $default, $filter); |
||
1061 | } |
||
1062 | |||
1063 | /** |
||
1064 | * 获取环境变量 |
||
1065 | * @access public |
||
1066 | * @param string $name 数据名称 |
||
1067 | * @param string $default 默认值 |
||
1068 | * @return mixed |
||
1069 | */ |
||
1070 | public function env(string $name = '', string $default = null) |
||
1071 | { |
||
1072 | if (empty($name)) { |
||
1073 | return $this->env->get(); |
||
1074 | } else { |
||
1075 | $name = strtoupper($name); |
||
1076 | } |
||
1077 | |||
1078 | return $this->env->get($name, $default); |
||
1079 | } |
||
1080 | |||
1081 | /** |
||
1082 | * 获取session数据 |
||
1083 | * @access public |
||
1084 | * @param string $name 数据名称 |
||
1085 | * @param string $default 默认值 |
||
1086 | * @return mixed |
||
1087 | */ |
||
1088 | public function session(string $name = '', $default = null) |
||
1089 | { |
||
1090 | if ('' === $name) { |
||
1091 | return $this->session->get(); |
||
1092 | } |
||
1093 | |||
1094 | return $this->getData($this->session->get(), $name, $default); |
||
1095 | } |
||
1096 | |||
1097 | /** |
||
1098 | * 获取cookie参数 |
||
1099 | * @access public |
||
1100 | * @param mixed $name 数据名称 |
||
1101 | * @param string $default 默认值 |
||
1102 | * @param string|array $filter 过滤方法 |
||
1103 | * @return mixed |
||
1104 | */ |
||
1105 | public function cookie(string $name = '', $default = null, $filter = '') |
||
1106 | { |
||
1107 | if (!empty($name)) { |
||
1108 | $data = $this->getData($this->cookie, $name, $default); |
||
1109 | } else { |
||
1110 | $data = $this->cookie; |
||
1111 | } |
||
1112 | |||
1113 | // 解析过滤器 |
||
1114 | $filter = $this->getFilter($filter, $default); |
||
1115 | |||
1116 | if (is_array($data)) { |
||
1117 | array_walk_recursive($data, [$this, 'filterValue'], $filter); |
||
1118 | reset($data); |
||
1119 | } else { |
||
1120 | $this->filterValue($data, $name, $filter); |
||
1121 | } |
||
1122 | |||
1123 | return $data; |
||
1124 | } |
||
1125 | |||
1126 | /** |
||
1127 | * 获取server参数 |
||
1128 | * @access public |
||
1129 | * @param string $name 数据名称 |
||
1130 | * @param string $default 默认值 |
||
1131 | * @return mixed |
||
1132 | */ |
||
1133 | 3 | public function server(string $name = '', string $default = null) |
|
1134 | { |
||
1135 | 3 | if (empty($name)) { |
|
1136 | return $this->server; |
||
1137 | } else { |
||
1138 | 3 | $name = strtoupper($name); |
|
1139 | } |
||
1140 | |||
1141 | 3 | return $this->server[$name] ?? $default; |
|
1142 | } |
||
1143 | |||
1144 | /** |
||
1145 | * 获取上传的文件信息 |
||
1146 | * @access public |
||
1147 | * @param string $name 名称 |
||
1148 | * @return null|array|\think\File |
||
1149 | */ |
||
1150 | public function file(string $name = '') |
||
1151 | { |
||
1152 | $files = $this->file; |
||
1153 | if (!empty($files)) { |
||
1154 | |||
1155 | if (strpos($name, '.')) { |
||
1156 | list($name, $sub) = explode('.', $name); |
||
1157 | } |
||
1158 | |||
1159 | // 处理上传文件 |
||
1160 | $array = $this->dealUploadFile($files, $name); |
||
1161 | |||
1162 | if ('' === $name) { |
||
1163 | // 获取全部文件 |
||
1164 | return $array; |
||
1165 | } elseif (isset($sub) && isset($array[$name][$sub])) { |
||
1166 | return $array[$name][$sub]; |
||
1167 | } elseif (isset($array[$name])) { |
||
1168 | return $array[$name]; |
||
1169 | } |
||
1170 | } |
||
1171 | |||
1172 | return; |
||
1173 | } |
||
1174 | |||
1175 | protected function dealUploadFile($files, $name) |
||
1221 | } |
||
1222 | |||
1223 | protected function throwUploadFileError($error) |
||
1224 | { |
||
1225 | static $fileUploadErrors = [ |
||
1226 | 1 => 'upload File size exceeds the maximum value', |
||
1227 | 2 => 'upload File size exceeds the maximum value', |
||
1228 | 3 => 'only the portion of file is uploaded', |
||
1229 | 4 => 'no file to uploaded', |
||
1230 | 6 => 'upload temp dir not found', |
||
1231 | 7 => 'file write error', |
||
1236 | } |
||
1237 | |||
1238 | /** |
||
1239 | * 设置或者获取当前的Header |
||
1240 | * @access public |
||
1241 | * @param string $name header名称 |
||
1242 | * @param string $default 默认值 |
||
1243 | * @return string|array |
||
1244 | */ |
||
1245 | public function header(string $name = '', string $default = null) |
||
1246 | { |
||
1247 | if ('' === $name) { |
||
1248 | return $this->header; |
||
1249 | } |
||
1250 | |||
1251 | $name = str_replace('_', '-', strtolower($name)); |
||
1252 | |||
1253 | return $this->header[$name] ?? $default; |
||
1254 | } |
||
1255 | |||
1256 | /** |
||
1257 | * 获取变量 支持过滤和默认值 |
||
1258 | * @access public |
||
1259 | * @param array $data 数据源 |
||
1260 | * @param string|false $name 字段名 |
||
1261 | * @param mixed $default 默认值 |
||
1262 | * @param string|array $filter 过滤函数 |
||
1263 | * @return mixed |
||
1264 | */ |
||
1265 | public function input(array $data = [], $name = '', $default = null, $filter = '') |
||
1266 | { |
||
1267 | if (false === $name) { |
||
1268 | // 获取原始数据 |
||
1269 | return $data; |
||
1270 | } |
||
1271 | |||
1272 | $name = (string) $name; |
||
1273 | if ('' != $name) { |
||
1274 | // 解析name |
||
1275 | if (strpos($name, '/')) { |
||
1276 | list($name, $type) = explode('/', $name); |
||
1277 | } |
||
1278 | |||
1279 | $data = $this->getData($data, $name); |
||
1280 | |||
1281 | if (is_null($data)) { |
||
1282 | return $default; |
||
1283 | } |
||
1284 | |||
1285 | if (is_object($data)) { |
||
1286 | return $data; |
||
1287 | } |
||
1288 | } |
||
1289 | |||
1290 | $data = $this->filterData($data, $filter, $name, $default); |
||
1291 | |||
1292 | if (isset($type) && $data !== $default) { |
||
1293 | // 强制类型转换 |
||
1294 | $this->typeCast($data, $type); |
||
1295 | } |
||
1296 | |||
1297 | return $data; |
||
1298 | } |
||
1299 | |||
1300 | protected function filterData($data, $filter, $name, $default) |
||
1301 | { |
||
1302 | // 解析过滤器 |
||
1303 | $filter = $this->getFilter($filter, $default); |
||
1304 | |||
1305 | if (is_array($data)) { |
||
1306 | array_walk_recursive($data, [$this, 'filterValue'], $filter); |
||
1307 | reset($data); |
||
1308 | } else { |
||
1309 | $this->filterValue($data, $name, $filter); |
||
1310 | } |
||
1311 | |||
1312 | return $data; |
||
1313 | } |
||
1314 | |||
1315 | /** |
||
1316 | * 强制类型转换 |
||
1317 | * @access public |
||
1318 | * @param string $data |
||
1319 | * @param string $type |
||
1320 | * @return mixed |
||
1321 | */ |
||
1322 | private function typeCast(&$data, string $type) |
||
1323 | { |
||
1324 | switch (strtolower($type)) { |
||
1325 | // 数组 |
||
1326 | case 'a': |
||
1 ignored issue
–
show
|
|||
1327 | $data = (array) $data; |
||
1328 | break; |
||
1329 | // 数字 |
||
1330 | case 'd': |
||
1 ignored issue
–
show
|
|||
1331 | $data = (int) $data; |
||
1332 | break; |
||
1333 | // 浮点 |
||
1334 | case 'f': |
||
1 ignored issue
–
show
|
|||
1335 | $data = (float) $data; |
||
1336 | break; |
||
1337 | // 布尔 |
||
1338 | case 'b': |
||
1 ignored issue
–
show
|
|||
1339 | $data = (boolean) $data; |
||
1340 | break; |
||
1341 | // 字符串 |
||
1342 | case 's': |
||
1 ignored issue
–
show
|
|||
1343 | if (is_scalar($data)) { |
||
1 ignored issue
–
show
|
|||
1344 | $data = (string) $data; |
||
1345 | } else { |
||
1 ignored issue
–
show
|
|||
1346 | throw new \InvalidArgumentException('variable type error:' . gettype($data)); |
||
1347 | } |
||
1 ignored issue
–
show
|
|||
1348 | break; |
||
1349 | } |
||
1350 | } |
||
1351 | |||
1352 | /** |
||
1353 | * 获取数据 |
||
1354 | * @access public |
||
1355 | * @param array $data 数据源 |
||
1356 | * @param string $name 字段名 |
||
1357 | * @param mixed $default 默认值 |
||
1358 | * @return mixed |
||
1359 | */ |
||
1360 | protected function getData(array $data, string $name, $default = null) |
||
1361 | { |
||
1362 | foreach (explode('.', $name) as $val) { |
||
1363 | if (isset($data[$val])) { |
||
1364 | $data = $data[$val]; |
||
1365 | } else { |
||
1366 | return $default; |
||
1367 | } |
||
1368 | } |
||
1369 | |||
1370 | return $data; |
||
1371 | } |
||
1372 | |||
1373 | /** |
||
1374 | * 设置或获取当前的过滤规则 |
||
1375 | * @access public |
||
1376 | * @param mixed $filter 过滤规则 |
||
1377 | * @return mixed |
||
1378 | */ |
||
1379 | public function filter($filter = null) |
||
1380 | { |
||
1381 | if (is_null($filter)) { |
||
1382 | return $this->filter; |
||
1383 | } |
||
1384 | |||
1385 | $this->filter = $filter; |
||
1386 | |||
1387 | return $this; |
||
1388 | } |
||
1389 | |||
1390 | protected function getFilter($filter, $default) |
||
1391 | { |
||
1392 | if (is_null($filter)) { |
||
1393 | $filter = []; |
||
1394 | } else { |
||
1395 | $filter = $filter ?: $this->filter; |
||
1396 | if (is_string($filter) && false === strpos($filter, '/')) { |
||
1397 | $filter = explode(',', $filter); |
||
1398 | } else { |
||
1399 | $filter = (array) $filter; |
||
1400 | } |
||
1401 | } |
||
1402 | |||
1403 | $filter[] = $default; |
||
1404 | |||
1405 | return $filter; |
||
1406 | } |
||
1407 | |||
1408 | /** |
||
1409 | * 递归过滤给定的值 |
||
1410 | * @access public |
||
1411 | * @param mixed $value 键值 |
||
1412 | * @param mixed $key 键名 |
||
1413 | * @param array $filters 过滤方法+默认值 |
||
1414 | * @return mixed |
||
1415 | */ |
||
1416 | private function filterValue(&$value, $key, $filters) |
||
1417 | { |
||
1418 | $default = array_pop($filters); |
||
1419 | |||
1420 | foreach ($filters as $filter) { |
||
1421 | if (is_callable($filter)) { |
||
1422 | // 调用函数或者方法过滤 |
||
1423 | $value = call_user_func($filter, $value); |
||
1424 | } elseif (is_scalar($value)) { |
||
1425 | if (false !== strpos($filter, '/')) { |
||
1426 | // 正则过滤 |
||
1427 | if (!preg_match($filter, $value)) { |
||
1428 | // 匹配不成功返回默认值 |
||
1429 | $value = $default; |
||
1430 | break; |
||
1431 | } |
||
1432 | } elseif (!empty($filter)) { |
||
1433 | // filter函数不存在时, 则使用filter_var进行过滤 |
||
1434 | // filter为非整形值时, 调用filter_id取得过滤id |
||
1435 | $value = filter_var($value, is_int($filter) ? $filter : filter_id($filter)); |
||
1436 | if (false === $value) { |
||
1437 | $value = $default; |
||
1438 | break; |
||
1439 | } |
||
1440 | } |
||
1441 | } |
||
1442 | } |
||
1443 | |||
1444 | return $value; |
||
1445 | } |
||
1446 | |||
1447 | /** |
||
1448 | * 是否存在某个请求参数 |
||
1449 | * @access public |
||
1450 | * @param string $name 变量名 |
||
1451 | * @param string $type 变量类型 |
||
1452 | * @param bool $checkEmpty 是否检测空值 |
||
1453 | * @return bool |
||
1454 | */ |
||
1455 | public function has(string $name, string $type = 'param', bool $checkEmpty = false): bool |
||
1456 | { |
||
1457 | $param = empty($this->$type) ? $this->$type() : $this->$type; |
||
1458 | |||
1459 | // 按.拆分成多维数组进行判断 |
||
1460 | foreach (explode('.', $name) as $val) { |
||
1461 | if (isset($param[$val])) { |
||
1462 | $param = $param[$val]; |
||
1463 | } else { |
||
1464 | return false; |
||
1465 | } |
||
1466 | } |
||
1467 | |||
1468 | return ($checkEmpty && '' === $param) ? false : true; |
||
1469 | } |
||
1470 | |||
1471 | /** |
||
1472 | * 获取指定的参数 |
||
1473 | * @access public |
||
1474 | * @param array $name 变量名 |
||
1475 | * @param mixed $data 数据或者变量类型 |
||
1476 | * @param string|array $filter 过滤方法 |
||
1477 | * @return array |
||
1478 | */ |
||
1479 | public function only(array $name, $data = 'param', $filter = ''): array |
||
1480 | { |
||
1481 | $data = is_array($data) ? $data : $this->$data(); |
||
1482 | |||
1483 | $item = []; |
||
1484 | foreach ($name as $key => $val) { |
||
1485 | |||
1486 | if (is_int($key)) { |
||
1487 | $default = null; |
||
1488 | $key = $val; |
||
1489 | if (!isset($data[$key])) { |
||
1490 | continue; |
||
1491 | } |
||
1492 | } else { |
||
1493 | $default = $val; |
||
1494 | } |
||
1495 | |||
1496 | $item[$key] = $this->filterData($data[$key] ?? $default, $filter, $key, $default); |
||
1497 | } |
||
1498 | |||
1499 | return $item; |
||
1500 | } |
||
1501 | |||
1502 | /** |
||
1503 | * 排除指定参数获取 |
||
1504 | * @access public |
||
1505 | * @param array $name 变量名 |
||
1506 | * @param string $type 变量类型 |
||
1507 | * @return mixed |
||
1508 | */ |
||
1509 | public function except(array $name, string $type = 'param'): array |
||
1510 | { |
||
1511 | $param = $this->$type(); |
||
1512 | |||
1513 | foreach ($name as $key) { |
||
1514 | if (isset($param[$key])) { |
||
1515 | unset($param[$key]); |
||
1516 | } |
||
1517 | } |
||
1518 | |||
1519 | return $param; |
||
1520 | } |
||
1521 | |||
1522 | /** |
||
1523 | * 当前是否ssl |
||
1524 | * @access public |
||
1525 | * @return bool |
||
1526 | */ |
||
1527 | public function isSsl(): bool |
||
1528 | { |
||
1529 | if ($this->server('HTTPS') && ('1' == $this->server('HTTPS') || 'on' == strtolower($this->server('HTTPS')))) { |
||
1530 | return true; |
||
1531 | } elseif ('https' == $this->server('REQUEST_SCHEME')) { |
||
1532 | return true; |
||
1533 | } elseif ('443' == $this->server('SERVER_PORT')) { |
||
1534 | return true; |
||
1535 | } elseif ('https' == $this->server('HTTP_X_FORWARDED_PROTO')) { |
||
1536 | return true; |
||
1537 | } elseif ($this->config['https_agent_name'] && $this->server($this->config['https_agent_name'])) { |
||
1538 | return true; |
||
1539 | } |
||
1540 | |||
1541 | return false; |
||
1542 | } |
||
1543 | |||
1544 | /** |
||
1545 | * 当前是否JSON请求 |
||
1546 | * @access public |
||
1547 | * @return bool |
||
1548 | */ |
||
1549 | public function isJson(): bool |
||
1550 | { |
||
1551 | $contentType = $this->contentType(); |
||
1552 | |||
1553 | return false !== strpos($contentType, '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->config['var_ajax']) ? 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->config['var_pjax']) ? 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->config('proxy_server_ip'); |
||
1607 | $proxyIpHeader = $this->config('proxy_server_ip_header'); |
||
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': |
||
1 ignored issue
–
show
|
|||
1673 | $flag = FILTER_FLAG_IPV4; |
||
1674 | break; |
||
1675 | case 'ipv6': |
||
1 ignored issue
–
show
|
|||
1676 | $flag = FILTER_FLAG_IPV6; |
||
1677 | break; |
||
1678 | default: |
||
1 ignored issue
–
show
|
|||
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 |
||
1 ignored issue
–
show
|
|||
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 |
||
1798 | { |
||
1799 | return $this->server('SERVER_PROTOCOL', ''); |
||
1800 | } |
||
1801 | |||
1802 | /** |
||
1803 | * 当前请求 REMOTE_PORT |
||
1804 | * @access public |
||
1805 | * @return string |
||
1806 | */ |
||
1807 | public function remotePort(): string |
||
1808 | { |
||
1809 | return $this->server('REMOTE_PORT', ''); |
||
1810 | } |
||
1811 | |||
1812 | /** |
||
1813 | * 当前请求 HTTP_CONTENT_TYPE |
||
1814 | * @access public |
||
1815 | * @return string |
||
1816 | */ |
||
1817 | public function contentType(): string |
||
1818 | { |
||
1819 | $contentType = $this->server('CONTENT_TYPE'); |
||
1820 | |||
1821 | if ($contentType) { |
||
1822 | if (strpos($contentType, ';')) { |
||
1823 | list($type) = explode(';', $contentType); |
||
1824 | } else { |
||
1825 | $type = $contentType; |
||
1826 | } |
||
1827 | return trim($type); |
||
1828 | } |
||
1829 | |||
1830 | return ''; |
||
1831 | } |
||
1832 | |||
1833 | /** |
||
1834 | * 设置或者获取当前请求的调度信息 |
||
1835 | * @access public |
||
1836 | * @param Dispatch $dispatch 调度信息 |
||
1837 | * @return Dispatch |
||
1838 | */ |
||
1839 | public function dispatch(Dispatch $dispatch = null) |
||
1840 | { |
||
1841 | if (!is_null($dispatch)) { |
||
1842 | $this->dispatch = $dispatch; |
||
1843 | } |
||
1844 | |||
1845 | return $this->dispatch; |
||
1846 | } |
||
1847 | |||
1848 | /** |
||
1849 | * 获取当前请求的安全Key |
||
1850 | * @access public |
||
1851 | * @return string |
||
1852 | */ |
||
1853 | public function secureKey(): string |
||
1854 | { |
||
1855 | if (is_null($this->secureKey)) { |
||
1856 | $this->secureKey = uniqid('', true); |
||
1857 | } |
||
1858 | |||
1859 | return $this->secureKey; |
||
1860 | } |
||
1861 | |||
1862 | /** |
||
1863 | * 设置当前的应用名 |
||
1864 | * @access public |
||
1865 | * @param string $app 应用名 |
||
1866 | * @return $this |
||
1867 | */ |
||
1868 | 5 | public function setApp(string $app) |
|
1869 | { |
||
1870 | 5 | $this->app = $app; |
|
1871 | 5 | return $this; |
|
1872 | } |
||
1873 | |||
1874 | /** |
||
1875 | * 设置当前的控制器名 |
||
1876 | * @access public |
||
1877 | * @param string $controller 控制器名 |
||
1878 | * @return $this |
||
1879 | */ |
||
1880 | public function setController(string $controller) |
||
1881 | { |
||
1882 | $this->controller = $controller; |
||
1883 | return $this; |
||
1884 | } |
||
1885 | |||
1886 | /** |
||
1887 | * 设置当前的操作名 |
||
1888 | * @access public |
||
1889 | * @param string $action 操作名 |
||
1890 | * @return $this |
||
1891 | */ |
||
1892 | public function setAction(string $action) |
||
1893 | { |
||
1894 | $this->action = $action; |
||
1895 | return $this; |
||
1896 | } |
||
1897 | |||
1898 | /** |
||
1899 | * 获取当前的应用名 |
||
1900 | * @access public |
||
1901 | * @return string |
||
1902 | */ |
||
1903 | public function app(): string |
||
1904 | { |
||
1905 | return $this->app ?: ''; |
||
1906 | } |
||
1907 | |||
1908 | /** |
||
1909 | * 获取当前的控制器名 |
||
1910 | * @access public |
||
1911 | * @param bool $convert 转换为小写 |
||
1912 | * @return string |
||
1913 | */ |
||
1914 | public function controller(bool $convert = false): string |
||
1915 | { |
||
1916 | $name = $this->controller ?: ''; |
||
1917 | return $convert ? strtolower($name) : $name; |
||
1918 | } |
||
1919 | |||
1920 | /** |
||
1921 | * 获取当前的操作名 |
||
1922 | * @access public |
||
1923 | * @param bool $convert 转换为小写 |
||
1924 | * @return string |
||
1925 | */ |
||
1926 | public function action(bool $convert = false): string |
||
1927 | { |
||
1928 | $name = $this->action ?: ''; |
||
1929 | return $convert ? strtolower($name) : $name; |
||
1930 | } |
||
1931 | |||
1932 | /** |
||
1933 | * 设置或者获取当前请求的content |
||
1934 | * @access public |
||
1935 | * @return string |
||
1936 | */ |
||
1937 | public function getContent(): string |
||
1944 | } |
||
1945 | |||
1946 | /** |
||
1947 | * 获取当前请求的php://input |
||
1948 | * @access public |
||
1949 | * @return string |
||
1950 | */ |
||
1951 | public function getInput(): string |
||
1952 | { |
||
1953 | return $this->input; |
||
1954 | } |
||
1955 | |||
1956 | /** |
||
1957 | * 生成请求令牌 |
||
1958 | * @access public |
||
1959 | * @param string $name 令牌名称 |
||
1960 | * @param mixed $type 令牌生成方法 |
||
1961 | * @return string |
||
1962 | */ |
||
1963 | public function token(string $name = '__token__', $type = 'md5'): string |
||
1964 | { |
||
1965 | $type = is_callable($type) ? $type : 'md5'; |
||
1966 | $token = call_user_func($type, $this->server('REQUEST_TIME_FLOAT')); |
||
1967 | |||
1968 | if ($this->isAjax()) { |
||
1969 | header($name . ': ' . $token); |
||
1970 | } |
||
1971 | |||
1972 | $this->session->set($name, $token); |
||
1973 | |||
1974 | return $token; |
||
1975 | } |
||
1976 | |||
1977 | /** |
||
1978 | * 设置当前地址的请求缓存 |
||
1979 | * @access public |
||
1980 | * @param mixed $key 缓存标识,支持变量规则 ,例如 item/:name/:id |
||
1981 | * @param int $expire 缓存有效期 |
||
1982 | * @param string $tag 缓存标签 |
||
1983 | * @param array $except 缓存排除 |
||
1984 | * @return mixed |
||
1985 | */ |
||
1986 | public function cache($key = null, int $expire = null, string $tag = null, array $except = []) |
||
1987 | { |
||
1988 | $key = $key ?: $this->config['request_cache']; |
||
1989 | $expire = $expire ?: $this->config['request_cache_expire']; |
||
1990 | $except = !empty($except) ? $except : $this->config['request_cache_except']; |
||
1991 | $tag = $tag ?: $this->config['request_cache_tag']; |
||
1992 | |||
1993 | if ($this->isCheckCache) { |
||
1994 | // 关闭当前缓存 |
||
1995 | return; |
||
1996 | } |
||
1997 | |||
1998 | // 标记请求缓存检查 |
||
1999 | $this->isCheckCache = true; |
||
2000 | |||
2001 | foreach ($except as $rule) { |
||
2002 | if (0 === stripos($this->url(), $rule)) { |
||
2003 | return; |
||
2004 | } |
||
2005 | } |
||
2006 | |||
2007 | if ($key instanceof \Closure) { |
||
2008 | $key = call_user_func_array($key, [$this]); |
||
2009 | } elseif (true === $key) { |
||
2010 | // 自动缓存功能 |
||
2011 | $key = '__URL__'; |
||
2012 | } elseif (strpos($key, '|')) { |
||
2013 | list($key, $fun) = explode('|', $key); |
||
2014 | } |
||
2015 | |||
2016 | // 特殊规则替换 |
||
2017 | if (false !== strpos($key, '__')) { |
||
2018 | $key = str_replace(['__APP__', '__CONTROLLER__', '__ACTION__', '__URL__'], [$this->app, $this->controller, $this->action, md5($this->url(true))], $key); |
||
2019 | } |
||
2020 | |||
2021 | if (false !== strpos($key, ':')) { |
||
2022 | $param = $this->param(); |
||
2023 | foreach ($param as $item => $val) { |
||
2024 | if (is_string($val) && false !== strpos($key, ':' . $item)) { |
||
2025 | $key = str_replace(':' . $item, $val, $key); |
||
2026 | } |
||
2027 | } |
||
2028 | } elseif (strpos($key, ']')) { |
||
2029 | if ('[' . $this->ext() . ']' == $key) { |
||
2030 | // 缓存某个后缀的请求 |
||
2031 | $key = md5($this->url()); |
||
2032 | } else { |
||
2033 | return; |
||
2034 | } |
||
2035 | } |
||
2036 | |||
2037 | if (isset($fun)) { |
||
2038 | $key = $fun($key); |
||
2039 | } |
||
2040 | |||
2041 | $this->cache = [$key, $expire, $tag]; |
||
2042 | return $this->cache; |
||
2043 | } |
||
2044 | |||
2045 | /** |
||
2046 | * 读取请求缓存设置 |
||
2047 | * @access public |
||
2048 | * @return array|null |
||
2049 | */ |
||
2050 | public function getCache() |
||
2051 | { |
||
2052 | return $this->cache; |
||
2053 | } |
||
2054 | |||
2055 | /** |
||
2056 | * 设置在中间件传递的数据 |
||
2057 | * @access public |
||
2058 | * @param array $middleware 数据 |
||
2059 | * @return $this |
||
2060 | */ |
||
2061 | public function withMiddleware(array $middleware) |
||
2062 | { |
||
2063 | $this->middleware = array_merge($this->middleware, $middleware); |
||
2064 | return $this; |
||
2065 | } |
||
2066 | |||
2067 | /** |
||
2068 | * 设置GET数据 |
||
2069 | * @access public |
||
2070 | * @param array $get 数据 |
||
2071 | * @return $this |
||
2072 | */ |
||
2073 | public function withGet(array $get) |
||
2074 | { |
||
2075 | $this->get = $get; |
||
2076 | return $this; |
||
2077 | } |
||
2078 | |||
2079 | /** |
||
2080 | * 设置POST数据 |
||
2081 | * @access public |
||
2082 | * @param array $post 数据 |
||
2083 | * @return $this |
||
2084 | */ |
||
2085 | public function withPost(array $post) |
||
2089 | } |
||
2090 | |||
2091 | /** |
||
2092 | * 设置COOKIE数据 |
||
2093 | * @access public |
||
2094 | * @param array $cookie 数据 |
||
1 ignored issue
–
show
|
|||
2095 | * @return $this |
||
2096 | */ |
||
2097 | public function withCookie(array $cookie) |
||
2098 | { |
||
2099 | $this->cookie = $cookie; |
||
2100 | return $this; |
||
2101 | } |
||
2102 | |||
2103 | /** |
||
2104 | * 设置SESSION数据 |
||
2105 | * @access public |
||
2106 | * @param Session $session 数据 |
||
1 ignored issue
–
show
|
|||
2107 | * @return $this |
||
2108 | */ |
||
2109 | public function withSession(Session $session) |
||
2110 | { |
||
2111 | $this->session = $session; |
||
2112 | return $this; |
||
2113 | } |
||
2114 | |||
2115 | /** |
||
2116 | * 设置SERVER数据 |
||
2117 | * @access public |
||
2118 | * @param array $server 数据 |
||
2119 | * @return $this |
||
2120 | */ |
||
2121 | public function withServer(array $server) |
||
2122 | { |
||
2123 | $this->server = array_change_key_case($server, CASE_UPPER); |
||
2124 | return $this; |
||
2125 | } |
||
2126 | |||
2127 | /** |
||
2128 | * 设置HEADER数据 |
||
2129 | * @access public |
||
2130 | * @param array $header 数据 |
||
2131 | * @return $this |
||
2132 | */ |
||
2133 | public function withHeader(array $header) |
||
2134 | { |
||
2135 | $this->header = array_change_key_case($header); |
||
2136 | return $this; |
||
2137 | } |
||
2138 | |||
2139 | /** |
||
2140 | * 设置ENV数据 |
||
2141 | * @access public |
||
2142 | * @param Env $env 数据 |
||
1 ignored issue
–
show
|
|||
2143 | * @return $this |
||
2144 | */ |
||
2145 | public function withEnv(Env $env) |
||
2146 | { |
||
2147 | $this->env = $env; |
||
2148 | return $this; |
||
2149 | } |
||
2150 | |||
2151 | /** |
||
2152 | * 设置php://input数据 |
||
2153 | * @access public |
||
2154 | * @param string $input RAW数据 |
||
2155 | * @return $this |
||
2156 | */ |
||
2157 | public function withInput(string $input) |
||
2158 | { |
||
2159 | $this->input = $input; |
||
2160 | return $this; |
||
2161 | } |
||
2162 | |||
2163 | /** |
||
2164 | * 设置文件上传数据 |
||
2165 | * @access public |
||
2166 | * @param array $files 上传信息 |
||
2167 | * @return $this |
||
2168 | */ |
||
2169 | public function withFiles(array $files) |
||
2170 | { |
||
2171 | $this->file = $files; |
||
2172 | return $this; |
||
2173 | } |
||
2174 | |||
2175 | /** |
||
2176 | * 设置ROUTE变量 |
||
2177 | * @access public |
||
2178 | * @param array $route 数据 |
||
2179 | * @return $this |
||
2180 | */ |
||
2181 | public function withRoute(array $route) |
||
2182 | { |
||
2183 | $this->route = $route; |
||
2184 | return $this; |
||
2185 | } |
||
2186 | |||
2187 | /** |
||
2188 | * 设置中间传递数据 |
||
2189 | * @access public |
||
2190 | * @param string $name 参数名 |
||
2191 | * @param mixed $value 值 |
||
2192 | */ |
||
2193 | public function __set(string $name, $value) |
||
2196 | } |
||
2197 | |||
2198 | /** |
||
2199 | * 获取中间传递数据的值 |
||
2200 | * @access public |
||
2201 | * @param string $name 名称 |
||
2202 | * @return mixed |
||
2203 | */ |
||
2204 | public function __get(string $name) |
||
2205 | { |
||
2206 | return $this->middleware($name); |
||
2207 | } |
||
2208 | |||
2209 | /** |
||
2210 | * 检测请求数据的值 |
||
2211 | * @access public |
||
2212 | * @param string $name 名称 |
||
2213 | * @return boolean |
||
2214 | */ |
||
2215 | public function __isset(string $name): bool |
||
2216 | { |
||
2217 | return isset($this->param[$name]); |
||
2218 | } |
||
2219 | } |
||
2220 |