Passed
Push — 6.0 ( c6d9ce...351e06 )
by liu
03:47
created

Request::getContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think;
14
15
use think\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...
16
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...
17
use think\route\Dispatch;
18
19
class Request
1 ignored issue
show
Coding Style introduced by
Missing class doc comment
Loading history...
20
{
21
    /**
22
     * 配置
23
     * @var array
24
     */
25
    protected $config = [
26
        // PATHINFO变量名 用于兼容模式
27
        'var_pathinfo'           => 's',
28
        // 兼容PATH_INFO获取
29
        'pathinfo_fetch'         => ['ORIG_PATH_INFO', 'REDIRECT_PATH_INFO', 'REDIRECT_URL'],
30
        // 表单请求类型伪装变量
31
        'var_method'             => '_method',
32
        // 表单ajax伪装变量
33
        'var_ajax'               => '_ajax',
34
        // 表单pjax伪装变量
35
        'var_pjax'               => '_pjax',
36
        // 默认全局过滤方法 用逗号分隔多个
37
        'default_filter'         => '',
38
        // 域名根,如thinkphp.cn
39
        'url_domain_root'        => '',
40
        // HTTPS代理标识
41
        'https_agent_name'       => '',
42
        // 前端代理服务器IP
43
        'proxy_server_ip'        => [],
44
        // 前端代理服务器真实IP头
45
        'proxy_server_ip_header' => ['HTTP_X_REAL_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP', 'HTTP_X_CLIENT_IP', 'HTTP_X_CLUSTER_CLIENT_IP'],
46
        // URL伪静态后缀
47
        'url_html_suffix'        => 'html',
48
        // 是否开启请求缓存 true自动缓存 支持设置请求缓存规则
49
        'request_cache'          => false,
50
        // 请求缓存有效期
51
        'request_cache_expire'   => null,
52
        // 全局请求缓存排除规则
53
        'request_cache_except'   => [],
54
        // 请求缓存的Tag
55
        'request_cache_tag'      => '',
56
    ];
57
58
    /**
59
     * 请求类型
60
     * @var string
61
     */
62
    protected $method;
63
64
    /**
65
     * 域名(含协议及端口)
66
     * @var string
67
     */
68
    protected $domain;
69
70
    /**
71
     * HOST(含端口)
72
     * @var string
73
     */
74
    protected $host;
75
76
    /**
77
     * 子域名
78
     * @var string
79
     */
80
    protected $subDomain;
81
82
    /**
83
     * 泛域名
84
     * @var string
85
     */
86
    protected $panDomain;
87
88
    /**
89
     * 当前URL地址
90
     * @var string
91
     */
92
    protected $url;
93
94
    /**
95
     * 基础URL
96
     * @var string
97
     */
98
    protected $baseUrl;
99
100
    /**
101
     * 当前执行的文件
102
     * @var string
103
     */
104
    protected $baseFile;
105
106
    /**
107
     * 访问的ROOT地址
108
     * @var string
109
     */
110
    protected $root;
111
112
    /**
113
     * pathinfo
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
114
     * @var string
115
     */
116
    protected $pathinfo;
117
118
    /**
119
     * pathinfo(不含后缀)
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
120
     * @var string
121
     */
122
    protected $path;
123
124
    /**
125
     * 当前请求的IP地址
126
     * @var string
127
     */
128
    protected $realIP;
129
130
    /**
131
     * 当前路由信息
132
     * @var array
133
     */
134
    protected $routeInfo = [];
135
136
    /**
137
     * 当前调度信息
138
     * @var Dispatch
139
     */
140
    protected $dispatch;
141
142
    /**
143
     * 当前应用名
144
     * @var string
145
     */
146
    protected $app;
147
148
    /**
149
     * 当前控制器名
150
     * @var string
151
     */
152
    protected $controller;
153
154
    /**
155
     * 当前操作名
156
     * @var string
157
     */
158
    protected $action;
159
160
    /**
161
     * 当前语言集
162
     * @var string
163
     */
164
    protected $langset;
165
166
    /**
167
     * 当前请求参数
168
     * @var array
169
     */
170
    protected $param = [];
171
172
    /**
173
     * 当前GET参数
174
     * @var array
175
     */
176
    protected $get = [];
177
178
    /**
179
     * 当前POST参数
180
     * @var array
181
     */
182
    protected $post = [];
183
184
    /**
185
     * 当前REQUEST参数
186
     * @var array
187
     */
188
    protected $request = [];
189
190
    /**
191
     * 当前ROUTE参数
192
     * @var array
193
     */
194
    protected $route = [];
195
196
    /**
197
     * 中间件传递的参数
198
     * @var array
199
     */
200
    protected $middleware = [];
201
202
    /**
203
     * 当前PUT参数
204
     * @var array
205
     */
206
    protected $put;
207
208
    /**
209
     * 当前SESSION参数
210
     * @var array
211
     */
212
    protected $session = [];
213
214
    /**
215
     * 当前FILE参数
216
     * @var array
217
     */
218
    protected $file = [];
219
220
    /**
221
     * 当前COOKIE参数
222
     * @var array
223
     */
224
    protected $cookie = [];
225
226
    /**
227
     * 当前SERVER参数
228
     * @var array
229
     */
230
    protected $server = [];
231
232
    /**
233
     * 当前ENV参数
234
     * @var array
235
     */
236
    protected $env = [];
237
238
    /**
239
     * 当前HEADER参数
240
     * @var array
241
     */
242
    protected $header = [];
243
244
    /**
245
     * 资源类型定义
246
     * @var array
247
     */
248
    protected $mimeType = [
249
        'xml'   => 'application/xml,text/xml,application/x-xml',
250
        'json'  => 'application/json,text/x-json,application/jsonrequest,text/json',
251
        'js'    => 'text/javascript,application/javascript,application/x-javascript',
252
        'css'   => 'text/css',
253
        'rss'   => 'application/rss+xml',
254
        'yaml'  => 'application/x-yaml,text/yaml',
255
        'atom'  => 'application/atom+xml',
256
        'pdf'   => 'application/pdf',
257
        'text'  => 'text/plain',
258
        'image' => 'image/png,image/jpg,image/jpeg,image/pjpeg,image/gif,image/webp,image/*',
259
        'csv'   => 'text/csv',
260
        'html'  => 'text/html,application/xhtml+xml,*/*',
261
    ];
262
263
    /**
264
     * 当前请求内容
265
     * @var string
266
     */
267
    protected $content;
268
269
    /**
270
     * 全局过滤规则
271
     * @var array
272
     */
273
    protected $filter;
274
275
    /**
276
     * php://input内容
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
277
     * @var string
278
     */
279
    // php://input
280
    protected $input;
281
282
    /**
283
     * 请求缓存
284
     * @var array
285
     */
286
    protected $cache;
287
288
    /**
289
     * 缓存是否检查
290
     * @var bool
291
     */
292
    protected $isCheckCache;
293
294
    /**
295
     * 请求安全Key
296
     * @var string
297
     */
298
    protected $secureKey;
299
300
    /**
301
     * 是否合并Param
302
     * @var bool
303
     */
304
    protected $mergeParam = false;
305
306
    /**
307
     * 架构函数
308
     * @access public
309
     * @param  array  $options 参数
310
     */
311
    public function __construct(array $options = [])
312
    {
313
        $this->init($options);
314
315
        // 保存 php://input
316
        $this->input = file_get_contents('php://input');
317
    }
318
319
    public function init(array $options = []): void
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
320
    {
321
        $this->config = array_merge($this->config, $options);
322
323
        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...
324
            $this->filter = $this->config['default_filter'];
325
        }
326
    }
