Completed
Push — 6.0 ( 122edf...c283ff )
by liu
03:01
created

Request::cache()   F

Complexity

Conditions 19
Paths 228

Size

Total Lines 57
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 380

Importance

Changes 0
Metric Value
cc 19
eloc 32
nc 228
nop 4
dl 0
loc 57
ccs 0
cts 32
cp 0
crap 380
rs 3.3333
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think;
14
15
use think\route\Dispatch;
16
17
/**
18
 * 请求管理类
19
 */
5 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
115
     * @var string
116
     */
117
    protected $pathinfo;
118
119
    /**
120
     * pathinfo(不含后缀)
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
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内容
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function init()
Loading history...
309
    {
310
        $this->config = array_merge($this->config, $options);
311
312
        if (is_null($this->filter) && !empty($this->config['default_filter'])) {
0 ignored issues
show
introduced by
The condition is_null($this->filter) is always false.
Loading history...
313
            $this->filter = $this->config['default_filter'];
314
        }
315
    }
316
317
    public function config($name = null)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function config()
Loading history...
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
Coding Style introduced by
Missing doc comment for function __make()
Loading history...
Coding Style introduced by
Method name "Request::__make" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
Coding Style introduced by
Public method name "Request::__make" must not be prefixed with an underscore
Loading history...
327
    {
328
        $request = new static($config->get('route'));
0 ignored issues
show
Bug introduced by
It seems like $config->get('route') can also be of type null; however, parameter $options of think\Request::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

328
        $request = new static(/** @scrutinizer ignore-type */ $config->get('route'));
