Passed
Pull Request — 5.1 (#2217)
by
unknown
06:42
created

Request::dealUploadFile()   C

Complexity

Conditions 12
Paths 14

Size

Total Lines 47
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 12
eloc 31
c 5
b 0
f 0
nc 14
nop 2
dl 0
loc 47
rs 6.9666

How to fix   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
// +----------------------------------------------------------------------
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
           
1188
            // 处理上传文件
1189
            $array = $this->dealUploadFile($files, $name);
1190
1191
            list($name, $sub) = explode('.', $name);
1192
            if ('' === $name) {
1193
                // 获取全部文件
1194
                return $array;
1195
            } elseif (isset($sub) && isset($array[$name][$sub])) {
1196
                return $array[$name][$sub];
1197
            } elseif (isset($array[$name])) {
1198
                return $array[$name];
1199
            }
1200
        }
1201
1202
        return;
1203
    }
1204
1205
    protected function dealUploadFile($files, $name)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
1206
    {
1207
        $array = [];
1208
        foreach ($files as $key => $file) {
1209
            if ($file instanceof File) {
1210
                $array[$key] = $file;
1211
            } elseif (is_array($file['name'])) {
1212
                $keys  = array_keys($file);
1213
                $array[$key]=[];
1214
                list($_name,$subName)=explode('.',$name);
1215
                $subFileNames=array_keys($file['name']);
1216
                foreach ($subFileNames as $index => $subFileName) {
1217
                    $errorNo=$file['error'][$subFileName];
1218
                    if ($errorNo > 0) {
1219
                        if($_name==$name){
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...){\n"
Loading history...
Coding Style introduced by
There must be a single space between the closing parenthesis and the opening brace of a multi-line IF statement; found 0 spaces
Loading history...
1220
                            //没有.符号时,忽略没上传的文件
1221
                            if($errorNo==4){
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...){\n"
Loading history...
Coding Style introduced by
There must be a single space between the closing parenthesis and the opening brace of a multi-line IF statement; found 0 spaces
Loading history...
1222
                                continue;
1223
                            }
1224
                        }else {
0 ignored issues
show
Coding Style introduced by
Expected "} else \n"; found "else {\n"
Loading history...
1225
                            //有.符号,但是二级名称不匹配时
1226
                            if ($subName != $subFileName) {
1227
                                continue;
1228
                            }
1229
                        }
1230
                        $this->throwUploadFileError($file['error'][$subFileName]);
1231
                    }
1232
                    $temp['key'] = "{$key}.{$subFileName}";
1233
                    foreach ($keys as $_key) {
1234
                        $temp[$_key] = $file[$_key][$subFileName];
1235
                    }
1236
                    $array[$key][$subFileName] = (new File($temp['tmp_name']))->setUploadInfo($temp);
1237
                }
1238
            } else {
1239
                if ($file['error'] > 0) {
1240
                    if ($key == $name) {
1241
                        $this->throwUploadFileError($file['error']);
1242
                    } else {
1243
                        continue;
1244
                    }
1245
                }
1246
1247
                $array[$key] = (new File($file['tmp_name']))->setUploadInfo($file);
1248
            }
1249
        }
1250
1251
        return $array;
1252
    }
1253
1254
    protected function throwUploadFileError($error)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
1255
    {
1256
        static $fileUploadErrors = [
1257
            1 => 'upload File size exceeds the maximum value',
1258
            2 => 'upload File size exceeds the maximum value',
1259
            3 => 'only the portion of file is uploaded',
1260
            4 => 'no file to uploaded',
1261
            6 => 'upload temp dir not found',
1262
            7 => 'file write error',
1263
        ];
1264
1265
        $msg = $fileUploadErrors[$error];
1266
1267
        throw new Exception($msg);
1268
    }
1269
1270
    /**
1271
     * 获取环境变量
1272
     * @access public
1273
     * @param  string        $name 数据名称
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1274
     * @param  string        $default 默认值
1275
     * @return mixed
1276
     */
1277
    public function env($name = '', $default = null)