327
328
    public function config($name = null)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
329
    {
330
        if (is_null($name)) {
331
            return $this->config;
332
        }
333
334
        return $this->config[$name] ?? null;
335
    }
336
337
    public static function __make(App $app, Config $config)
1 ignored issue
show
Coding Style introduced by
Method name "Request::__make" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
Coding Style introduced by
Missing function doc comment
Loading history...
338
    {
339
        $request = new static($config->get('route'));
0 ignored issues
show
Bug introduced by
It seems like $config->get('route') can also be of type null; however, parameter $options of think\Request::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

339
        $request = new static(/** @scrutinizer ignore-type */ $config->get('route'));
Loading history...
340
341
        $request->server  = $_SERVER;
342
        $request->env     = $app->env->get();
343
        $request->get     = $_GET;
344
        $request->post    = $_POST ?: $request->getInputData($request->input);
345
        $request->put     = $request->getInputData($request->input);
346
        $request->request = $_REQUEST;
347
        $request->session = $app->session->get();
348
        $request->cookie  = $app->cookie->get();
349
        $request->file    = $_FILES ?? [];
350
351
        if (function_exists('apache_request_headers') && $result = apache_request_headers()) {
352
            $header = $result;
353
        } else {
354
            $header = [];
355
            $server = $_SERVER;
356
            foreach ($server as $key => $val) {
357
                if (0 === strpos($key, 'HTTP_')) {
358
                    $key          = str_replace('_', '-', strtolower(substr($key, 5)));
359
                    $header[$key] = $val;
360
                }
361
            }
362
            if (isset($server['CONTENT_TYPE'])) {
363
                $header['content-type'] = $server['CONTENT_TYPE'];
364
            }
365
            if (isset($server['CONTENT_LENGTH'])) {
366
                $header['content-length'] = $server['CONTENT_LENGTH'];
367
            }
368
        }
369
370
        $request->header = array_change_key_case($header);
371
372
        return $request;
373
    }
374
375
    /**
376
     * 创建一个URL请求
377
     * @access public
378
     * @param  string    $uri URL地址
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
379
     * @param  string    $method 请求类型
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
380
     * @param  array     $params 请求参数
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
381
     * @param  array     $cookie
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
382
     * @param  array     $files
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
383
     * @param  array     $server
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
384
     * @param  string    $content
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
385
     * @return \think\Request
386
     */
387
    public function create(string $uri, string $method = 'GET', array $params = [], array $cookie = [], array $files = [], array $server = [], string $content = null)
388
    {
389
        $server['PATH_INFO']      = '';
390
        $server['REQUEST_METHOD'] = strtoupper($method);
391
        $info                     = parse_url($uri);
392
393
        if (isset($info['host'])) {
394
            $server['SERVER_NAME'] = $info['host'];
395
            $server['HTTP_HOST']   = $info['host'];
396
        }
397
398
        if (isset($info['scheme'])) {
399
            if ('https' === $info['scheme']) {
400
                $server['HTTPS']       = 'on';
401
                $server['SERVER_PORT'] = 443;
402
            } else {
403
                unset($server['HTTPS']);
404
                $server['SERVER_PORT'] = 80;
405
            }
406
        }
407
408
        if (isset($info['port'])) {
409
            $server['SERVER_PORT'] = $info['port'];
410
            $server['HTTP_HOST']   = $server['HTTP_HOST'] . ':' . $info['port'];
411
        }
412
413
        if (isset($info['user'])) {
414
            $server['PHP_AUTH_USER'] = $info['user'];
415
        }
416
417
        if (isset($info['pass'])) {
418
            $server['PHP_AUTH_PW'] = $info['pass'];
419
        }
420
421
        if (!isset($info['path'])) {
422
            $info['path'] = '/';
423
        }
424
425
        $options     = [];
426
        $queryString = '';
427
428
        $options[strtolower($method)] = $params;
429
430
        if (isset($info['query'])) {
431
            parse_str(html_entity_decode($info['query']), $query);
432
            if (!empty($params)) {
433
                $params      = array_replace($query, $params);
434
                $queryString = http_build_query($params, '', '&');
435
            } else {
436
                $params      = $query;
437
                $queryString = $info['query'];
438
            }
439
        } elseif (!empty($params)) {
440
            $queryString = http_build_query($params, '', '&');
441
        }
442
443
        if ($queryString) {
444
            parse_str($queryString, $get);
445
            $options['get'] = isset($options['get']) ? array_merge($get, $options['get']) : $get;
446
        }
447
448
        $server['REQUEST_URI']  = $info['path'] . ('' !== $queryString ? '?' . $queryString : '');
449
        $server['QUERY_STRING'] = $queryString;
450
        $options['cookie']      = $cookie;
451
        $options['param']       = $params;
452
        $options['file']        = $files;
453
        $options['server']      = $server;
454
        $options['url']         = $server['REQUEST_URI'];
455
        $options['baseUrl']     = $info['path'];
456
        $options['pathinfo']    = '/' == $info['path'] ? '/' : ltrim($info['path'], '/');
457
        $options['method']      = $server['REQUEST_METHOD'];
458
        $options['domain']      = isset($info['scheme']) ? $info['scheme'] . '://' . $server['HTTP_HOST'] : '';
459
        $options['content']     = $content;
460
461
        foreach ($options as $name => $item) {
462
            if (property_exists($this, $name)) {
463
                $this->$name = $item;
464
            }
465
        }
466
467
        return $this;
468
    }