Loading history...
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)) {
0 ignored issues
show
introduced by
The condition is_null($this->subDomain) is always false.
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
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)) {
0 ignored issues
show
introduced by
The condition is_null($this->pathinfo) is always false.
Loading history...
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 string
706
     */
707
    public function type(): string
708
    {
709
        $accept = $this->server('HTTP_ACCEPT');
710
711
        if (empty($accept)) {
712
            return '';
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 '';
725
    }
726
727
    /**
728
     * 设置资源类型
729
     * @access public
730
     * @param  string|array $type 资源类型名
731
     * @param  string       $val 资源类型
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
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 变量名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
880
     * @param  mixed        $default 默认值
881
     * @param  string|array $filter 过滤方法
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
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
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
892
                    $vars = $this->post(false);
893
                    break;
894
                case 'PUT':
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
895
                case 'DELETE':
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
896
                case 'PATCH':
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
897
                    $vars = $this->put(false);
898
                    break;
899
                default:
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
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 变量名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
932
     * @param  mixed        $default 默认值
933
     * @param  string|array $filter 过滤方法
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
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 变量名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
949
     * @param  mixed        $default 默认值
950
     * @param  string|array $filter 过滤方法
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
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 变量名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
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 变量名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
978
     * @param  mixed        $default 默认值
979
     * @param  string|array $filter 过滤方法
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
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 变量名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
995
     * @param  mixed        $default 默认值
996
     * @param  string|array $filter 过滤方法
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
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)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getInputData()
Loading history...
1009
    {
1010
        if ($this->isJson()) {
1011
            return (array) json_decode($content, true);
1012
        } elseif (strpos($content, '=')) {
1013
            parse_str($content, $data);
1014
            return $data;
1015
        }
1016
1017
        return [];
1018
    }
1019
1020
    /**
1021
     * 设置获取DELETE参数
1022
     * @access public
1023
     * @param  mixed        $name 变量名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1024
     * @param  mixed        $default 默认值
1025
     * @param  string|array $filter 过滤方法
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
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 变量名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1037
     * @param  mixed        $default 默认值
1038
     * @param  string|array $filter 过滤方法
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
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 数据名称
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1050
     * @param  mixed        $default 默认值
1051
     * @param  string|array $filter 过滤方法
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
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 数据名称
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
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 数据名称
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
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 数据名称
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1101
     * @param  string       $default 默认值
1102
     * @param  string|array $filter 过滤方法
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
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 数据名称
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
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)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function dealUploadFile()
Loading history...
1176
    {
1177
        $array = [];
1178
        foreach ($files as $key => $file) {
1179
            if (is_array($file['name'])) {
1180
                $item  = [];
1181
                $keys  = array_keys($file);
1182
                $count = count($file['name']);
1183
1184
                for ($i = 0; $i < $count; $i++) {
1185
                    if ($file['error'][$i] > 0) {
1186
                        if ($name == $key) {
1187
                            $this->throwUploadFileError($file['error'][$i]);
1188
                        } else {
1189
                            continue;
1190
                        }
1191
                    }
1192
1193
                    $temp['key'] = $key;
1194
1195
                    foreach ($keys as $_key) {
1196
                        $temp[$_key] = $file[$_key][$i];
1197
                    }
1198
1199
                    $item[] = (new File($temp['tmp_name']))->setUploadInfo($temp);
1200
                }
1201
1202
                $array[$key] = $item;
1203
            } else {
1204
                if ($file instanceof File) {
1205
                    $array[$key] = $file;
1206
                } else {
1207
                    if ($file['error'] > 0) {
1208
                        if ($key == $name) {
1209
                            $this->throwUploadFileError($file['error']);
1210
                        } else {
1211
                            continue;
1212
                        }
1213
                    }
1214
1215
                    $array[$key] = (new File($file['tmp_name']))->setUploadInfo($file);
1216
                }
1217
            }
1218
        }
1219
1220
        return $array;
1221
    }
1222
1223
    protected function throwUploadFileError($error)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function throwUploadFileError()
Loading history...
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',
1232
        ];
1233
1234
        $msg = $fileUploadErrors[$error];
1235
        throw new Exception($msg);
1236
    }
1237
1238
    /**
1239
     * 设置或者获取当前的Header
1240
     * @access public
1241
     * @param  string $name header名称
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
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 数据源
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1260
     * @param  string|false $name 字段名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1261
     * @param  mixed        $default 默认值
1262
     * @param  string|array $filter 过滤函数
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
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)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function filterData()
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
1319
     * @param  string $type
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
1320
     * @return mixed
1321
     */
1322
    private function typeCast(&$data, string $type)
0 ignored issues
show
Coding Style introduced by
Private method name "Request::typeCast" must be prefixed with an underscore
Loading history...
1323
    {
1324
        switch (strtolower($type)) {
1325
            // 数组
1326
            case 'a':
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
1327
                $data = (array) $data;
1328
                break;
1329
            // 数字
1330
            case 'd':
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
1331
                $data = (int) $data;
1332
                break;
1333
            // 浮点
1334
            case 'f':
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
1335
                $data = (float) $data;
1336
                break;
1337
            // 布尔
1338
            case 'b':
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
1339
                $data = (boolean) $data;
1340
                break;
1341
            // 字符串
1342
            case 's':
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
1343
                if (is_scalar($data)) {
1 ignored issue
show
introduced by
The condition is_scalar($data) is always true.
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
1344
                    $data = (string) $data;
1345
                } else {
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
1346
                    throw new \InvalidArgumentException('variable type error:' . gettype($data));
1347
                }
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
1348
                break;
1349
        }
1350
    }
1351
1352
    /**
1353
     * 获取数据
1354
     * @access public
1355
     * @param  array  $data 数据源
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1356
     * @param  string $name 字段名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
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)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getFilter()
Loading history...
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 键值
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
1412
     * @param  mixed $key 键名
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
1413
     * @param  array $filters 过滤方法+默认值
1414
     * @return mixed
1415
     */
1416
    private function filterValue(&$value, $key, $filters)
0 ignored issues
show
Coding Style introduced by
Private method name "Request::filterValue" must be prefixed with an underscore
Loading history...
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 变量名
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter name; 1 found
Loading history...
1451
     * @param  string $type 变量类型
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter name; 1 found
Loading history...
1452
     * @param  bool   $checkEmpty 是否检测空值
1453
     * @return bool
1454
     */
1455
    public function has(string $name, string $type = 'param', bool $checkEmpty = false): bool
1456
    {
1457
        if (!in_array($type, ['param', 'get', 'post', 'put', 'patch', 'route', 'delete', 'cookie', 'session', 'env', 'request', 'server', 'header', 'file'])) {
1458
            return false;
1459
        }
1460
1461
        $param = empty($this->$type) ? $this->$type() : $this->$type;
1462
1463
        if (is_object($param)) {
1464
            return $param->has($name);
1465
        }
1466
1467
        // 按.拆分成多维数组进行判断
1468
        foreach (explode('.', $name) as $val) {
1469
            if (isset($param[$val])) {
1470
                $param = $param[$val];
1471
            } else {
1472
                return false;
1473
            }
1474
        }
1475
1476
        return ($checkEmpty && '' === $param) ? false : true;
1477
    }
1478
1479
    /**
1480
     * 获取指定的参数
1481
     * @access public
1482
     * @param  array        $name 变量名
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
1483
     * @param  mixed        $data 数据或者变量类型
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
1484
     * @param  string|array $filter 过滤方法
1485
     * @return array
1486
     */
1487
    public function only(array $name, $data = 'param', $filter = ''): array
1488
    {
1489
        $data = is_array($data) ? $data : $this->$data();
1490
1491
        $item = [];
1492
        foreach ($name as $key => $val) {
1493
1494
            if (is_int($key)) {
1495
                $default = null;
1496
                $key     = $val;
1497
                if (!isset($data[$key])) {
1498
                    continue;
1499
                }
1500
            } else {
1501
                $default = $val;
1502
            }
1503
1504
            $item[$key] = $this->filterData($data[$key] ?? $default, $filter, $key, $default);
1505
        }
1506
1507
        return $item;
1508
    }
1509
1510
    /**
1511
     * 排除指定参数获取
1512
     * @access public
1513
     * @param  array  $name 变量名
1514
     * @param  string $type 变量类型
1515
     * @return mixed
1516
     */
1517
    public function except(array $name, string $type = 'param'): array
1518
    {
1519
        $param = $this->$type();
1520
1521
        foreach ($name as $key) {
1522
            if (isset($param[$key])) {
1523
                unset($param[$key]);
1524
            }
1525
        }
1526
1527
        return $param;
1528
    }
1529
1530
    /**
1531
     * 当前是否ssl
1532
     * @access public
1533
     * @return bool
1534
     */
1535
    public function isSsl(): bool
1536
    {
1537
        if ($this->server('HTTPS') && ('1' == $this->server('HTTPS') || 'on' == strtolower($this->server('HTTPS')))) {
1538
            return true;
1539
        } elseif ('https' == $this->server('REQUEST_SCHEME')) {
1540
            return true;
1541
        } elseif ('443' == $this->server('SERVER_PORT')) {
1542
            return true;
1543
        } elseif ('https' == $this->server('HTTP_X_FORWARDED_PROTO')) {
1544
            return true;
1545
        } elseif ($this->config['https_agent_name'] && $this->server($this->config['https_agent_name'])) {
1546
            return true;
1547
        }
1548
1549
        return false;
1550
    }
1551
1552
    /**
1553
     * 当前是否JSON请求
1554
     * @access public
1555
     * @return bool
1556
     */
1557
    public function isJson(): bool
1558
    {
1559
        $contentType = $this->contentType();
1560
        $acceptType  = $this->type();
1561
1562
        return false !== strpos($contentType, 'json') || false !== strpos($acceptType, 'json');
1563
    }
1564
1565
    /**
1566
     * 当前是否Ajax请求
1567
     * @access public
1568
     * @param  bool $ajax true 获取原始ajax请求
1569
     * @return bool
1570
     */
1571
    public function isAjax(bool $ajax = false): bool
1572
    {
1573
        $value  = $this->server('HTTP_X_REQUESTED_WITH');
1574
        $result = $value && 'xmlhttprequest' == strtolower($value) ? true : false;
1575
1576
        if (true === $ajax) {
1577
            return $result;
1578
        }
1579
1580
        return $this->param($this->config['var_ajax']) ? true : $result;
1581
    }
1582
1583
    /**
1584
     * 当前是否Pjax请求
1585
     * @access public
1586
     * @param  bool $pjax true 获取原始pjax请求
1587
     * @return bool
1588
     */
1589
    public function isPjax(bool $pjax = false): bool
1590
    {
1591
        $result = !is_null($this->server('HTTP_X_PJAX')) ? true : false;
1592
1593
        if (true === $pjax) {
1594
            return $result;
1595
        }
1596
1597
        return $this->param($this->config['var_pjax']) ? true : $result;
1598
    }
1599
1600
    /**
1601
     * 获取客户端IP地址
1602
     * @access public
1603
     * @return string
1604
     */
1605
    public function ip(): string
1606
    {
1607
        if (!empty($this->realIP)) {
1608
            return $this->realIP;
1609
        }
1610
1611
        $this->realIP = $this->server('REMOTE_ADDR', '');
1612
1613
        // 如果指定了前端代理服务器IP以及其会发送的IP头
1614
        // 则尝试获取前端代理服务器发送过来的真实IP
1615
        $proxyIp       = $this->config('proxy_server_ip');
1616
        $proxyIpHeader = $this->config('proxy_server_ip_header');
1617
1618
        if (count($proxyIp) > 0 && count($proxyIpHeader) > 0) {
1619
            // 从指定的HTTP头中依次尝试获取IP地址
1620
            // 直到获取到一个合法的IP地址
1621
            foreach ($proxyIpHeader as $header) {
1622
                $tempIP = $this->server($header);
1623
1624
                if (empty($tempIP)) {
1625
                    continue;
1626
                }
1627
1628
                $tempIP = trim(explode(',', $tempIP)[0]);
0 ignored issues
show
Bug introduced by
It seems like $tempIP can also be of type array; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1628
                $tempIP = trim(explode(',', /** @scrutinizer ignore-type */ $tempIP)[0]);
Loading history...
1629
1630
                if (!$this->isValidIP($tempIP)) {
1631
                    $tempIP = null;
1632
                } else {
1633
                    break;
1634
                }
1635
            }
1636
1637
            // tempIP不为空,说明获取到了一个IP地址
1638
            // 这时我们检查 REMOTE_ADDR 是不是指定的前端代理服务器之一
1639
            // 如果是的话说明该 IP头 是由前端代理服务器设置的
1640
            // 否则则是伪装的
1641
            if ($tempIP) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $tempIP seems to be defined by a foreach iteration on line 1621. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
1642
                $realIPBin = $this->ip2bin($this->realIP);
1643
1644
                foreach ($proxyIp as $ip) {
1645
                    $serverIPElements = explode('/', $ip);
1646
                    $serverIP         = $serverIPElements[0];
1647
                    $serverIPPrefix   = $serverIPElements[1] ?? 128;
1648
                    $serverIPBin      = $this->ip2bin($serverIP);
1649
1650
                    // IP类型不符
1651
                    if (strlen($realIPBin) !== strlen($serverIPBin)) {
1652
                        continue;
1653
                    }
1654
1655
                    if (strncmp($realIPBin, $serverIPBin, (int) $serverIPPrefix) === 0) {
1656
                        $this->realIP = $tempIP;
1657
                        break;
1658
                    }
1659
                }
1660
            }
1661
        }
1662
1663
        if (!$this->isValidIP($this->realIP)) {
1664
            $this->realIP = '0.0.0.0';
1665
        }
1666
1667
        return $this->realIP;
1668
    }
1669
1670
    /**
1671
     * 检测是否是合法的IP地址
1672
     *
1673
     * @param string $ip   IP地址
1674
     * @param string $type IP地址类型 (ipv4, ipv6)
1675
     *
1676
     * @return boolean
1677
     */
1678
    public function isValidIP(string $ip, string $type = ''): bool
1679
    {
1680
        switch (strtolower($type)) {
1681
            case 'ipv4':
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
1682
                $flag = FILTER_FLAG_IPV4;
1683
                break;
1684
            case 'ipv6':
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
1685
                $flag = FILTER_FLAG_IPV6;
1686
                break;
1687
            default:
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
1688
                $flag = null;
1689
                break;
1690
        }
1691
1692
        return boolval(filter_var($ip, FILTER_VALIDATE_IP, $flag));
1693
    }
1694
1695
    /**
1696
     * 将IP地址转换为二进制字符串
1697
     *
1698
     * @param string $ip
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
1699
     *
1700
     * @return string
1701
     */
1702
    public function ip2bin(string $ip): string
1703
    {
1704
        if ($this->isValidIP($ip, 'ipv6')) {
1705
            $IPHex = str_split(bin2hex(inet_pton($ip)), 4);
1706
            foreach ($IPHex as $key => $value) {
1707
                $IPHex[$key] = intval($value, 16);
1708
            }
1709
            $IPBin = vsprintf('%016b%016b%016b%016b%016b%016b%016b%016b', $IPHex);
1710
        } else {
1711
            $IPHex = str_split(bin2hex(inet_pton($ip)), 2);
1712
            foreach ($IPHex as $key => $value) {
1713
                $IPHex[$key] = intval($value, 16);
1714
            }
1715
            $IPBin = vsprintf('%08b%08b%08b%08b', $IPHex);
1716
        }
1717
1718
        return $IPBin;
1719
    }
1720
1721
    /**
1722
     * 检测是否使用手机访问
1723
     * @access public
1724
     * @return bool
1725
     */
1726
    public function isMobile(): bool
1727
    {
1728
        if ($this->server('HTTP_VIA') && stristr($this->server('HTTP_VIA'), "wap")) {
1729
            return true;
1730
        } elseif ($this->server('HTTP_ACCEPT') && strpos(strtoupper($this->server('HTTP_ACCEPT')), "VND.WAP.WML")) {
1731
            return true;
1732
        } elseif ($this->server('HTTP_X_WAP_PROFILE') || $this->server('HTTP_PROFILE')) {
1733
            return true;
1734
        } 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'))) {
1735
            return true;
1736
        }
1737
1738
        return false;
1739
    }
