-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.php
More file actions
187 lines (153 loc) · 4.04 KB
/
Request.php
File metadata and controls
187 lines (153 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
namespace Numst;
use Numst\Kernel\Request as HttpRequest;
use Numst\Traits\InteractsContent;
use Numst\Support\Arr;
class Request extends HttpRequest{
use InteractsContent;
protected $json;
/**
* The user resolver callback.
*
* @var \Closure
*/
protected $userResolver;
/**
* The route resolver callback.
*
* @var \Closure
*/
protected $routeResolver;
public function __construct(){
}
public static function capture(){
return HttpRequest::createFromGlobals();
}
public function instance(){
return $this;
}
/**
* 检测是否是AJAX请求
*/
public function ajax(){
return $this->isXmlHttpRequest();
}
public function method(){
return $this->getMethod();
}
public function root(){
return rtrim($this->getSchemeAndHttpHost().$this->getBaseUrl(),'/');
}
/**
* 获取url
*/
public function url(){
return rtrim(preg_replace('/\?.*/','',$this->getUri()));
}
/**
* 获取path
*/
public function path(){
$pattern = trim($this->getPathInfo(),'/');
return $pattern == '' ?'/':$pattern;
}
public function pjax(){
return $this->headers->get('X-PJAX') == true;
}
public function secure(){
return $this->isSecure();
}
public function ip(){
return $this->getClientIp();
}
public function ips(){
return $this->getClientIps();
}
public function userAgent(){
return $this->headers->get('User-Agent');
}
/**
* 合并请求参数
*/
public function merge(array $input){
$this->getInputSource()->add($input);
return $this;
}
/**
* 替换参数
*/
public function replace(array $input) {
$this->getInputSource()->replace($input);
return $this;
}
public function get($key, $default = null){
return parent::get($key,$default);
}
/**
* 获取输入参数
*/
public function getInputSource(){
if ($this->isJson()) {
return $this->json();
}
return in_array($this->getRealMethod(),['GET','HEAD']) ? $this->query : $this->request;
}
/**
* 验证session并获取
*/
public function session(){
if (!$this->hasSession()){
throw new \RuntimeException('Session store not set on request');
}
return $this->session;
}
/**
* 获取session
*/
public function getSession(){
return $this->session;
}
public function route($param = null, $default = null) {
$route = call_user_func($this->getRouteResolver());
if (is_null($route) || is_null($param)) {
return $route;
}
return $route->parameter($param, $default);
}
public function getRouteResolver(){
return $this->routeResolver ?: function(){
//
};
}
public function toArray(){
return $this->all();
}
/**
* 从请求中获取json数据
*/
public function json($key = null, $default = null){
if (!isset($this->json)) {
$this->json = new ParameterBag((array) json_decode($this->getContent(),true));
}
if (is_null($key)) {
return $this->json;
}
return $this->data_get($this->json->all(),$key,$default);
}
public function data_get($target,$key,$default = null){
if (is_null($key)) {
return $target;
}
$key = is_array($key) ? $key : explode('.',$key);
while(!is_null($segment = array_shift($key))) {
if (Arr::accessible($value) && Arr::exists($target,$segment)) {
$target = $target[$segment];
} else if (is_object($target) && isset($target->{$segment})) {
$target = $target->{$segment};
} else {
return $default instanceof \Closure ? $default() : $default;
}
}
return $target;
}
}