469
470
    /**
471
     * 设置当前包含协议的域名
472
     * @access public
473
     * @param  string $domain 域名
474
     * @return $this
475
     */
476
    public function setDomain(string $domain)
477
    {
478
        $this->domain = $domain;
479
        return $this;
480
    }
481
482
    /**
483
     * 获取当前包含协议的域名
484
     * @access public
485
     * @param  bool $port 是否需要去除端口号
486
     * @return string
487
     */
488
    public function domain(bool $port = false): string
489
    {
490
        return $this->scheme() . '://' . $this->host($port);
491
    }
492
493
    /**
494
     * 获取当前根域名
495
     * @access public
496
     * @return string
497
     */
498
    public function rootDomain(): string
499
    {
500
        $root = $this->config['url_domain_root'];
501
502
        if (!$root) {
503
            $item  = explode('.', $this->host());
504
            $count = count($item);
505
            $root  = $count > 1 ? $item[$count - 2] . '.' . $item[$count - 1] : $item[0];
506
        }
507
508
        return $root;
509
    }
510
511
    /**
512
     * 获取当前子域名
513
     * @access public
514
     * @return string
515
     */
516
    public function subDomain(): string
517
    {
518
        if (is_null($this->subDomain)) {
0 ignored issues
show
introduced by
The condition is_null($this->subDomain) is always false.
Loading history...
519
            // 获取当前主域名
520
            $rootDomain = $this->config['url_domain_root'];
521
522
            if ($rootDomain) {
523
                // 配置域名根 例如 thinkphp.cn 163.com.cn 如果是国家级域名 com.cn net.cn 之类的域名需要配置
524
                $domain = explode('.', rtrim(stristr($this->host(), $rootDomain, true), '.'));
525
            } else {
526
                $domain = explode('.', $this->host(), -2);
527
            }
528
529
            $this->subDomain = implode('.', $domain);
530
        }
531
532
        return $this->subDomain;
533
    }
534
535
    /**
536
     * 设置当前泛域名的值
537
     * @access public
538
     * @param  string $domain 域名
539
     * @return $this
540
     */
541
    public function setPanDomain(string $domain)
542
    {
543
        $this->panDomain = $domain;
544
        return $this;
545
    }
546
547
    /**
548
     * 获取当前泛域名的值
549
     * @access public
550
     * @return string
551
     */
552
    public function panDomain(): string
553
    {
554
        return $this->panDomain ?: '';
555
    }
556
557
    /**
558
     * 设置当前完整URL 包括QUERY_STRING
559
     * @access public
560
     * @param  string $url URL地址
561
     * @return $this
562
     */
563
    public function setUrl(string $url)
564
    {
565
        $this->url = $url;
566
        return $this;
567
    }
568
569
    /**
570
     * 获取当前完整URL 包括QUERY_STRING
571
     * @access public
572
     * @param  bool $complete 是否包含完整域名
573
     * @return string
574
     */
575
    public function url(bool $complete = false): string
576
    {
577
578
        if ($this->url) {
579
            $url = $this->url;
580
        } elseif ($this->server('HTTP_X_REWRITE_URL')) {
581
            $url = $this->server('HTTP_X_REWRITE_URL');
582
        } elseif ($this->server('REQUEST_URI')) {
583
            $url = $this->server('REQUEST_URI');
584
        } elseif ($this->server('ORIG_PATH_INFO')) {
585
            $url = $this->server('ORIG_PATH_INFO') . (!empty($this->server('QUERY_STRING')) ? '?' . $this->server('QUERY_STRING') : '');
586
        } elseif (isset($_SERVER['argv'][1])) {
587
            $url = $_SERVER['argv'][1];
588
        } else {
589
            $url = '';
590
        }
591
592
        return $complete ? $this->domain() . $url : $url;
593
    }
594
595
    /**
596
     * 设置当前URL 不含QUERY_STRING
597
     * @access public
598
     * @param  string $url URL地址
599
     * @return $this
600
     */
601
    public function setBaseUrl(string $url)
602
    {
603
        $this->baseUrl = $url;
604
        return $this;
605
    }
606
607
    /**
608
     * 获取当前URL 不含QUERY_STRING
609
     * @access public
610
     * @param  bool $complete 是否包含完整域名
611
     * @return string
612
     */
613
    public function baseUrl(bool $complete = false): string
614
    {
615
        if (!$this->baseUrl) {
616
            $str           = $this->url();
617
            $this->baseUrl = strpos($str, '?') ? strstr($str, '?', true) : $str;
618
        }
619
620
        return $complete ? $this->domain() . $this->baseUrl : $this->baseUrl;
621
    }
622
623
    /**
624
     * 获取当前执行的文件 SCRIPT_NAME
625
     * @access public
626
     * @param  bool $complete 是否包含完整域名
627
     * @return string
628
     */
629
    public function baseFile(bool $complete = false): string
630
    {
631
        if (!$this->baseFile) {
632
            $url = '';
633
            if (!$this->isCli()) {
634
                $script_name = basename($this->server('SCRIPT_FILENAME'));
635
                if (basename($this->server('SCRIPT_NAME')) === $script_name) {
636
                    $url = $this->server('SCRIPT_NAME');
637
                } elseif (basename($this->server('PHP_SELF')) === $script_name) {
638
                    $url = $this->server('PHP_SELF');
639
                } elseif (basename($this->server('ORIG_SCRIPT_NAME')) === $script_name) {
640
                    $url = $this->server('ORIG_SCRIPT_NAME');
641
                } elseif (($pos = strpos($this->server('PHP_SELF'), '/' . $script_name)) !== false) {
642
                    $url = substr($this->server('SCRIPT_NAME'), 0, $pos) . '/' . $script_name;
643
                } elseif ($this->server('DOCUMENT_ROOT') && strpos($this->server('SCRIPT_FILENAME'), $this->server('DOCUMENT_ROOT')) === 0) {
644
                    $url = str_replace('\\', '/', str_replace($this->server('DOCUMENT_ROOT'), '', $this->server('SCRIPT_FILENAME')));
645
                }
646
            }
647
            $this->baseFile = $url;
648
        }
649
650
        return $complete ? $this->domain() . $this->baseFile : $this->baseFile;
651
    }