1740
1741
    /**
1742
     * 当前URL地址中的scheme参数
1743
     * @access public
1744
     * @return string
1745
     */
1746
    public function scheme(): string
1747
    {
1748
        return $this->isSsl() ? 'https' : 'http';
1749
    }
1750
1751
    /**
1752
     * 当前请求URL地址中的query参数
1753
     * @access public
1754
     * @return string
1755
     */
1756
    public function query(): string
1757
    {
1758
        return $this->server('QUERY_STRING', '');
1759
    }
1760
1761
    /**
1762
     * 设置当前请求的host(包含端口)
1763
     * @access public
1764
     * @param  string $host 主机名(含端口)
1765
     * @return $this
1766
     */
1767
    public function setHost(string $host)
1768
    {
1769
        $this->host = $host;
1770
1771
        return $this;
1772
    }
1773
1774
    /**
1775
     * 当前请求的host
1776
     * @access public
1777
     * @param bool $strict  true 仅仅获取HOST
1 ignored issue
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
1778
     * @return string
1779
     */
1780 3
    public function host(bool $strict = false): string
1781
    {
1782 3
        if ($this->host) {
1783
            $host = $this->host;
1784
        } else {
1785 3
            $host = strval($this->server('HTTP_X_REAL_HOST') ?: $this->server('HTTP_HOST'));
1786
        }
1787
1788 3
        return true === $strict && strpos($host, ':') ? strstr($host, ':', true) : $host;
1789
    }
