Passed
Pull Request — 5.1 (#1979)
by
unknown
20:24 queued 05:37
created

Request::arrayReset()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 2
b 0
f 0
nc 3
nop 1
dl 0
loc 8
rs 10
1
<?php
2
// +----------------------------------------------------------------------
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2018 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
12
namespace think;
13
14
use think\facade\Cookie;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, think\Cookie. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
15
use think\facade\Session;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, think\Session. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
16
17
class Request
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
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
102
     * @var string
103
     */
104
    protected $pathinfo;
105
106
    /**
107
     * pathinfo(不含后缀)
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
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内容
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
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 = [])
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
301
    {
302
        $this->config = array_merge($this->config, $options);
303
304
        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...
305
            $this->filter = $this->config['default_filter'];
306
        }
307
    }
308
309
    public function config($name = null)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
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)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
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)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
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 方法名
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
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地址
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
357
     * @param  string    $method 请求类型
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
358
     * @param  array     $params 请求参数
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
359
     * @param  array     $cookie
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
360
     * @param  array     $files
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
361
     * @param  array     $server
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
362
     * @param  string    $content
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
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)) {
0 ignored issues
show
introduced by
The condition is_null($this->subDomain) is always false.
Loading history...
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)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
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)) {
0 ignored issues
show
introduced by
The condition is_null($this->pathinfo) is always false.
Loading history...
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
                unset($this->get[$this->config['var_pathinfo']]);
686
            } elseif ($this->isCli()) {
687
                // CLI模式下 index.php module/controller/action/params/...
688
                $pathinfo = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
689
            } elseif ('cli-server' == PHP_SAPI) {
690
                $pathinfo = strpos($this->server('REQUEST_URI'), '?') ? strstr($this->server('REQUEST_URI'), '?', true) : $this->server('REQUEST_URI');
691
            } elseif ($this->server('PATH_INFO')) {
692
                $pathinfo = $this->server('PATH_INFO');
693
            }
694
695
            // 分析PATHINFO信息
696
            if (!isset($pathinfo)) {
697
                foreach ($this->config['pathinfo_fetch'] as $type) {
698
                    if ($this->server($type)) {
699
                        $pathinfo = (0 === strpos($this->server($type), $this->server('SCRIPT_NAME'))) ?
700
                        substr($this->server($type), strlen($this->server('SCRIPT_NAME'))) : $this->server($type);
701
                        break;
702
                    }
703
                }
704
            }
705
706
            if (!empty($pathinfo)) {
707
                unset($this->get[$pathinfo], $this->request[$pathinfo]);
708
            }
709
710
            $this->pathinfo = empty($pathinfo) || '/' == $pathinfo ? '' : ltrim($pathinfo, '/');
711
        }
712
713
        return $this->pathinfo;
714
    }
715
716
    /**
717
     * 获取当前请求URL的pathinfo信息(不含URL后缀)
718
     * @access public
719
     * @return string
720
     */
721
    public function path()
722
    {
723
        if (is_null($this->path)) {
0 ignored issues
show
introduced by
The condition is_null($this->path) is always false.
Loading history...
724
            $suffix   = $this->config['url_html_suffix'];
725
            $pathinfo = $this->pathinfo();
726
727
            if (false === $suffix) {
728
                // 禁止伪静态访问
729
                $this->path = $pathinfo;
730
            } elseif ($suffix) {
731
                // 去除正常的URL后缀
732
                $this->path = preg_replace('/\.(' . ltrim($suffix, '.') . ')$/i', '', $pathinfo);
733
            } else {
734
                // 允许任何后缀访问
735
                $this->path = preg_replace('/\.' . $this->ext() . '$/i', '', $pathinfo);
736
            }
737
        }
738
739
        return $this->path;
740
    }
741
742
    /**
743
     * 当前URL的访问后缀
744
     * @access public
745
     * @return string
746
     */
747
    public function ext()
748
    {
749
        return pathinfo($this->pathinfo(), PATHINFO_EXTENSION);
750
    }
751
752
    /**
753
     * 获取当前请求的时间
754
     * @access public
755
     * @param  bool $float 是否使用浮点类型
756
     * @return integer|float
757
     */
758
    public function time($float = false)
759
    {
760
        return $float ? $this->server('REQUEST_TIME_FLOAT') : $this->server('REQUEST_TIME');
761
    }
762
763
    /**
764
     * 当前请求的资源类型
765
     * @access public
766
     * @return false|string
767
     */
768
    public function type()
769
    {
770
        $accept = $this->server('HTTP_ACCEPT');
771
772
        if (empty($accept)) {
773
            return false;
774
        }
775
776
        foreach ($this->mimeType as $key => $val) {
777
            $array = explode(',', $val);
778
            foreach ($array as $k => $v) {
779
                if (stristr($accept, $v)) {
780
                    return $key;
781
                }
782
            }
783
        }
784
785
        return false;
786
    }
787
788
    /**
789
     * 设置资源类型
790
     * @access public
791
     * @param  string|array  $type 资源类型名
792
     * @param  string        $val 资源类型
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
793
     * @return void
794
     */
795
    public function mimeType($type, $val = '')
796
    {
797
        if (is_array($type)) {
798
            $this->mimeType = array_merge($this->mimeType, $type);
799
        } else {
800
            $this->mimeType[$type] = $val;
801
        }
802
    }
803
804
    /**
805
     * 当前的请求类型
806
     * @access public
807
     * @param  bool $origin  是否获取原始请求类型
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
808
     * @return string
809
     */
810
    public function method($origin = false)
811
    {
812
        if ($origin) {
813
            // 获取原始请求类型
814
            return $this->server('REQUEST_METHOD') ?: 'GET';
815
        } elseif (!$this->method) {
816
            if (isset($_POST[$this->config['var_method']])) {
817
                $method = strtolower($_POST[$this->config['var_method']]);
818
                if (in_array($method, ['get', 'post', 'put', 'patch', 'delete'])) {
819
                    $this->method    = strtoupper($method);
820
                    $this->{$method} = $_POST;
821
                } else {
822
                    $this->method = 'POST';
823
                }
824
                unset($_POST[$this->config['var_method']]);
825
            } elseif ($this->server('HTTP_X_HTTP_METHOD_OVERRIDE')) {
826
                $this->method = strtoupper($this->server('HTTP_X_HTTP_METHOD_OVERRIDE'));
827
            } else {
828
                $this->method = $this->server('REQUEST_METHOD') ?: 'GET';
829
            }
830
        }
831
832
        return $this->method;
833
    }
834
835
    /**
836
     * 是否为GET请求
837
     * @access public
838
     * @return bool
839
     */
840
    public function isGet()
841
    {
842
        return $this->method() == 'GET';
843
    }
844
845
    /**
846
     * 是否为POST请求
847
     * @access public
848
     * @return bool
849
     */
850
    public function isPost()
851
    {
852
        return $this->method() == 'POST';
853
    }
854
855
    /**
856
     * 是否为PUT请求
857
     * @access public
858
     * @return bool
859
     */
860
    public function isPut()
861
    {
862
        return $this->method() == 'PUT';
863
    }
864
865
    /**
866
     * 是否为DELTE请求
867
     * @access public
868
     * @return bool
869
     */
870
    public function isDelete()
871
    {
872
        return $this->method() == 'DELETE';
873
    }
874
875
    /**
876
     * 是否为HEAD请求
877
     * @access public
878
     * @return bool
879
     */
880
    public function isHead()
881
    {
882
        return $this->method() == 'HEAD';
883
    }
884
885
    /**
886
     * 是否为PATCH请求
887
     * @access public
888
     * @return bool
889
     */
890
    public function isPatch()
891
    {
892
        return $this->method() == 'PATCH';
893
    }
894
895
    /**
896
     * 是否为OPTIONS请求
897
     * @access public
898
     * @return bool
899
     */
900
    public function isOptions()
901
    {
902
        return $this->method() == 'OPTIONS';
903
    }
904
905
    /**
906
     * 是否为cli
907
     * @access public
908
     * @return bool
909
     */
910
    public function isCli()
911
    {
912
        return PHP_SAPI == 'cli';
913
    }
914
915
    /**
916
     * 是否为cgi
917
     * @access public
918
     * @return bool
919
     */
920
    public function isCgi()
921
    {
922
        return strpos(PHP_SAPI, 'cgi') === 0;
923
    }
924
925
    /**
926
     * 获取当前请求的参数
927
     * @access public
928
     * @param  mixed         $name 变量名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
929
     * @param  mixed         $default 默认值
930
     * @param  string|array  $filter 过滤方法
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
931
     * @return mixed
932
     */
933
    public function param($name = '', $default = null, $filter = '')
934
    {
935
        if (!$this->mergeParam) {
936
            $method = $this->method(true);
937
938
            // 自动获取请求变量
939
            switch ($method) {
940
                case 'POST':
941
                    $vars = $this->post(false);
942
                    break;
943
                case 'PUT':
944
                case 'DELETE':
945
                case 'PATCH':
946
                    $vars = $this->put(false);
947
                    break;
948
                default:
949
                    $vars = [];
950
            }
951
952
            // 当前请求参数和URL地址中的参数合并
953
            $this->param = array_merge($this->param, $this->get(false), $vars, $this->route(false));
954
955
            $this->mergeParam = true;
956
        }
957
958
        if (true === $name) {
959
            // 获取包含文件上传信息的数组
960
            $file = $this->file();
961
            $data = is_array($file) ? array_merge($this->param, $file) : $this->param;
962
963
            return $this->input($data, '', $default, $filter);
964
        }
965
966
        return $this->input($this->param, $name, $default, $filter);
967
    }
968
969
    /**
970
     * 设置路由变量
971
     * @access public
972
     * @param  array         $route 路由变量
973
     * @return $this
974
     */
975
    public function setRouteVars(array $route)
976
    {
977
        $this->route = array_merge($this->route, $route);
978
        return $this;
979
    }
980
981
    /**
982
     * 获取路由参数
983
     * @access public
984
     * @param  string|false  $name 变量名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
985
     * @param  mixed         $default 默认值
986
     * @param  string|array  $filter 过滤方法
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
987
     * @return mixed
988
     */
989
    public function route($name = '', $default = null, $filter = '')
990
    {
991
        return $this->input($this->route, $name, $default, $filter);
992
    }
993
994
    /**
995
     * 获取GET参数
996
     * @access public
997
     * @param  string|false  $name 变量名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
998
     * @param  mixed         $default 默认值
999
     * @param  string|array  $filter 过滤方法
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
1000
     * @return mixed
1001
     */
1002
    public function get($name = '', $default = null, $filter = '')
1003
    {
1004
        if (empty($this->get)) {
1005
            $this->get = $_GET;
1006
        }
1007
1008
        return $this->input($this->get, $name, $default, $filter);
1009
    }
1010
1011
    /**
1012
     * 获取POST参数
1013
     * @access public
1014
     * @param  string|false  $name 变量名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1015
     * @param  mixed         $default 默认值
1016
     * @param  string|array  $filter 过滤方法
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
1017
     * @return mixed
1018
     */
1019
    public function post($name = '', $default = null, $filter = '')
1020
    {
1021
        if (empty($this->post)) {
1022
            $this->post = !empty($_POST) ? $_POST : $this->getInputData($this->input);
1023
        }
1024
1025
        return $this->input($this->post, $name, $default, $filter);
1026
    }
1027
1028
    /**
1029
     * 获取PUT参数
1030
     * @access public
1031
     * @param  string|false      $name 变量名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1032
     * @param  mixed             $default 默认值
1033
     * @param  string|array      $filter 过滤方法
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
1034
     * @return mixed
1035
     */
1036
    public function put($name = '', $default = null, $filter = '')
1037
    {
1038
        if (is_null($this->put)) {
0 ignored issues
show
introduced by
The condition is_null($this->put) is always false.
Loading history...
1039
            $this->put = $this->getInputData($this->input);
1040
        }
1041
1042
        return $this->input($this->put, $name, $default, $filter);
1043
    }
1044
1045
    protected function getInputData($content)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
1046
    {
1047
        if (false !== strpos($this->contentType(), 'json')) {
1048
            return (array) json_decode($content, true);
1049
        } elseif (strpos($content, '=')) {
1050
            parse_str($content, $data);
1051
            return $data;
1052
        }
1053
1054
        return [];
1055
    }
1056
1057
    /**
1058
     * 获取DELETE参数
1059
     * @access public
1060
     * @param  string|false      $name 变量名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1061
     * @param  mixed             $default 默认值
1062
     * @param  string|array      $filter 过滤方法
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
1063
     * @return mixed
1064
     */
1065
    public function delete($name = '', $default = null, $filter = '')
1066
    {
1067
        return $this->put($name, $default, $filter);
1068
    }
1069
1070
    /**
1071
     * 获取PATCH参数
1072
     * @access public
1073
     * @param  string|false      $name 变量名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1074
     * @param  mixed             $default 默认值
1075
     * @param  string|array      $filter 过滤方法
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
1076
     * @return mixed
1077
     */
1078
    public function patch($name = '', $default = null, $filter = '')
1079
    {
1080
        return $this->put($name, $default, $filter);
1081
    }
1082
1083
    /**
1084
     * 获取request变量
1085
     * @access public
1086
     * @param  string|false  $name 变量名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1087
     * @param  mixed         $default 默认值
1088
     * @param  string|array  $filter 过滤方法
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
1089
     * @return mixed
1090
     */
1091
    public function request($name = '', $default = null, $filter = '')
1092
    {
1093
        if (empty($this->request)) {
1094
            $this->request = $_REQUEST;
1095
        }
1096
1097
        return $this->input($this->request, $name, $default, $filter);
1098
    }
1099
1100
    /**
1101
     * 获取session数据
1102
     * @access public
1103
     * @param  string        $name 数据名称
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1104
     * @param  string        $default 默认值
1105
     * @return mixed
1106
     */
1107
    public function session($name = '', $default = null)
1108
    {
1109
        if (empty($this->session)) {
1110
            $this->session = Session::get();
1111
        }
1112
1113
        if ('' === $name) {
1114
            return $this->session;
1115
        }
1116
1117
        $data = $this->getData($this->session, $name);
1118
1119
        return is_null($data) ? $default : $data;
1120
    }
1121
1122
    /**
1123
     * 获取cookie参数
1124
     * @access public
1125
     * @param  string        $name 变量名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1126
     * @param  string        $default 默认值
1127
     * @param  string|array  $filter 过滤方法
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
1128
     * @return mixed
1129
     */
1130
    public function cookie($name = '', $default = null, $filter = '')
1131
    {
1132
        if (empty($this->cookie)) {
1133
            $this->cookie = Cookie::get();
0 ignored issues
show
Bug introduced by
The call to think\facade\Cookie::get() has too few arguments starting with name. ( Ignorable by Annotation )

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

1133
            /** @scrutinizer ignore-call */ 
1134
            $this->cookie = Cookie::get();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
1134
        }
1135
1136
        if (!empty($name)) {
1137
            $data = Cookie::has($name) ? Cookie::get($name) : $default;
1138
        } else {
1139
            $data = $this->cookie;
1140
        }
1141
1142
        // 解析过滤器
1143
        $filter = $this->getFilter($filter, $default);
1144
1145
        if (is_array($data)) {
1146
            array_walk_recursive($data, [$this, 'filterValue'], $filter);
1147
            reset($data);
1148
        } else {
1149
            $this->filterValue($data, $name, $filter);
1150
        }
1151
1152
        return $data;
1153
    }
1154
1155
    /**
1156
     * 获取server参数
1157
     * @access public
1158
     * @param  string        $name 数据名称
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1159
     * @param  string        $default 默认值
1160
     * @return mixed
1161
     */
1162
    public function server($name = '', $default = null)
1163
    {
1164
        if (empty($name)) {
1165
            return $this->server;
1166
        } else {
1167
            $name = strtoupper($name);
1168
        }
1169
1170
        return isset($this->server[$name]) ? $this->server[$name] : $default;
1171
    }
1172
1173
    /**
1174
     * 获取上传的文件信息
1175
     * @access public
1176
     * @param  string   $name 名称
1177
     * @return null|array|\think\File
1178
     */
1179
    public function file($name = '')
1180
    {
1181
        if (empty($this->file)) {
1182
            $this->file = isset($_FILES) ? $_FILES : [];
1183
        }
1184
1185
        $files = $this->file;
1186
        if (!empty($files)) {
1187
            if (strpos($name, '.')) {
1188
                list($name, $sub) = explode('.', $name);
1189
            }
1190
1191
            // 处理上传文件
1192
            $array = $this->dealUploadFile($files, $name);
1193
1194
            if ('' === $name) {
1195
                // 获取全部文件
1196
                return $array;
1197
            } elseif (isset($sub) && isset($array[$name][$sub])) {
1198
                return $array[$name][$sub];
1199
            } elseif (isset($array[$name])) {
1200
                return $array[$name];
1201
            }
1202
        }
1203
1204
        return;
1205
    }
1206
1207
    protected function dealUploadFile($files, $name)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
1208
    {
1209
        $array = [];
1210
        foreach ($files as $key => $file) {
1211
            if ($file instanceof File) {
1212
                $array[$key] = $file;
1213
            } elseif (is_array($file['name'])) {
1214
                $item  = [];
1215
                $keys  = array_keys($file);
1216
                $count = count($file['name']);
1217
1218
                for ($i = 0; $i < $count; $i++) {
1219
                    if ($file['error'][$i] > 0) {
1220
                        if ($name == $key) {
1221
                            $this->throwUploadFileError($file['error'][$i]);
1222
                        } else {
1223
                            continue;
1224
                        }
1225
                    }
1226
1227
                    $temp['key'] = $key;
1228
1229
                    foreach ($keys as $_key) {
1230
                        $temp[$_key] = $file[$_key][$i];
1231
                    }
1232
1233
                    $item[] = (new File($temp['tmp_name']))->setUploadInfo($temp);
1234
                }
1235
1236
                $array[$key] = $item;
1237
            } else {
1238
                if ($file['error'] > 0) {
1239
                    if ($key == $name) {
1240
                        $this->throwUploadFileError($file['error']);
1241
                    } else {
1242
                        continue;
1243
                    }
1244
                }
1245
1246
                $array[$key] = (new File($file['tmp_name']))->setUploadInfo($file);
1247
            }
1248
        }
1249
1250
        return $array;
1251
    }
1252
1253
    protected function throwUploadFileError($error)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
1254
    {
1255
        static $fileUploadErrors = [
1256
            1 => 'upload File size exceeds the maximum value',
1257
            2 => 'upload File size exceeds the maximum value',
1258
            3 => 'only the portion of file is uploaded',
1259
            4 => 'no file to uploaded',
1260
            6 => 'upload temp dir not found',
1261
            7 => 'file write error',
1262
        ];
1263
1264
        $msg = $fileUploadErrors[$error];
1265
1266
        throw new Exception($msg);
1267
    }
1268
1269
    /**
1270
     * 获取环境变量
1271
     * @access public
1272
     * @param  string        $name 数据名称
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1273
     * @param  string        $default 默认值
1274
     * @return mixed
1275
     */
1276
    public function env($name = '', $default = null)
1277
    {
1278
        if (empty($name)) {
1279
            return $this->env;
1280
        } else {
1281
            $name = strtoupper($name);
1282
        }
1283
1284
        return isset($this->env[$name]) ? $this->env[$name] : $default;
1285
    }
1286
1287
    /**
1288
     * 获取当前的Header
1289
     * @access public
1290
     * @param  string   $name header名称
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1291
     * @param  string   $default 默认值
1292
     * @return string|array
1293
     */
1294
    public function header($name = '', $default = null)
1295
    {
1296
        if (empty($this->header)) {
1297
            $header = [];
1298
            if (function_exists('apache_request_headers') && $result = apache_request_headers()) {
1299
                $header = $result;
1300
            } else {
1301
                $server = $this->server;
1302
                foreach ($server as $key => $val) {
1303
                    if (0 === strpos($key, 'HTTP_')) {
1304
                        $key          = str_replace('_', '-', strtolower(substr($key, 5)));
1305
                        $header[$key] = $val;
1306
                    }
1307
                }
1308
                if (isset($server['CONTENT_TYPE'])) {
1309
                    $header['content-type'] = $server['CONTENT_TYPE'];
1310
                }
1311
                if (isset($server['CONTENT_LENGTH'])) {
1312
                    $header['content-length'] = $server['CONTENT_LENGTH'];
1313
                }
1314
            }
1315
            $this->header = array_change_key_case($header);
1316
        }
1317
1318
        if ('' === $name) {
1319
            return $this->header;
1320
        }
1321
1322
        $name = str_replace('_', '-', strtolower($name));
1323
1324
        return isset($this->header[$name]) ? $this->header[$name] : $default;
1325
    }
1326
1327
    /**
1328
     * 递归重置数组指针
1329
     * @access public
1330
     * @param array $data 数据源
1331
     * @return void
1332
     */
1333
    public function arrayReset(array &$data)
1334
    {
1335
        foreach ($data as &$value) {
1336
            if (is_array($value)) {
1337
                $this->arrayReset($value);
1338
            }
1339
        }
1340
        reset($data);
1341
    }
1342
1343
    /**
1344
     * 获取变量 支持过滤和默认值
1345
     * @access public
1346
     * @param  array         $data 数据源
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1347
     * @param  string|false  $name 字段名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1348
     * @param  mixed         $default 默认值
1349
     * @param  string|array  $filter 过滤函数
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
1350
     * @return mixed
1351
     */
1352
    public function input($data = [], $name = '', $default = null, $filter = '')
1353
    {
1354
        if (false === $name) {
1355
            // 获取原始数据
1356
            return $data;
1357
        }
1358
1359
        $name = (string) $name;
1360
        if ('' != $name) {
1361
            // 解析name
1362
            if (strpos($name, '/')) {
1363
                list($name, $type) = explode('/', $name);
1364
            }
1365
1366
            $data = $this->getData($data, $name);
1367
1368
            if (is_null($data)) {
1369
                return $default;
1370
            }
1371
1372
            if (is_object($data)) {
1373
                return $data;
1374
            }
1375
        }
1376
1377
        // 解析过滤器
1378
        $filter = $this->getFilter($filter, $default);
1379
1380
        if (is_array($data)) {
1381
            array_walk_recursive($data, [$this, 'filterValue'], $filter);
1382
            if (version_compare(PHP_VERSION, '7.1.0', '<')) {
1383
                // 恢复PHP版本低于 7.1 时 array_walk_recursive 中消耗的内部指针
1384
                $this->arrayReset($data);
1385
            }
1386
        } else {
1387
            $this->filterValue($data, $name, $filter);
1388
        }
1389
1390
        if (isset($type) && $data !== $default) {
1391
            // 强制类型转换
1392
            $this->typeCast($data, $type);
1393
        }
1394
1395
        return $data;
1396
    }
1397
1398
    /**
1399
     * 获取数据
1400
     * @access public
1401
     * @param  array         $data 数据源
1402
     * @param  string|false  $name 字段名
1403
     * @return mixed
1404
     */
1405
    protected function getData(array $data, $name)
1406
    {
1407
        foreach (explode('.', $name) as $val) {
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type false; 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

1407
        foreach (explode('.', /** @scrutinizer ignore-type */ $name) as $val) {
Loading history...
1408
            if (isset($data[$val])) {
1409
                $data = $data[$val];
1410
            } else {
1411
                return;
1412
            }
1413
        }
1414
1415
        return $data;
1416
    }
1417
1418
    /**
1419
     * 设置或获取当前的过滤规则
1420
     * @access public
1421
     * @param  mixed $filter 过滤规则
1422
     * @return mixed
1423
     */
1424
    public function filter($filter = null)
1425
    {
1426
        if (is_null($filter)) {
1427
            return $this->filter;
1428
        }
1429
1430
        $this->filter = $filter;
1431
    }
1432
1433
    protected function getFilter($filter, $default)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
1434
    {
1435
        if (is_null($filter)) {
1436
            $filter = [];
1437
        } else {
1438
            $filter = $filter ?: $this->filter;
1439
            if (is_string($filter) && false === strpos($filter, '/')) {
1440
                $filter = explode(',', $filter);
1441
            } else {
1442
                $filter = (array) $filter;
1443
            }
1444
        }
1445
1446
        $filter[] = $default;
1447
1448
        return $filter;
1449
    }
1450
1451
    /**
1452
     * 递归过滤给定的值
1453
     * @access public
1454
     * @param  mixed     $value 键值
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
1455
     * @param  mixed     $key 键名
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
1456
     * @param  array     $filters 过滤方法+默认值
1457
     * @return mixed
1458
     */
1459
    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...
1460
    {
1461
        $default = array_pop($filters);
1462
1463
        foreach ($filters as $filter) {
1464
            if (is_callable($filter)) {
1465
                // 调用函数或者方法过滤
1466
                $value = call_user_func($filter, $value);
1467
            } elseif (is_scalar($value)) {
1468
                if (false !== strpos($filter, '/')) {
1469
                    // 正则过滤
1470
                    if (!preg_match($filter, $value)) {
1471
                        // 匹配不成功返回默认值
1472
                        $value = $default;
1473
                        break;
1474
                    }
1475
                } elseif (!empty($filter)) {
1476
                    // filter函数不存在时, 则使用filter_var进行过滤
1477
                    // filter为非整形值时, 调用filter_id取得过滤id
1478
                    $value = filter_var($value, is_int($filter) ? $filter : filter_id($filter));
1479
                    if (false === $value) {
1480
                        $value = $default;
1481
                        break;
1482
                    }
1483
                }
1484
            }
1485
        }
1486
1487
        return $value;
1488
    }
1489
1490
    /**
1491
     * 强制类型转换
1492
     * @access public
1493
     * @param  string $data
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
1494
     * @param  string $type
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
1495
     * @return mixed
1496
     */
1497
    private function typeCast(&$data, $type)
0 ignored issues
show
Coding Style introduced by
Private method name "Request::typeCast" must be prefixed with an underscore
Loading history...
1498
    {
1499
        switch (strtolower($type)) {
1500
            // 数组
1501
            case 'a':
1502
                $data = (array) $data;
1503
                break;
1504
            // 数字
1505
            case 'd':
1506
                $data = (int) $data;
1507
                break;
1508
            // 浮点
1509
            case 'f':
1510
                $data = (float) $data;
1511
                break;
1512
            // 布尔
1513
            case 'b':
1514
                $data = (boolean) $data;
1515
                break;
1516
            // 字符串
1517
            case 's':
1518
                if (is_scalar($data)) {
0 ignored issues
show
introduced by
The condition is_scalar($data) is always true.
Loading history...
1519
                    $data = (string) $data;
1520
                } else {
1521
                    throw new \InvalidArgumentException('variable type error:' . gettype($data));
1522
                }
1523
                break;
1524
        }
1525
    }
1526
1527
    /**
1528
     * 是否存在某个请求参数
1529
     * @access public
1530
     * @param  string    $name 变量名
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter name; 1 found
Loading history...
1531
     * @param  string    $type 变量类型
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter name; 1 found
Loading history...
1532
     * @param  bool      $checkEmpty 是否检测空值
1533
     * @return mixed
1534
     */
1535
    public function has($name, $type = 'param', $checkEmpty = false)
1536
    {
1537
        if (!in_array($type, ['param', 'get', 'post', 'request', 'put', 'patch', 'file', 'session', 'cookie', 'env', 'header', 'route'])) {
1538
            return false;
1539
        }
1540
1541
        if (empty($this->$type)) {
1542
            $param = $this->$type();
1543
        } else {
1544
            $param = $this->$type;
1545
        }
1546
1547
        // 按.拆分成多维数组进行判断
1548
        foreach (explode('.', $name) as $val) {
1549
            if (isset($param[$val])) {
1550
                $param = $param[$val];
1551
            } else {
1552
                return false;
1553
            }
1554
        }
1555
1556
        return ($checkEmpty && '' === $param) ? false : true;
1557
    }
1558
1559
    /**
1560
     * 获取指定的参数
1561
     * @access public
1562
     * @param  string|array  $name 变量名
1563
     * @param  string        $type 变量类型
1564
     * @return mixed
1565
     */
1566
    public function only($name, $type = 'param')
1567
    {
1568
        $param = $this->$type();
1569
1570
        if (is_string($name)) {
1571
            $name = explode(',', $name);
1572
        }
1573
1574
        $item = [];
1575
        foreach ($name as $key => $val) {
1576
1577
            if (is_int($key)) {
1578
                $default = null;
1579
                $key     = $val;
1580
            } else {
1581
                $default = $val;
1582
            }
1583
1584
            if (isset($param[$key])) {
1585
                $item[$key] = $param[$key];
1586
            } elseif (isset($default)) {
1587
                $item[$key] = $default;
1588
            }
1589
        }
1590
1591
        return $item;
1592
    }
1593
1594
    /**
1595
     * 排除指定参数获取
1596
     * @access public
1597
     * @param  string|array  $name 变量名
1598
     * @param  string        $type 变量类型
1599
     * @return mixed
1600
     */
1601
    public function except($name, $type = 'param')
1602
    {
1603
        $param = $this->$type();
1604
        if (is_string($name)) {
1605
            $name = explode(',', $name);
1606
        }
1607
1608
        foreach ($name as $key) {
1609
            if (isset($param[$key])) {
1610
                unset($param[$key]);
1611
            }
1612
        }
1613
1614
        return $param;
1615
    }
1616
1617
    /**
1618
     * 当前是否ssl
1619
     * @access public
1620
     * @return bool
1621
     */
1622
    public function isSsl()
1623
    {
1624
        if ($this->server('HTTPS') && ('1' == $this->server('HTTPS') || 'on' == strtolower($this->server('HTTPS')))) {
1625
            return true;
1626
        } elseif ('https' == $this->server('REQUEST_SCHEME')) {
1627
            return true;
1628
        } elseif ('443' == $this->server('SERVER_PORT')) {
1629
            return true;
1630
        } elseif ('https' == $this->server('HTTP_X_FORWARDED_PROTO')) {
1631
            return true;
1632
        } elseif ($this->config['https_agent_name'] && $this->server($this->config['https_agent_name'])) {
1633
            return true;
1634
        }
1635
1636
        return false;
1637
    }
1638
1639
    /**
1640
     * 当前是否JSON请求
1641
     * @access public
1642
     * @return bool
1643
     */
1644
    public function isJson()
1645
    {
1646
        return false !== strpos($this->type(), 'json');
0 ignored issues
show
Bug introduced by
It seems like $this->type() can also be of type false; however, parameter $haystack of strpos() 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

1646
        return false !== strpos(/** @scrutinizer ignore-type */ $this->type(), 'json');
Loading history...
1647
    }
1648
1649
    /**
1650
     * 当前是否Ajax请求
1651
     * @access public
1652
     * @param  bool $ajax  true 获取原始ajax请求
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
1653
     * @return bool
1654
     */
1655
    public function isAjax($ajax = false)
1656
    {
1657
        $value  = $this->server('HTTP_X_REQUESTED_WITH');
1658
        $result = 'xmlhttprequest' == strtolower($value) ? true : false;
1659
1660
        if (true === $ajax) {
1661
            return $result;
1662
        }
1663
1664
        $result           = $this->param($this->config['var_ajax']) ? true : $result;
1665
        $this->mergeParam = false;
1666
        return $result;
1667
    }
1668
1669
    /**
1670
     * 当前是否Pjax请求
1671
     * @access public
1672
     * @param  bool $pjax  true 获取原始pjax请求
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
1673
     * @return bool
1674
     */
1675
    public function isPjax($pjax = false)
1676
    {
1677
        $result = !is_null($this->server('HTTP_X_PJAX')) ? true : false;
1678
1679
        if (true === $pjax) {
1680
            return $result;
1681
        }
1682
1683
        $result           = $this->param($this->config['var_pjax']) ? true : $result;
1684
        $this->mergeParam = false;
1685
        return $result;
1686
    }
1687
1688
    /**
1689
     * 获取客户端IP地址
1690
     * @access public
1691
     * @param  integer   $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
1692
     * @param  boolean   $adv 是否进行高级模式获取(有可能被伪装)
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
1693
     * @return mixed
1694
     */
1695
    public function ip($type = 0, $adv = true)
1696
    {
1697
        $type      = $type ? 1 : 0;
1698
        static $ip = null;
1699
1700
        if (null !== $ip) {
1701
            return $ip[$type];
1702
        }
1703
1704
        $httpAgentIp = $this->config['http_agent_ip'];
1705
1706
        if ($httpAgentIp && $this->server($httpAgentIp)) {
1707
            $ip = $this->server($httpAgentIp);
1708
        } elseif ($adv) {
1709
            if ($this->server('HTTP_X_FORWARDED_FOR')) {
1710
                $arr = explode(',', $this->server('HTTP_X_FORWARDED_FOR'));
1711
                $pos = array_search('unknown', $arr);
1712
                if (false !== $pos) {
1713
                    unset($arr[$pos]);
1714
                }
1715
                $ip = trim(current($arr));
1716
            } elseif ($this->server('HTTP_CLIENT_IP')) {
1717
                $ip = $this->server('HTTP_CLIENT_IP');
1718
            } elseif ($this->server('REMOTE_ADDR')) {
1719
                $ip = $this->server('REMOTE_ADDR');
1720
            }
1721
        } elseif ($this->server('REMOTE_ADDR')) {
1722
            $ip = $this->server('REMOTE_ADDR');
1723
        }
1724
1725
        // IP地址类型
1726
        $ip_mode = (strpos($ip, ':') === false) ? 'ipv4' : 'ipv6';
1727
1728
        // IP地址合法验证
1729
        if (filter_var($ip, FILTER_VALIDATE_IP) !== $ip) {
1730
            $ip = ('ipv4' === $ip_mode) ? '0.0.0.0' : '::';
1731
        }
1732
1733
        // 如果是ipv4地址,则直接使用ip2long返回int类型ip;如果是ipv6地址,暂时不支持,直接返回0
1734
        $long_ip = ('ipv4' === $ip_mode) ? sprintf("%u", ip2long($ip)) : 0;
1735
1736
        $ip = [$ip, $long_ip];
1737
1738
        return $ip[$type];
1739
    }
1740
1741
    /**
1742
     * 检测是否使用手机访问
1743
     * @access public
1744
     * @return bool
1745
     */
1746
    public function isMobile()
1747
    {
1748
        if ($this->server('HTTP_VIA') && stristr($this->server('HTTP_VIA'), "wap")) {
1749
            return true;
1750
        } elseif ($this->server('HTTP_ACCEPT') && strpos(strtoupper($this->server('HTTP_ACCEPT')), "VND.WAP.WML")) {
1751
            return true;
1752
        } elseif ($this->server('HTTP_X_WAP_PROFILE') || $this->server('HTTP_PROFILE')) {
1753
            return true;
1754
        } 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'))) {
1755
            return true;
1756
        }
1757
1758
        return false;
1759
    }
1760
1761
    /**
1762
     * 当前URL地址中的scheme参数
1763
     * @access public
1764
     * @return string
1765
     */
1766
    public function scheme()
1767
    {
1768
        return $this->isSsl() ? 'https' : 'http';
1769
    }
1770
1771
    /**
1772
     * 当前请求URL地址中的query参数
1773
     * @access public
1774
     * @return string
1775
     */
1776
    public function query()
1777
    {
1778
        return $this->server('QUERY_STRING');
1779
    }
1780
1781
    /**
1782
     * 设置当前请求的host(包含端口)
1783
     * @access public
1784
     * @param  string $host 主机名(含端口)
1785
     * @return $this
1786
     */
1787
    public function setHost($host)
1788
    {
1789
        $this->host = $host;
1790
1791
        return $this;
1792
    }
1793
1794
    /**
1795
     * 当前请求的host
1796
     * @access public
1797
     * @param bool $strict  true 仅仅获取HOST
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
1798
     * @return string
1799
     */
1800
    public function host($strict = false)
1801
    {
1802
        if (!$this->host) {
1803
            $this->host = $this->server('HTTP_X_REAL_HOST') ?: $this->server('HTTP_HOST');
1804
        }
1805
1806
        return true === $strict && strpos($this->host, ':') ? strstr($this->host, ':', true) : $this->host;
1807
    }
1808
1809
    /**
1810
     * 当前请求URL地址中的port参数
1811
     * @access public
1812
     * @return integer
1813
     */
1814
    public function port()
1815
    {
1816
        return $this->server('SERVER_PORT');
1817
    }
1818
1819
    /**
1820
     * 当前请求 SERVER_PROTOCOL
1821
     * @access public
1822
     * @return string
1823
     */
1824
    public function protocol()
1825
    {
1826
        return $this->server('SERVER_PROTOCOL');
1827
    }
1828
1829
    /**
1830
     * 当前请求 REMOTE_PORT
1831
     * @access public
1832
     * @return integer
1833
     */
1834
    public function remotePort()
1835
    {
1836
        return $this->server('REMOTE_PORT');
1837
    }
1838
1839
    /**
1840
     * 当前请求 HTTP_CONTENT_TYPE
1841
     * @access public
1842
     * @return string
1843
     */
1844
    public function contentType()
1845
    {
1846
        $contentType = $this->server('CONTENT_TYPE');
1847
1848
        if ($contentType) {
1849
            if (strpos($contentType, ';')) {
1850
                list($type) = explode(';', $contentType);
1851
            } else {
1852
                $type = $contentType;
1853
            }
1854
            return trim($type);
1855
        }
1856
1857
        return '';
1858
    }
1859
1860
    /**
1861
     * 获取当前请求的路由信息
1862
     * @access public
1863
     * @param  array $route 路由名称
1864
     * @return array
1865
     */
1866
    public function routeInfo(array $route = [])
1867
    {
1868
        if (!empty($route)) {
1869
            $this->routeInfo = $route;
1870
        }
1871
1872
        return $this->routeInfo;
1873
    }
1874
1875
    /**
1876
     * 设置或者获取当前请求的调度信息
1877
     * @access public
1878
     * @param  \think\route\Dispatch  $dispatch 调度信息
1879
     * @return \think\route\Dispatch
1880
     */
1881
    public function dispatch($dispatch = null)
1882
    {
1883
        if (!is_null($dispatch)) {
1884
            $this->dispatch = $dispatch;
1885
        }
1886
1887
        return $this->dispatch;
1888
    }
1889
1890
    /**
1891
     * 获取当前请求的安全Key
1892
     * @access public
1893
     * @return string
1894
     */
1895
    public function secureKey()
1896
    {
1897
        if (is_null($this->secureKey)) {
0 ignored issues
show
introduced by
The condition is_null($this->secureKey) is always false.
Loading history...
1898
            $this->secureKey = uniqid('', true);
1899
        }
1900
1901
        return $this->secureKey;
1902
    }
1903
1904
    /**
1905
     * 设置当前的模块名
1906
     * @access public
1907
     * @param  string $module 模块名
1908
     * @return $this
1909
     */
1910
    public function setModule($module)
1911
    {
1912
        $this->module = $module;
1913
        return $this;
1914
    }
1915
1916
    /**
1917
     * 设置当前的控制器名
1918
     * @access public
1919
     * @param  string $controller 控制器名
1920
     * @return $this
1921
     */
1922
    public function setController($controller)
1923
    {
1924
        $this->controller = $controller;
1925
        return $this;
1926
    }
1927
1928
    /**
1929
     * 设置当前的操作名
1930
     * @access public
1931
     * @param  string $action 操作名
1932
     * @return $this
1933
     */
1934
    public function setAction($action)
1935
    {
1936
        $this->action = $action;
1937
        return $this;
1938
    }
1939
1940
    /**
1941
     * 获取当前的模块名
1942
     * @access public
1943
     * @return string
1944
     */
1945
    public function module()
1946
    {
1947
        return $this->module ?: '';
1948
    }
1949
1950
    /**
1951
     * 获取当前的控制器名
1952
     * @access public
1953
     * @param  bool $convert 转换为小写
1954
     * @return string
1955
     */
1956
    public function controller($convert = false)
1957
    {
1958
        $name = $this->controller ?: '';
1959
        return $convert ? strtolower($name) : $name;
1960
    }
1961
1962
    /**
1963
     * 获取当前的操作名
1964
     * @access public
1965
     * @param  bool $convert 转换为驼峰
1966
     * @return string
1967
     */
1968
    public function action($convert = false)
1969
    {
1970
        $name = $this->action ?: '';
1971
        return $convert ? $name : strtolower($name);
1972
    }
1973
1974
    /**
1975
     * 设置当前的语言
1976
     * @access public
1977
     * @param  string $lang 语言名
1978
     * @return $this
1979
     */
1980
    public function setLangset($lang)
1981
    {
1982
        $this->langset = $lang;
1983
        return $this;
1984
    }
1985
1986
    /**
1987
     * 获取当前的语言
1988
     * @access public
1989
     * @return string
1990
     */
1991
    public function langset()
1992
    {
1993
        return $this->langset ?: '';
1994
    }
1995
1996
    /**
1997
     * 设置或者获取当前请求的content
1998
     * @access public
1999
     * @return string
2000
     */
2001
    public function getContent()
2002
    {
2003
        if (is_null($this->content)) {
0 ignored issues
show
introduced by
The condition is_null($this->content) is always false.
Loading history...
2004
            $this->content = $this->input;
2005
        }
2006
2007
        return $this->content;
2008
    }
2009
2010
    /**
2011
     * 获取当前请求的php://input
2012
     * @access public
2013
     * @return string
2014
     */
2015
    public function getInput()
2016
    {
2017
        return $this->input;
2018
    }
2019
2020
    /**
2021
     * 生成请求令牌
2022
     * @access public
2023
     * @param  string $name 令牌名称
2024
     * @param  mixed  $type 令牌生成方法
2025
     * @return string
2026
     */
2027
    public function token($name = '__token__', $type = null)
2028
    {
2029
        $type  = is_callable($type) ? $type : 'md5';
2030
        $token = call_user_func($type, $this->server('REQUEST_TIME_FLOAT'));
2031
2032
        if ($this->isAjax()) {
2033
            header($name . ': ' . $token);
2034
        }
2035
2036
        facade\Session::set($name, $token);
2037
2038
        return $token;
2039
    }
2040
2041
    /**
2042
     * 设置当前地址的请求缓存
2043
     * @access public
2044
     * @param  string $key 缓存标识,支持变量规则 ,例如 item/:name/:id
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
2045
     * @param  mixed  $expire 缓存有效期
2046
     * @param  array  $except 缓存排除
2047
     * @param  string $tag    缓存标签
2048
     * @return mixed
2049
     */
2050
    public function cache($key, $expire = null, $except = [], $tag = null)
2051
    {
2052
        if (!is_array($except)) {
0 ignored issues
show
introduced by
The condition is_array($except) is always true.
Loading history...
2053
            $tag    = $except;
2054
            $except = [];
2055
        }
2056
2057
        if (false === $key || !$this->isGet() || $this->isCheckCache || false === $expire) {
2058
            // 关闭当前缓存
2059
            return;
2060
        }
2061
2062
        // 标记请求缓存检查
2063
        $this->isCheckCache = true;
2064
2065
        foreach ($except as $rule) {
2066
            if (0 === stripos($this->url(), $rule)) {
2067
                return;
2068
            }
2069
        }
2070
2071
        if ($key instanceof \Closure) {
0 ignored issues
show
introduced by
$key is never a sub-type of Closure.
Loading history...
2072
            $key = call_user_func_array($key, [$this]);
2073
        } elseif (true === $key) {
0 ignored issues
show
introduced by
The condition true === $key is always false.
Loading history...
2074
            // 自动缓存功能
2075
            $key = '__URL__';
2076
        } elseif (strpos($key, '|')) {
2077
            list($key, $fun) = explode('|', $key);
2078
        }
2079
2080
        // 特殊规则替换
2081
        if (false !== strpos($key, '__')) {
2082
            $key = str_replace(['__MODULE__', '__CONTROLLER__', '__ACTION__', '__URL__'], [$this->module, $this->controller, $this->action, md5($this->url(true))], $key);
2083
        }
2084
2085
        if (false !== strpos($key, ':')) {
2086
            $param = $this->param();
2087
            foreach ($param as $item => $val) {
2088
                if (is_string($val) && false !== strpos($key, ':' . $item)) {
2089
                    $key = str_replace(':' . $item, $val, $key);
2090
                }
2091
            }
2092
        } elseif (strpos($key, ']')) {
2093
            if ('[' . $this->ext() . ']' == $key) {
2094
                // 缓存某个后缀的请求
2095
                $key = md5($this->url());
2096
            } else {
2097
                return;
2098
            }
2099
        }
2100
2101
        if (isset($fun)) {
2102
            $key = $fun($key);
2103
        }
2104
2105
        $this->cache = [$key, $expire, $tag];
2106
        return $this->cache;
2107
    }
2108
2109
    /**
2110
     * 读取请求缓存设置
2111
     * @access public
2112
     * @return array
2113
     */
2114
    public function getCache()
2115
    {
2116
        return $this->cache;
2117
    }
2118
2119
    /**
2120
     * 设置GET数据
2121
     * @access public
2122
     * @param  array $get 数据
2123
     * @return $this
2124
     */
2125
    public function withGet(array $get)
2126
    {
2127
        $this->get = $get;
2128
        return $this;
2129
    }
2130
2131
    /**
2132
     * 设置POST数据
2133
     * @access public
2134
     * @param  array $post 数据
2135
     * @return $this
2136
     */
2137
    public function withPost(array $post)
2138
    {
2139
        $this->post = $post;
2140
        return $this;
2141
    }
2142
2143
    /**
2144
     * 设置php://input数据
2145
     * @access public
2146
     * @param  string $input RAW数据
2147
     * @return $this
2148
     */
2149
    public function withInput($input)
2150
    {
2151
        $this->input = $input;
2152
        return $this;
2153
    }
2154
2155
    /**
2156
     * 设置param数据
2157
     * @access public
2158
     * @param array $param
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
2159
     * @return $this
2160
     */
2161
    public function withParam(array $param)
2162
    {
2163
        $this->param = $param;
2164
        return $this;
2165
    }
2166
2167
    /**
2168
     * 设置文件上传数据
2169
     * @access public
2170
     * @param  array $files 上传信息
2171
     * @return $this
2172
     */
2173
    public function withFiles(array $files)
2174
    {
2175
        $this->file = $files;
2176
        return $this;
2177
    }
2178
2179
    /**
2180
     * 设置COOKIE数据
2181
     * @access public
2182
     * @param  array $cookie 数据
2183
     * @return $this
2184
     */
2185
    public function withCookie(array $cookie)
2186
    {
2187
        $this->cookie = $cookie;
2188
        return $this;
2189
    }
2190
2191
    /**
2192
     * 设置SERVER数据
2193
     * @access public
2194
     * @param  array $server 数据
2195
     * @return $this
2196
     */
2197
    public function withServer(array $server)
2198
    {
2199
        $this->server = array_change_key_case($server, CASE_UPPER);
2200
        return $this;
2201
    }
2202
2203
    /**
2204
     * 设置HEADER数据
2205
     * @access public
2206
     * @param  array $header 数据
2207
     * @return $this
2208
     */
2209
    public function withHeader(array $header)
2210
    {
2211
        $this->header = array_change_key_case($header);
2212
        return $this;
2213
    }
2214
2215
    /**
2216
     * 设置ENV数据
2217
     * @access public
2218
     * @param  array $env 数据
2219
     * @return $this
2220
     */
2221
    public function withEnv(array $env)
2222
    {
2223
        $this->env = $env;
2224
        return $this;
2225
    }
2226
2227
    /**
2228
     * 设置ROUTE变量
2229
     * @access public
2230
     * @param  array $route 数据
2231
     * @return $this
2232
     */
2233
    public function withRoute(array $route)
2234
    {
2235
        $this->route = $route;
2236
        return $this;
2237
    }
2238
2239
    /**
2240
     * 设置请求数据
2241
     * @access public
2242
     * @param  string    $name  参数名
2243
     * @param  mixed     $value 值
2244
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
2245
    public function __set($name, $value)
2246
    {
2247
        return $this->param[$name] = $value;
2248
    }
2249
2250
    /**
2251
     * 获取请求数据的值
2252
     * @access public
2253
     * @param  string $name 参数名
2254
     * @return mixed
2255
     */
2256
    public function __get($name)
2257
    {
2258
        return $this->param($name);
2259
    }
2260
2261
    /**
2262
     * 检测请求数据的值
2263
     * @access public
2264
     * @param  string $name 名称
2265
     * @return boolean
2266
     */
2267
    public function __isset($name)
2268
    {
2269
        return isset($this->param[$name]);
2270
    }
2271
2272
    public function __debugInfo()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
2273
    {
2274
        $data = get_object_vars($this);
2275
        unset($data['dispatch'], $data['config']);
2276
2277
        return $data;
2278
    }
2279
}
2280