652
653
    /**
654
     * 设置URL访问根地址
655
     * @access public
656
     * @param  string $url URL地址
657
     * @return $this
658
     */
659
    public function setRoot(string $url)
660
    {
661
        $this->root = $url;
662
        return $this;
663
    }
664
665
    /**
666
     * 获取URL访问根地址
667
     * @access public
668
     * @param  bool $complete 是否包含完整域名
669
     * @return string
670
     */
671
    public function root(bool $complete = false): string
672
    {
673
        if (!$this->root) {
674
            $file = $this->baseFile();
675
            if ($file && 0 !== strpos($this->url(), $file)) {
676
                $file = str_replace('\\', '/', dirname($file));
677
            }
678
            $this->root = rtrim($file, '/');
679
        }
680
681
        return $complete ? $this->domain() . $this->root : $this->root;
682
    }
683
684
    /**
685
     * 获取URL访问根目录
686
     * @access public
687
     * @return string
688
     */
689
    public function rootUrl(): string
690
    {
691
        $base = $this->root();
692
        $root = strpos($base, '.') ? ltrim(dirname($base), DIRECTORY_SEPARATOR) : $base;
693
694
        if ('' != $root) {
695
            $root = '/' . ltrim($root, '/');
696
        }
697
698
        return $root;
699
    }
700
701
    /**
702
     * 设置当前请求的pathinfo
703
     * @access public
704
     * @param  string $pathinfo
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
705
     * @return $this
706
     */
707
    public function setPathinfo(string $pathinfo)
708
    {
709
        $this->pathinfo = $pathinfo;
710
        return $this;
711
    }
712
713
    /**
714
     * 获取当前请求URL的pathinfo信息(含URL后缀)
715
     * @access public
716
     * @return string
717
     */
718
    public function pathinfo(): string
719
    {
720
        if (is_null($this->pathinfo)) {
0 ignored issues
show
introduced by
The condition is_null($this->pathinfo) is always false.
Loading history...
721
            if (isset($_GET[$this->config['var_pathinfo']])) {
722
                // 判断URL里面是否有兼容模式参数
723
                $pathinfo = $_GET[$this->config['var_pathinfo']];
724
                unset($_GET[$this->config['var_pathinfo']]);
725
            } elseif ($this->server('PATH_INFO')) {
726
                $pathinfo = $this->server('PATH_INFO');
727
            } elseif ($this->server('REQUEST_URI')) {
728
                $pathinfo = strpos($this->server('REQUEST_URI'), '?') ? strstr($this->server('REQUEST_URI'), '?', true) : $this->server('REQUEST_URI');
729
            } elseif (isset($_SERVER['argv'][1])) {
730
                // CLI模式下 index.php controller/action/params/...
731
                $pathinfo = $_SERVER['argv'][1];
732
            }
733
734
            // 分析PATHINFO信息
735
            if (!isset($pathinfo)) {
736
                foreach ($this->config['pathinfo_fetch'] as $type) {
737
                    if ($this->server($type)) {
738
                        $pathinfo = (0 === strpos($this->server($type), $this->server('SCRIPT_NAME'))) ?
739
                        substr($this->server($type), strlen($this->server('SCRIPT_NAME'))) : $this->server($type);
740
                        break;
741
                    }
742
                }
743
            }
744
            $this->pathinfo = empty($pathinfo) || '/' == $pathinfo ? '' : ltrim($pathinfo, '/');
745
        }
746
747
        return $this->pathinfo;
748
    }
749
750
    /**
751
     * 获取当前请求URL的pathinfo信息(不含URL后缀)
752
     * @access public
753
     * @return string
754
     */
755
    public function path(): string
756
    {
757
        $suffix   = $this->config['url_html_suffix'];
758
        $pathinfo = $this->pathinfo();
759
760
        if (false === $suffix) {
761
            // 禁止伪静态访问
762
            $path = $pathinfo;
763
        } elseif ($suffix) {
764
            // 去除正常的URL后缀
765
            $path = preg_replace('/\.(' . ltrim($suffix, '.') . ')$/i', '', $pathinfo);
766
        } else {
767
            // 允许任何后缀访问
768
            $path = preg_replace('/\.' . $this->ext() . '$/i', '', $pathinfo);
769
        }
770
771
        return $path;
772
    }
773
774
    /**
775
     * 当前URL的访问后缀
776
     * @access public
777
     * @return string
778
     */
779
    public function ext(): string
780
    {
781
        return pathinfo($this->pathinfo(), PATHINFO_EXTENSION);
782
    }
783
784
    /**
785
     * 获取当前请求的时间
786
     * @access public
787
     * @param  bool $float 是否使用浮点类型
788
     * @return integer|float
789
     */
790
    public function time(bool $float = false)
791
    {
792
        return $float ? $this->server('REQUEST_TIME_FLOAT') : $this->server('REQUEST_TIME');
793
    }
794
795
    /**
796
     * 当前请求的资源类型
797
     * @access public
798
     * @return false|string
799
     */
800
    public function type()
801
    {
802
        $accept = $this->server('HTTP_ACCEPT');
803
804
        if (empty($accept)) {
805
            return false;
806
        }
807
808
        foreach ($this->mimeType as $key => $val) {
809
            $array = explode(',', $val);
810
            foreach ($array as $k => $v) {
811
                if (stristr($accept, $v)) {
812
                    return $key;
813
                }
814
            }
815
        }
816
817
        return false;
818
    }
819
820
    /**
821
     * 设置资源类型
822
     * @access public
823
     * @param  string|array $type 资源类型名
824
     * @param  string       $val 资源类型
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
825
     * @return void
826
     */
827
    public function mimeType($type, $val = ''): void