1790
1791
    /**
1792
     * 当前请求URL地址中的port参数
1793
     * @access public
1794
     * @return string
1795
     */
1796
    public function port(): string
1797
    {
1798
        return $this->server('SERVER_PORT', '');
1799
    }
1800
1801
    /**
1802
     * 当前请求 SERVER_PROTOCOL
1803
     * @access public
1804
     * @return string
1805
     */
1806
    public function protocol(): string
1807
    {
1808
        return $this->server('SERVER_PROTOCOL', '');
1809
    }
1810
1811
    /**
1812
     * 当前请求 REMOTE_PORT
1813
     * @access public
1814
     * @return string
1815
     */
1816
    public function remotePort(): string
1817
    {
1818
        return $this->server('REMOTE_PORT', '');
1819
    }
1820
1821
    /**
1822
     * 当前请求 HTTP_CONTENT_TYPE
1823
     * @access public
1824
     * @return string
1825
     */
1826
    public function contentType(): string
1827
    {
1828
        $contentType = $this->server('CONTENT_TYPE');
1829
1830
        if ($contentType) {
1831
            if (strpos($contentType, ';')) {
1832
                list($type) = explode(';', $contentType);
1833
            } else {
1834
                $type = $contentType;
1835
            }
1836
            return trim($type);
1837
        }
1838
1839
        return '';
1840
    }
