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