828
    {
829
        if (is_array($type)) {
830
            $this->mimeType = array_merge($this->mimeType, $type);
831
        } else {
832
            $this->mimeType[$type] = $val;
833
        }
834
    }
835
836
    /**
837
     * 当前的请求类型
838
     * @access public
839
     * @param  bool $origin 是否获取原始请求类型
840
     * @return string
841
     */
842
    public function method(bool $origin = false): string
843
    {
844
        if ($origin) {
845
            // 获取原始请求类型
846
            return $this->server('REQUEST_METHOD') ?: 'GET';
847
        } elseif (!$this->method) {
848
            if (isset($_POST[$this->config['var_method']])) {
849
                $method = strtolower($_POST[$this->config['var_method']]);
850
                if (in_array($method, ['get', 'post', 'put', 'patch', 'delete'])) {
851
                    $this->method    = strtoupper($method);
852
                    $this->{$method} = $_POST;
853
                } else {
854
                    $this->method = 'POST';
855
                }
856
                unset($_POST[$this->config['var_method']]);
857
            } elseif ($this->server('HTTP_X_HTTP_METHOD_OVERRIDE')) {
858
                $this->method = strtoupper($this->server('HTTP_X_HTTP_METHOD_OVERRIDE'));
859
            } else {
860
                $this->method = $this->server('REQUEST_METHOD') ?: 'GET';
861
            }
862
        }
863
864
        return $this->method;
865
    }
866
867
    /**
868
     * 是否为GET请求
869
     * @access public
870
     * @return bool
871
     */
872
    public function isGet(): bool
873
    {
874
        return $this->method() == 'GET';
875
    }
876
877
    /**
878
     * 是否为POST请求
879
     * @access public
880
     * @return bool
881
     */
882
    public function isPost(): bool
883
    {
884
        return $this->method() == 'POST';
885
    }
886
887
    /**
888
     * 是否为PUT请求
889
     * @access public
890
     * @return bool
891
     */
892
    public function isPut(): bool
893
    {
894
        return $this->method() == 'PUT';
895
    }
896
897
    /**
898
     * 是否为DELTE请求
899
     * @access public
900
     * @return bool
901
     */
902
    public function isDelete(): bool
903
    {
904
        return $this->method() == 'DELETE';
905
    }
906
907
    /**
908
     * 是否为HEAD请求
909
     * @access public
910
     * @return bool
911
     */
912
    public function isHead(): bool
913
    {
914
        return $this->method() == 'HEAD';
915
    }
916
917
    /**
918
     * 是否为PATCH请求
919
     * @access public
920
     * @return bool
921
     */
922
    public function isPatch(): bool
923
    {
924
        return $this->method() == 'PATCH';
925
    }
926
927
    /**
928
     * 是否为OPTIONS请求
929
     * @access public
930
     * @return bool
931
     */
932
    public function isOptions(): bool
933
    {
934
        return $this->method() == 'OPTIONS';
935
    }
936
937
    /**
938
     * 是否为cli
939
     * @access public
940
     * @return bool
941
     */
942
    public function isCli(): bool
943
    {
944
        return PHP_SAPI == 'cli';
945
    }
946
947
    /**
948
     * 是否为cgi
949
     * @access public
950
     * @return bool
951
     */
952
    public function isCgi(): bool
953
    {
954
        return strpos(PHP_SAPI, 'cgi') === 0;
955
    }
956
957
    /**
958
     * 获取当前请求的参数
959
     * @access public
960
     * @param  string|array $name 变量名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
961
     * @param  mixed        $default 默认值
962
     * @param  string|array $filter 过滤方法
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
963
     * @return mixed
964
     */
965
    public function param($name = '', $default = null, $filter = '')
966
    {
967
        if (empty($this->mergeParam)) {
968
            $method = $this->method(true);
969
970
            // 自动获取请求变量
971
            switch ($method) {
972
                case 'POST':
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
973
                    $vars = $this->post(false);
974
                    break;
975
                case 'PUT':
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
976
                case 'DELETE':
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
977
                case 'PATCH':
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
978
                    $vars = $this->put(false);
979
                    break;
980
                default:
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
981
                    $vars = [];
982
            }
983
984
            // 当前请求参数和URL地址中的参数合并
985
            $this->param = array_merge($this->param, $this->get(false), $vars, $this->route(false));
986
987
            $this->mergeParam = true;
988
        }
989
990
        if (is_array($name)) {
991
            return $this->only($name, $this->param, $filter);
992
        }
993
994
        return $this->input($this->param, $name, $default, $filter);
995
    }
996
997
    /**
998
     * 设置路由变量
999
     * @access public
1000
     * @param  array $route 路由变量
1001
     * @return $this
1002
     */
1003
    public function setRoute(array $route)
1004
    {
1005
        $this->route = array_merge($this->route, $route);
1006
        return $this;
1007
    }
1008
1009
    /**
1010
     * 获取路由参数
1011
     * @access public
1012
     * @param  mixed        $name 变量名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1013
     * @param  mixed        $default 默认值
1014
     * @param  string|array $filter 过滤方法
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
1015
     * @return mixed
1016
     */
1017
    public function route($name = '', $default = null, $filter = '')
1018
    {
1019
        if (is_array($name)) {
1020
            return $this->only($name, $this->route, $filter);
1021
        }
1022
1023
        return $this->input($this->route, $name, $default, $filter);
1024
    }
1025
1026
    /**
1027
     * 获取GET参数
1028
     * @access public
1029
     * @param  mixed        $name 变量名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1030
     * @param  mixed        $default 默认值
1031
     * @param  string|array $filter 过滤方法
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
1032
     * @return mixed
1033
     */
1034
    public function get($name = '', $default = null, $filter = '')
1035
    {
1036
        if (is_array($name)) {
1037
            return $this->only($name, $this->get, $filter);
1038
        }
1039
1040
        return $this->input($this->get, $name, $default, $filter);
1041
    }
1042
1043
    /**
1044
     * 获取中间件传递的参数
1045
     * @access public
1046
     * @param  mixed $name 变量名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1047
     * @param  mixed $default 默认值
1048
     * @return mixed
1049
     */
1050
    public function middleware($name, $default = null)
1051
    {
1052
        return $this->middleware[$name] ?? $default;
1053
    }