1841
1842
    /**
1843
     * 设置或者获取当前请求的调度信息
1844
     * @access public
1845
     * @param  Dispatch  $dispatch 调度信息
1846
     * @return Dispatch
1847
     */
1848
    public function dispatch(Dispatch $dispatch = null)
1849
    {
1850
        if (!is_null($dispatch)) {
1851
            $this->dispatch = $dispatch;
1852
        }
1853
1854
        return $this->dispatch;
1855
    }
1856
1857
    /**
1858
     * 获取当前请求的安全Key
1859
     * @access public
1860
     * @return string
1861
     */
1862
    public function secureKey(): string
1863
    {
1864
        if (is_null($this->secureKey)) {
0 ignored issues
show
introduced by
The condition is_null($this->secureKey) is always false.
Loading history...
1865
            $this->secureKey = uniqid('', true);
1866
        }
1867
1868
        return $this->secureKey;
1869
    }
1870
1871
    /**
1872
     * 设置当前的应用名
1873
     * @access public
1874
     * @param  string $app 应用名
1875
     * @return $this
1876
     */
1877 5
    public function setApp(string $app)
1878
    {
1879 5
        $this->app = $app;
1880 5
        return $this;
1881
    }
1882
1883
    /**
1884
     * 设置当前的控制器名
1885
     * @access public
1886
     * @param  string $controller 控制器名
1887
     * @return $this
1888
     */