1278
    {
1279
        if (empty($name)) {
1280
            return $this->env;
1281
        } else {
1282
            $name = strtoupper($name);
1283
        }
1284
1285
        return isset($this->env[$name]) ? $this->env[$name] : $default;
1286
    }
1287
1288
    /**
1289
     * 获取当前的Header
1290
     * @access public
1291
     * @param  string   $name header名称
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1292
     * @param  string   $default 默认值
1293
     * @return string|array
1294
     */
1295
    public function header($name = '', $default = null)
1296
    {
1297
        if (empty($this->header)) {
1298
            $header = [];
1299
            if (function_exists('apache_request_headers') && $result = apache_request_headers()) {
1300
                $header = $result;
1301
            } else {
1302
                $server = $this->server;
1303
                foreach ($server as $key => $val) {
1304
                    if (0 === strpos($key, 'HTTP_')) {
1305
                        $key          = str_replace('_', '-', strtolower(substr($key, 5)));
1306
                        $header[$key] = $val;
1307
                    }
1308
                }
1309
                if (isset($server['CONTENT_TYPE'])) {
1310
                    $header['content-type'] = $server['CONTENT_TYPE'];
1311
                }
1312
                if (isset($server['CONTENT_LENGTH'])) {
1313
                    $header['content-length'] = $server['CONTENT_LENGTH'];
1314
                }
1315
            }
1316
            $this->header = array_change_key_case($header);
1317
        }
1318
1319
        if ('' === $name) {
1320
            return $this->header;
1321
        }
1322
1323
        $name = str_replace('_', '-', strtolower($name));
1324
1325
        return isset($this->header[$name]) ? $this->header[$name] : $default;
1326
    }
1327
1328
    /**
1329
     * 递归重置数组指针
1330
     * @access public
1331
     * @param array $data 数据源
1332
     * @return void
1333
     */
1334
    public function arrayReset(array &$data)
1335
    {
1336
        foreach ($data as &$value) {
1337
            if (is_array($value)) {
1338
                $this->arrayReset($value);
1339
            }
1340
        }
1341
        reset($data);
1342
    }
1343
1344
    /**
1345
     * 获取变量 支持过滤和默认值
1346
     * @access public
1347
     * @param  array         $data 数据源
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1348
     * @param  string|false  $name 字段名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1349
     * @param  mixed         $default 默认值
1350
     * @param  string|array  $filter 过滤函数
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
1351
     * @return mixed
1352
     */
1353
    public function input($data = [], $name = '', $default = null, $filter = '')
1354
    {
1355
        if (false === $name) {
1356
            // 获取原始数据
1357
            return $data;
1358
        }
1359
1360
        $name = (string) $name;
1361
        if ('' != $name) {
1362
            // 解析name
1363
            if (strpos($name, '/')) {
1364
                list($name, $type) = explode('/', $name);
1365
            }
1366
1367
            $data = $this->getData($data, $name);
1368
1369
            if (is_null($data)) {
1370
                return $default;
1371
            }
1372
1373
            if (is_object($data)) {
1374
                return $data;
1375
            }
1376
        }
1377
1378
        // 解析过滤器
1379
        $filter = $this->getFilter($filter, $default);
1380
1381
        if (is_array($data)) {
1382
            array_walk_recursive($data, [$this, 'filterValue'], $filter);
1383
            if (version_compare(PHP_VERSION, '7.1.0', '<')) {
1384
                // 恢复PHP版本低于 7.1 时 array_walk_recursive 中消耗的内部指针
1385
                $this->arrayReset($data);
1386
            }
1387
        } else {
1388
            $this->filterValue($data, $name, $filter);
1389
        }
1390
1391
        if (isset($type) && $data !== $default) {
1392
            // 强制类型转换
1393
            $this->typeCast($data, $type);
1394
        }
1395
1396
        return $data;
1397
    }
1398
1399
    /**
1400
     * 获取数据
1401
     * @access public
1402
     * @param  array         $data 数据源
1403
     * @param  string|false  $name 字段名
1404
     * @return mixed
1405
     */
1406
    protected function getData(array $data, $name)
1407
    {
1408
        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

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

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