1054
1055
    /**
1056
     * 获取POST参数
1057
     * @access public
1058
     * @param  mixed        $name 变量名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1059
     * @param  mixed        $default 默认值
1060
     * @param  string|array $filter 过滤方法
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
1061
     * @return mixed
1062
     */
1063
    public function post($name = '', $default = null, $filter = '')
1064
    {
1065
        if (is_array($name)) {
1066
            return $this->only($name, $this->post, $filter);
1067
        }
1068
1069
        return $this->input($this->post, $name, $default, $filter);
1070
    }
1071
1072
    /**
1073
     * 获取PUT参数
1074
     * @access public
1075
     * @param  mixed        $name 变量名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1076
     * @param  mixed        $default 默认值
1077
     * @param  string|array $filter 过滤方法
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
1078
     * @return mixed
1079
     */
1080
    public function put($name = '', $default = null, $filter = '')
1081
    {
1082
        if (is_array($name)) {
1083
            return $this->only($name, $this->put, $filter);
1084
        }
1085
1086
        return $this->input($this->put, $name, $default, $filter);
1087
    }
1088
1089
    protected function getInputData($content)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
1090
    {
1091
        if ($this->isJson()) {
1092
            return (array) json_decode($content, true);
1093
        } elseif (strpos($content, '=')) {
1094
            parse_str($content, $data);
1095
            return $data;
1096
        }
1097
1098
        return [];
1099
    }
1100
1101
    /**
1102
     * 设置获取DELETE参数
1103
     * @access public
1104
     * @param  mixed        $name 变量名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1105
     * @param  mixed        $default 默认值
1106
     * @param  string|array $filter 过滤方法
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
1107
     * @return mixed
1108
     */
1109
    public function delete($name = '', $default = null, $filter = '')
1110
    {
1111
        return $this->put($name, $default, $filter);
1112
    }
1113
1114
    /**
1115
     * 设置获取PATCH参数
1116
     * @access public
1117
     * @param  mixed        $name 变量名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1118
     * @param  mixed        $default 默认值
1119
     * @param  string|array $filter 过滤方法
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
1120
     * @return mixed
1121
     */
1122
    public function patch($name = '', $default = null, $filter = '')
1123
    {
1124
        return $this->put($name, $default, $filter);
1125
    }
1126
1127
    /**
1128
     * 获取request变量
1129
     * @access public
1130
     * @param  mixed        $name 数据名称
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1131
     * @param  mixed        $default 默认值
1132
     * @param  string|array $filter 过滤方法
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
1133
     * @return mixed
1134
     */
1135
    public function request($name = '', $default = null, $filter = '')
1136
    {
1137
        if (is_array($name)) {
1138
            return $this->only($name, $this->request, $filter);
1139
        }
1140
1141
        return $this->input($this->request, $name, $default, $filter);
1142
    }
1143
1144
    /**
1145
     * 获取session数据
1146
     * @access public
1147
     * @param  string $name 数据名称
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1148
     * @param  string $default 默认值
1149
     * @return mixed
1150
     */
1151
    public function session(string $name = '', $default = null)
1152
    {
1153
        if ('' === $name) {
1154
            return $this->session;
1155
        }
1156
1157
        return $this->getData($this->session, $name, $default);
1158
    }
1159
1160
    /**
1161
     * 获取cookie参数
1162
     * @access public
1163
     * @param  mixed        $name 数据名称
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1164
     * @param  string       $default 默认值
1165
     * @param  string|array $filter 过滤方法
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
1166
     * @return mixed
1167
     */
1168
    public function cookie(string $name = '', $default = null, $filter = '')
1169
    {
1170
        if (!empty($name)) {
1171
            $data = $this->getData($this->cookie, $name, $default);
1172
        } else {
1173
            $data = $this->cookie;
1174
        }
1175
1176
        // 解析过滤器
1177
        $filter = $this->getFilter($filter, $default);
1178
1179
        if (is_array($data)) {
1180
            array_walk_recursive($data, [$this, 'filterValue'], $filter);
1181
            reset($data);
1182
        } else {
1183
            $this->filterValue($data, $name, $filter);
1184
        }
1185
1186
        return $data;
1187
    }
1188
1189
    /**
1190
     * 获取server参数
1191
     * @access public
1192
     * @param  string $name 数据名称
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1193
     * @param  string $default 默认值
1194
     * @return mixed
1195
     */
1196
    public function server(string $name = '', string $default = null)
1197
    {
1198
        if (empty($name)) {
1199
            return $this->server;
1200
        } else {
1201
            $name = strtoupper($name);
1202
        }
1203
1204
        return $this->server[$name] ?? $default;
1205
    }
1206
1207
    /**
1208
     * 获取环境变量
1209
     * @access public
1210
     * @param  string $name 数据名称
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1211
     * @param  string $default 默认值
1212
     * @return mixed
1213
     */
1214
    public function env(string $name = '', string $default = null)
1215
    {
1216
        if (empty($name)) {
1217
            return $this->env;
1218
        } else {
1219
            $name = strtoupper($name);
1220
        }
1221
1222
        return $this->env[$name] ?? $default;
1223
    }
1224
1225
    /**
1226
     * 获取上传的文件信息
1227
     * @access public
1228
     * @param  string $name 名称
1229
     * @return null|array|\think\File
1230
     */
1231
    public function file(string $name = '')
1232
    {
1233
        $files = $this->file;
1234
        if (!empty($files)) {
1235
1236
            if (strpos($name, '.')) {
1237
                list($name, $sub) = explode('.', $name);
1238
            }
1239
1240
            // 处理上传文件
1241
            $array = $this->dealUploadFile($files, $name);
1242
1243
            if ('' === $name) {
1244
                // 获取全部文件
1245
                return $array;
1246
            } elseif (isset($sub) && isset($array[$name][$sub])) {
1247
                return $array[$name][$sub];
1248
            } elseif (isset($array[$name])) {
1249
                return $array[$name];
1250
            }
1251
        }
1252
1253
        return;
1254
    }
1255
1256
    protected function dealUploadFile($files, $name)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