1889
    public function setController(string $controller)
1890
    {
1891
        $this->controller = $controller;
1892
        return $this;
1893
    }
1894
1895
    /**
1896
     * 设置当前的操作名
1897
     * @access public
1898
     * @param  string $action 操作名
1899
     * @return $this
1900
     */
1901
    public function setAction(string $action)
1902
    {
1903
        $this->action = $action;
1904
        return $this;
1905
    }
1906
1907
    /**
1908
     * 获取当前的应用名
1909
     * @access public
1910
     * @return string
1911
     */
1912
    public function app(): string
1913
    {
1914
        return $this->app ?: '';
1915
    }
1916
1917
    /**
1918
     * 获取当前的控制器名
1919
     * @access public
1920
     * @param  bool $convert 转换为小写
1921
     * @return string
1922
     */
1923
    public function controller(bool $convert = false): string
1924
    {
1925
        $name = $this->controller ?: '';
1926
        return $convert ? strtolower($name) : $name;
1927
    }
1928
1929
    /**
1930
     * 获取当前的操作名
1931
     * @access public
1932
     * @param  bool $convert 转换为小写
1933
     * @return string
1934
     */
1935
    public function action(bool $convert = false): string
1936
    {
1937
        $name = $this->action ?: '';
1938
        return $convert ? strtolower($name) : $name;
1939
    }
1940
1941
    /**
1942
     * 设置或者获取当前请求的content
1943
     * @access public
1944
     * @return string
1945
     */
1946
    public function getContent(): string
1947
    {
1948
        if (is_null($this->content)) {
0 ignored issues
show
introduced by
The condition is_null($this->content) is always false.
Loading history...
1949
            $this->content = $this->input;
1950
        }
1951
1952
        return $this->content;
1953
    }
1954
1955
    /**
1956
     * 获取当前请求的php://input
1957
     * @access public
1958
     * @return string
1959
     */
1960
    public function getInput(): string
1961
    {
1962
        return $this->input;
1963
    }
1964
1965
    /**
1966
     * 生成请求令牌
1967
     * @access public
1968
     * @param  string $name 令牌名称
1969
     * @param  mixed  $type 令牌生成方法
1970
     * @return string
1971
     */
1972
    public function token(string $name = '__token__', $type = 'md5'): string
1973
    {
1974
        $type  = is_callable($type) ? $type : 'md5';
1975
        $token = call_user_func($type, $this->server('REQUEST_TIME_FLOAT'));
1976
1977
        if ($this->isAjax()) {
1978
            header($name . ': ' . $token);
1979
        }
1980
1981
        $this->session->set($name, $token);
1982
1983
        return $token;
1984
    }
1985
1986
    /**
1987
     * 设置当前地址的请求缓存
1988
     * @access public
1989
     * @param  mixed  $key 缓存标识,支持变量规则 ,例如 item/:name/:id
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1990
     * @param  int   $expire 缓存有效期
1991
     * @param  string $tag    缓存标签
1992
     * @param  array  $except 缓存排除
1993
     * @return mixed
1994
     */
1995
    public function cache($key = null, int $expire = null, string $tag = null, array $except = [])
1996
    {
1997
        $key    = $key ?: $this->config['request_cache'];
1998
        $expire = $expire ?: $this->config['request_cache_expire'];
1999
        $except = !empty($except) ? $except : $this->config['request_cache_except'];
2000
        $tag    = $tag ?: $this->config['request_cache_tag'];
2001
2002
        if ($this->isCheckCache) {
2003
            // 关闭当前缓存
2004
            return;
2005
        }
2006
2007
        // 标记请求缓存检查
2008
        $this->isCheckCache = true;
2009
2010
        foreach ($except as $rule) {
2011
            if (0 === stripos($this->url(), $rule)) {
2012
                return;
2013
            }
2014
        }
2015
2016
        if ($key instanceof \Closure) {
2017
            $key = call_user_func_array($key, [$this]);
2018
        } elseif (true === $key) {
2019
            // 自动缓存功能
2020
            $key = '__URL__';
2021
        } elseif (strpos($key, '|')) {
2022
            list($key, $fun) = explode('|', $key);
2023
        }
2024
2025
        // 特殊规则替换
2026
        if (false !== strpos($key, '__')) {
2027
            $key = str_replace(['__APP__', '__CONTROLLER__', '__ACTION__', '__URL__'], [$this->app, $this->controller, $this->action, md5($this->url(true))], $key);
2028
        }
2029
2030
        if (false !== strpos($key, ':')) {
2031
            $param = $this->param();
2032
            foreach ($param as $item => $val) {
2033
                if (is_string($val) && false !== strpos($key, ':' . $item)) {
2034
                    $key = str_replace(':' . $item, $val, $key);
2035
                }
2036
            }
2037
        } elseif (strpos($key, ']')) {
2038
            if ('[' . $this->ext() . ']' == $key) {
2039
                // 缓存某个后缀的请求
2040
                $key = md5($this->url());
2041
            } else {
2042
                return;
2043
            }
2044
        }
2045
2046
        if (isset($fun)) {
2047
            $key = $fun($key);
2048
        }
2049
2050
        $this->cache = [$key, $expire, $tag];
2051
        return $this->cache;
2052
    }
2053
2054
    /**
2055
     * 读取请求缓存设置
2056
     * @access public
2057
     * @return array|null
2058
     */
2059
    public function getCache()
2060
    {
2061
        return $this->cache;
2062
    }
2063
2064
    /**
2065
     * 设置在中间件传递的数据
2066
     * @access public
2067
     * @param  array $middleware 数据
2068
     * @return $this
2069
     */