1257
    {
1258
        $array = [];
1259
        foreach ($files as $key => $file) {
1260
            if (is_array($file['name'])) {
1261
                $item  = [];
1262
                $keys  = array_keys($file);
1263
                $count = count($file['name']);
1264
1265
                for ($i = 0; $i < $count; $i++) {
1266
                    if ($file['error'][$i] > 0) {
1267
                        if ($name == $key) {
1268
                            $this->throwUploadFileError($file['error'][$i]);
1269
                        } else {
1270
                            continue;
1271
                        }
1272
                    }
1273
1274
                    $temp['key'] = $key;
1275
1276
                    foreach ($keys as $_key) {
1277
                        $temp[$_key] = $file[$_key][$i];
1278
                    }
1279
1280
                    $item[] = (new File($temp['tmp_name']))->setUploadInfo($temp);
1281
                }
1282
1283
                $array[$key] = $item;
1284
            } else {
1285
                if ($file instanceof File) {
1286
                    $array[$key] = $file;
1287
                } else {
1288
                    if ($file['error'] > 0) {
1289
                        if ($key == $name) {
1290
                            $this->throwUploadFileError($file['error']);
1291
                        } else {
1292
                            continue;
1293
                        }
1294
                    }
1295
1296
                    $array[$key] = (new File($file['tmp_name']))->setUploadInfo($file);
1297
                }
1298
            }
1299
        }
1300
1301
        return $array;
1302
    }
1303
1304
    protected function throwUploadFileError($error)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
1305
    {
1306
        static $fileUploadErrors = [
1307
            1 => 'upload File size exceeds the maximum value',
1308
            2 => 'upload File size exceeds the maximum value',
1309
            3 => 'only the portion of file is uploaded',
1310
            4 => 'no file to uploaded',
1311
            6 => 'upload temp dir not found',
1312
            7 => 'file write error',
1313
        ];
1314
1315
        $msg = $fileUploadErrors[$error];
1316
        throw new Exception($msg);
1317
    }
1318
1319
    /**
1320
     * 设置或者获取当前的Header
1321
     * @access public
1322
     * @param  string $name header名称
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1323
     * @param  string $default 默认值
1324
     * @return string|array
1325
     */
1326
    public function header(string $name = '', string $default = null)
1327
    {
1328
        if ('' === $name) {
1329
            return $this->header;
1330
        }
1331
1332
        $name = str_replace('_', '-', strtolower($name));
1333
1334
        return $this->header[$name] ?? $default;
1335
    }
1336
1337
    /**
1338
     * 获取变量 支持过滤和默认值
1339
     * @access public
1340
     * @param  array        $data 数据源
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1341
     * @param  string|false $name 字段名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1342
     * @param  mixed        $default 默认值
1343
     * @param  string|array $filter 过滤函数
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
1344
     * @return mixed
1345
     */
1346
    public function input(array $data = [], $name = '', $default = null, $filter = '')
1347
    {
1348
        if (false === $name) {
1349
            // 获取原始数据
1350
            return $data;
1351
        }
1352
1353
        $name = (string) $name;
1354
        if ('' != $name) {
1355
            // 解析name
1356
            if (strpos($name, '/')) {
1357
                list($name, $type) = explode('/', $name);
1358
            }
1359
1360
            $data = $this->getData($data, $name);
1361
1362
            if (is_null($data)) {
1363
                return $default;
1364
            }
1365
1366
            if (is_object($data)) {
1367
                return $data;
1368
            }
1369
        }
1370
1371
        return $this->filterData($data, $filter, $name, $default);
1372
    }
1373
1374
    protected function filterData($data, $filter, $name, $default)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
1375
    {
1376
        // 解析过滤器
1377
        $filter = $this->getFilter($filter, $default);
1378
1379
        if (is_array($data)) {
1380
            array_walk_recursive($data, [$this, 'filterValue'], $filter);
1381
            reset($data);
1382
        } else {
1383
            $this->filterValue($data, $name, $filter);
1384
        }
1385
1386
        return $data;
1387
    }
1388
1389
    /**
1390
     * 获取数据
1391
     * @access public
1392
     * @param  array  $data 数据源
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1393
     * @param  string $name 字段名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
1394
     * @param  mixed  $default 默认值
1395
     * @return mixed
1396
     */
1397
    protected function getData(array $data, string $name, $default = null)
1398
    {
1399
        foreach (explode('.', $name) as $val) {
1400
            if (isset($data[$val])) {
1401
                $data = $data[$val];
1402
            } else {
1403
                return $default;
1404
            }
1405
        }
1406
1407
        return $data;
1408
    }
1409
1410
    /**
1411
     * 设置或获取当前的过滤规则
1412
     * @access public
1413
     * @param  mixed $filter 过滤规则
1414
     * @return mixed
1415
     */
1416
    public function filter($filter = null)
1417
    {
1418
        if (is_null($filter)) {
1419
            return $this->filter;
1420
        }
1421
1422
        $this->filter = $filter;
1423
1424
        return $this;
1425
    }
1426
1427
    protected function getFilter($filter, $default)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
1428
    {
1429
        if (is_null($filter)) {
1430
            $filter = [];
1431
        } else {
1432
            $filter = $filter ?: $this->filter;
1433
            if (is_string($filter) && false === strpos($filter, '/')) {
1434
                $filter = explode(',', $filter);
1435
            } else {
1436
                $filter = (array) $filter;
1437
            }
1438
        }
1439
1440
        $filter[] = $default;
1441
1442
        return $filter;
1443
    }
1444
1445
    /**
1446
     * 递归过滤给定的值
1447
     * @access public
1448
     * @param  mixed $value 键值
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
1449
     * @param  mixed $key 键名
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
1450
     * @param  array $filters 过滤方法+默认值
1451
     * @return mixed
1452
     */
1453
    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...
1454
    {
1455
        $default = array_pop($filters);
1456
1457
        foreach ($filters as $filter) {
1458
            if (is_callable($filter)) {
1459
                // 调用函数或者方法过滤
1460
                $value = call_user_func($filter, $value);
1461
            } elseif (is_scalar($value)) {
1462
                if (false !== strpos($filter, '/')) {
1463
                    // 正则过滤
1464
                    if (!preg_match($filter, $value)) {
1465
                        // 匹配不成功返回默认值
1466
                        $value = $default;
1467
                        break;
1468
                    }
1469
                } elseif (!empty($filter)) {
1470
                    // filter函数不存在时, 则使用filter_var进行过滤
1471
                    // filter为非整形值时, 调用filter_id取得过滤id
1472
                    $value = filter_var($value, is_int($filter) ? $filter : filter_id($filter));
1473
                    if (false === $value) {
1474
                        $value = $default;
1475
                        break;
1476
                    }
1477
                }
1478
            }
1479
        }
1480
1481
        return $value;
1482
    }