2070
    public function withMiddleware(array $middleware)
2071
    {
2072
        $this->middleware = array_merge($this->middleware, $middleware);
2073
        return $this;
2074
    }
2075
2076
    /**
2077
     * 设置GET数据
2078
     * @access public
2079
     * @param  array $get 数据
2080
     * @return $this
2081
     */
2082
    public function withGet(array $get)
2083
    {
2084
        $this->get = $get;
2085
        return $this;
2086
    }
2087
2088
    /**
2089
     * 设置POST数据
2090
     * @access public
2091
     * @param  array $post 数据
2092
     * @return $this
2093
     */
2094
    public function withPost(array $post)
2095
    {
2096
        $this->post = $post;
2097
        return $this;
2098
    }
2099
2100
    /**
2101
     * 设置COOKIE数据
2102
     * @access public
2103
     * @param array $cookie 数据
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
2104
     * @return $this
2105
     */
2106
    public function withCookie(array $cookie)
2107
    {
2108
        $this->cookie = $cookie;
2109
        return $this;
2110
    }
2111
2112
    /**
2113
     * 设置SESSION数据
2114
     * @access public
2115
     * @param Session $session 数据
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
2116
     * @return $this
2117
     */
2118
    public function withSession(Session $session)
2119
    {
2120
        $this->session = $session;
2121
        return $this;
2122
    }
2123
2124
    /**
2125
     * 设置SERVER数据
2126
     * @access public
2127
     * @param  array $server 数据
2128
     * @return $this
2129
     */
2130
    public function withServer(array $server)
2131
    {
2132
        $this->server = array_change_key_case($server, CASE_UPPER);
2133
        return $this;
2134
    }
2135
2136
    /**
2137
     * 设置HEADER数据
2138
     * @access public
2139
     * @param  array $header 数据
2140
     * @return $this
2141
     */
2142
    public function withHeader(array $header)
2143
    {
2144
        $this->header = array_change_key_case($header);
2145
        return $this;
2146
    }
2147
2148
    /**
2149
     * 设置ENV数据
2150
     * @access public
2151
     * @param Env $env 数据
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
2152
     * @return $this
2153
     */
2154
    public function withEnv(Env $env)
2155
    {
2156
        $this->env = $env;
2157
        return $this;
2158
    }
2159
2160
    /**
2161
     * 设置php://input数据
2162
     * @access public
2163
     * @param  string $input RAW数据
2164
     * @return $this
2165
     */
2166
    public function withInput(string $input)
2167
    {
2168
        $this->input = $input;
2169
        return $this;
2170
    }
2171
2172
    /**
2173
     * 设置文件上传数据
2174
     * @access public
2175
     * @param  array $files 上传信息
2176
     * @return $this
2177
     */
2178
    public function withFiles(array $files)
2179
    {
2180
        $this->file = $files;
2181
        return $this;
2182
    }
2183
2184
    /**
2185
     * 设置ROUTE变量
2186
     * @access public
2187
     * @param  array $route 数据
2188
     * @return $this
2189
     */
2190
    public function withRoute(array $route)
2191
    {
2192
        $this->route = $route;
2193
        return $this;
2194
    }
2195
2196
    /**
2197
     * 设置中间传递数据
2198
     * @access public
2199
     * @param  string    $name  参数名
2200
     * @param  mixed     $value 值
2201
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
2202
    public function __set(string $name, $value)
2203
    {
2204
        $this->middleware[$name] = $value;
2205
    }
2206
2207
    /**
2208
     * 获取中间传递数据的值
2209
     * @access public
2210
     * @param  string $name 名称
2211
     * @return mixed
2212
     */
2213
    public function __get(string $name)
2214
    {
2215
        return $this->middleware($name);
2216
    }
2217
2218
    /**
2219
     * 检测请求数据的值
2220
     * @access public
2221
     * @param  string $name 名称
2222
     * @return boolean
2223
     */
2224
    public function __isset(string $name): bool
2225
    {
2226
        return isset($this->param[$name]);
2227
    }
2228
}
2229