1483
1484
    /**
1485
     * 是否存在某个请求参数
1486
     * @access public
1487
     * @param  string $name 变量名
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter name; 1 found
Loading history...
1488
     * @param  string $type 变量类型
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter name; 1 found
Loading history...
1489
     * @param  bool   $checkEmpty 是否检测空值
1490
     * @return bool
1491
     */
1492
    public function has(string $name, string $type = 'param', bool $checkEmpty = false): bool
1493
    {
1494
        $param = empty($this->$type) ? $this->$type() : $this->$type;
1495
1496
        // 按.拆分成多维数组进行判断
1497
        foreach (explode('.', $name) as $val) {
1498
            if (isset($param[$val])) {
1499
                $param = $param[$val];
1500
            } else {
1501
                return false;
1502
            }
1503
        }
1504
1505
        return ($checkEmpty && '' === $param) ? false : true;
1506
    }
1507
1508
    /**
1509
     * 获取指定的参数
1510
     * @access public
1511
     * @param  array        $name 变量名
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
1512
     * @param  mixed        $data 数据或者变量类型
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
1513
     * @param  string|array $filter 过滤方法
1514
     * @return array
1515
     */
1516
    public function only(array $name, $data = 'param', $filter = ''): array
1517
    {
1518
        $data = is_array($data) ? $data : $this->$data();
1519
1520
        $item = [];
1521
        foreach ($name as $key => $val) {
1522
1523
            if (is_int($key)) {
1524
                $default = null;
1525
                $key     = $val;
1526
                if (!isset($data[$key])) {
1527
                    continue;
1528
                }
1529
            } else {
1530
                $default = $val;
1531
            }
1532
1533
            $item[$key] = $this->filterData($data[$key] ?? $default, $filter, $key, $default);
1534
        }
1535
1536
        return $item;
1537
    }
1538
1539
    /**
1540
     * 排除指定参数获取
1541
     * @access public
1542
     * @param  array  $name 变量名
1543
     * @param  string $type 变量类型
1544
     * @return mixed
1545
     */
1546
    public function except(array $name, string $type = 'param'): array
1547
    {
1548
        $param = $this->$type();
1549
1550
        foreach ($name as $key) {
1551
            if (isset($param[$key])) {
1552
                unset($param[$key]);
1553
            }
1554
        }
1555
1556
        return $param;
1557
    }
1558
1559
    /**
1560
     * 当前是否ssl
1561
     * @access public
1562
     * @return bool
1563
     */
1564
    public function isSsl(): bool
1565
    {
1566
        if ($this->server('HTTPS') && ('1' == $this->server('HTTPS') || 'on' == strtolower($this->server('HTTPS')))) {
1567
            return true;
1568
        } elseif ('https' == $this->server('REQUEST_SCHEME')) {
1569
            return true;
1570
        } elseif ('443' == $this->server('SERVER_PORT')) {
1571
            return true;
1572
        } elseif ('https' == $this->server('HTTP_X_FORWARDED_PROTO')) {
1573
            return true;
1574
        } elseif ($this->config['https_agent_name'] && $this->server($this->config['https_agent_name'])) {
1575
            return true;
1576
        }
1577
1578
        return false;
1579
    }
1580
1581
    /**
1582
     * 当前是否JSON请求
1583
     * @access public
1584
     * @return bool
1585
     */
1586
    public function isJson(): bool
1587
    {
1588
        $contentType = $this->contentType();
1589
1590
        return false !== strpos($contentType, 'json');
1591
    }
1592
1593
    /**
1594
     * 当前是否Ajax请求
1595
     * @access public
1596
     * @param  bool $ajax true 获取原始ajax请求
1597
     * @return bool
1598
     */
1599
    public function isAjax(bool $ajax = false): bool
1600
    {
1601
        $value  = $this->server('HTTP_X_REQUESTED_WITH');
1602
        $result = $value && 'xmlhttprequest' == strtolower($value) ? true : false;
1603
1604
        if (true === $ajax) {
1605
            return $result;
1606
        }
1607
1608
        return $this->param($this->config['var_ajax']) ? true : $result;
1609
    }
1610
1611
    /**
1612
     * 当前是否Pjax请求
1613
     * @access public
1614
     * @param  bool $pjax true 获取原始pjax请求
1615
     * @return bool
1616
     */
1617
    public function isPjax(bool $pjax = false): bool
1618
    {
1619
        $result = !is_null($this->server('HTTP_X_PJAX')) ? true : false;
1620
1621
        if (true === $pjax) {
1622
            return $result;
1623
        }
1624
1625
        return $this->param($this->config['var_pjax']) ? true : $result;
1626
    }
1627
1628
    /**
1629
     * 获取客户端IP地址
1630
     * @access public
1631
     * @return string
1632
     */
1633
    public function ip(): string
1634
    {
1635
        if (!empty($this->realIP)) {
1636
            return $this->realIP;
1637
        }
1638
1639
        $this->realIP = $this->server('REMOTE_ADDR', '');
1640
1641
        // 如果指定了前端代理服务器IP以及其会发送的IP头
1642
        // 则尝试获取前端代理服务器发送过来的真实IP
1643
        $proxyIp       = $this->config('proxy_server_ip');
1644
        $proxyIpHeader = $this->config('proxy_server_ip_header');
1645
1646
        if (count($proxyIp) > 0 && count($proxyIpHeader) > 0) {
1647
            // 从指定的HTTP头中依次尝试获取IP地址
1648
            // 直到获取到一个合法的IP地址
1649
            foreach ($proxyIpHeader as $header) {
1650
                $tempIP = $this->server($header);
1651
1652
                if (empty($tempIP)) {
1653
                    continue;
1654
                }
1655
1656
                $tempIP = trim(explode(',', $tempIP)[0]);
0 ignored issues
show
Bug introduced by
It seems like $tempIP can also be of type array; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

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