1
|
|
|
<?php |
2
|
|
|
namespace suda\framework\request; |
3
|
|
|
|
4
|
|
|
use suda\framework\http\Request; |
5
|
|
|
use suda\framework\http\UploadedFile; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* 请求包装器 |
9
|
|
|
* 包装PHP请求 |
10
|
|
|
*/ |
11
|
|
|
class RequestWrapper |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* HTTP请求 |
16
|
|
|
* |
17
|
|
|
* @var Request |
18
|
|
|
*/ |
19
|
|
|
protected $request; |
20
|
|
|
/** |
21
|
|
|
* 远程地址 |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $remoteAddr = '0.0.0.0'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* 获取本地HOST |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $host = '127.0.0.1'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* 获取本地端口 |
36
|
|
|
* |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
protected $port = 80; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* 是否为安全模式 |
43
|
|
|
* |
44
|
|
|
* @var boolean |
45
|
|
|
*/ |
46
|
|
|
protected $secure = false; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* 请求URI |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $uri = '/'; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* 请求参数 |
57
|
|
|
* |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
protected $method = 'GET'; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* 查询参数($_GET) |
64
|
|
|
* |
65
|
|
|
* @var array |
66
|
|
|
*/ |
67
|
|
|
protected $query = []; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* 请求索引 |
71
|
|
|
* |
72
|
|
|
* @var string |
73
|
|
|
*/ |
74
|
|
|
protected $index; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* URI基础部分 |
78
|
|
|
* |
79
|
|
|
* @var string |
80
|
|
|
*/ |
81
|
|
|
protected $uriBase; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* 服务器环境信息 |
85
|
|
|
* |
86
|
|
|
* @var array |
87
|
|
|
*/ |
88
|
|
|
protected $server; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* 创建请求包装器 |
92
|
|
|
* |
93
|
|
|
* @param Request $request |
94
|
|
|
*/ |
95
|
|
|
public function __construct(Request $request) |
96
|
|
|
{ |
97
|
|
|
$this->request = $request; |
98
|
|
|
(new Builder($request))->build($this); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get 远程地址 |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function getRemoteAddr() |
107
|
|
|
{ |
108
|
|
|
return $this->remoteAddr; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set 远程地址 |
113
|
|
|
* |
114
|
|
|
* @param string $remoteAddr 远程地址 |
115
|
|
|
* |
116
|
|
|
* @return $this |
117
|
|
|
*/ |
118
|
|
|
public function setRemoteAddr(string $remoteAddr) |
119
|
|
|
{ |
120
|
|
|
$this->remoteAddr = $remoteAddr; |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get 获取本地HOST |
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
public function getHost() |
131
|
|
|
{ |
132
|
|
|
return $this->host; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Set 获取本地HOST |
137
|
|
|
* |
138
|
|
|
* @param string $host 获取本地HOST |
139
|
|
|
* |
140
|
|
|
* @return $this |
141
|
|
|
*/ |
142
|
|
|
public function setHost(string $host) |
143
|
|
|
{ |
144
|
|
|
$this->host = $host; |
145
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get 获取本地端口 |
151
|
|
|
* |
152
|
|
|
* @return int |
153
|
|
|
*/ |
154
|
|
|
public function getPort() |
155
|
|
|
{ |
156
|
|
|
return $this->port; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Set 获取本地端口 |
161
|
|
|
* |
162
|
|
|
* @param int $port 获取本地端口 |
163
|
|
|
* |
164
|
|
|
* @return $this |
165
|
|
|
*/ |
166
|
|
|
public function setPort(int $port) |
167
|
|
|
{ |
168
|
|
|
$this->port = $port; |
169
|
|
|
|
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Get 是否为安全模式 |
175
|
|
|
* |
176
|
|
|
* @return bool |
177
|
|
|
*/ |
178
|
|
|
public function isSecure():bool |
179
|
|
|
{ |
180
|
|
|
return $this->secure; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Set 是否为安全模式 |
185
|
|
|
* |
186
|
|
|
* @param bool $secure 是否为安全模式 |
187
|
|
|
* @return $this |
188
|
|
|
*/ |
189
|
|
|
public function setSecure(bool $secure) |
190
|
|
|
{ |
191
|
|
|
$this->secure = $secure; |
192
|
|
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Get 请求URI |
197
|
|
|
* |
198
|
|
|
* @return string |
199
|
|
|
*/ |
200
|
|
|
public function getUri() |
201
|
|
|
{ |
202
|
|
|
return $this->uri; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Set 请求URI |
207
|
|
|
* |
208
|
|
|
* @param string $uri 请求URI |
209
|
|
|
* @return $this |
210
|
|
|
*/ |
211
|
|
|
public function setUri(string $uri) |
212
|
|
|
{ |
213
|
|
|
$this->uri = $uri; |
214
|
|
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Get 请求参数 |
219
|
|
|
* |
220
|
|
|
* @return string |
221
|
|
|
*/ |
222
|
|
|
public function getMethod() |
223
|
|
|
{ |
224
|
|
|
return $this->method; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Set 请求参数 |
229
|
|
|
* |
230
|
|
|
* @param string $method 请求参数 |
231
|
|
|
* @return $this |
232
|
|
|
*/ |
233
|
|
|
public function setMethod(string $method) |
234
|
|
|
{ |
235
|
|
|
$this->method = $method; |
236
|
|
|
return $this; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* 获取查询参数 |
241
|
|
|
* |
242
|
|
|
* @param string $name |
243
|
|
|
* @param mixed $default |
244
|
|
|
* @return mixed |
245
|
|
|
*/ |
246
|
|
|
public function getQuery(?string $name = null, $default = null) |
247
|
|
|
{ |
248
|
|
|
return $name === null ? $this->query:$this->query[$name] ?? $default; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* 设置查询参数 |
253
|
|
|
* |
254
|
|
|
* @param string $name |
255
|
|
|
* @param $query |
256
|
|
|
* @return $this |
257
|
|
|
*/ |
258
|
|
|
public function setQuery(string $name, $query) |
259
|
|
|
{ |
260
|
|
|
$this->query[$name] = $query; |
261
|
|
|
|
262
|
|
|
return $this; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* 设置查询参数 |
267
|
|
|
* |
268
|
|
|
* @param array $query |
269
|
|
|
* @return $this |
270
|
|
|
*/ |
271
|
|
|
public function setQueries(array $query) |
272
|
|
|
{ |
273
|
|
|
$this->query = $query; |
274
|
|
|
|
275
|
|
|
return $this; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* 合并参数 |
280
|
|
|
* |
281
|
|
|
* @param array $query |
282
|
|
|
* @return $this |
283
|
|
|
*/ |
284
|
|
|
public function mergeQueries(array $query) |
285
|
|
|
{ |
286
|
|
|
$this->query = array_merge($this->query, $query); |
287
|
|
|
return $this; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Get 请求索引 |
292
|
|
|
* |
293
|
|
|
* @return string |
294
|
|
|
*/ |
295
|
|
|
public function getIndex() |
296
|
|
|
{ |
297
|
|
|
return $this->index; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Set 请求索引 |
302
|
|
|
* |
303
|
|
|
* @param string $index 请求索引 |
304
|
|
|
* @return $this |
305
|
|
|
*/ |
306
|
|
|
public function setIndex(string $index) |
307
|
|
|
{ |
308
|
|
|
$this->index = $index; |
309
|
|
|
|
310
|
|
|
return $this; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* 获取文件 |
315
|
|
|
* |
316
|
|
|
* @param string|null $name |
317
|
|
|
* @return UploadedFile|null |
318
|
|
|
*/ |
319
|
|
|
public function getFile(string $name) |
320
|
|
|
{ |
321
|
|
|
return $this->request->files()[$name] ?? null; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* 获取请求头 |
326
|
|
|
* |
327
|
|
|
* @param string $name |
328
|
|
|
* @param mixed $default |
329
|
|
|
* @return mixed |
330
|
|
|
*/ |
331
|
|
|
public function getHeader(string $name, $default = null) |
332
|
|
|
{ |
333
|
|
|
if (array_key_exists(strtolower($name), $this->request->header())) { |
334
|
|
|
return $this->request->header()[$name]; |
335
|
|
|
} |
336
|
|
|
return $default; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* 判断请求头 |
341
|
|
|
* |
342
|
|
|
* @param string $name |
343
|
|
|
* @return boolean |
344
|
|
|
*/ |
345
|
|
|
public function hasHeader(string $name) |
346
|
|
|
{ |
347
|
|
|
return $this->getHeader($name) !== null; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Get 文件包装 |
352
|
|
|
* |
353
|
|
|
* @return UploadedFile[] |
354
|
|
|
*/ |
355
|
|
|
public function getFiles() |
356
|
|
|
{ |
357
|
|
|
return $this->request->files(); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Get 请求头部 |
362
|
|
|
* |
363
|
|
|
* @return string[] |
364
|
|
|
*/ |
365
|
|
|
public function getHeaders() |
366
|
|
|
{ |
367
|
|
|
return $this->request->header(); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* 获取Cookie |
372
|
|
|
* |
373
|
|
|
* @param string $name |
374
|
|
|
* @param mixed $default |
375
|
|
|
* @return mixed |
376
|
|
|
*/ |
377
|
|
|
public function getCookie(string $name = null, $default = null) |
378
|
|
|
{ |
379
|
|
|
if ($name === null) { |
380
|
|
|
return $this->request->cookies(); |
381
|
|
|
} |
382
|
|
|
return $this->request->cookies()[$name] ?? $default; |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* 获取服务器变量 |
387
|
|
|
* |
388
|
|
|
* @param string $name |
389
|
|
|
* @param mixed $default |
390
|
|
|
* @return mixed |
391
|
|
|
*/ |
392
|
|
|
public function getServer(string $name = null, $default = null) |
393
|
|
|
{ |
394
|
|
|
if ($name === null) { |
395
|
|
|
return $this->server; |
396
|
|
|
} |
397
|
|
|
return $this->server[$name] ?? $default; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Get URI基础部分 |
402
|
|
|
* @return string |
403
|
|
|
*/ |
404
|
|
|
public function getUriBase() |
405
|
|
|
{ |
406
|
|
|
return $this->uriBase; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* @param array $server |
411
|
|
|
*/ |
412
|
|
|
public function setServer(array $server): void |
413
|
|
|
{ |
414
|
|
|
$this->server = $server; |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* Set URI基础部分 |
419
|
|
|
* |
420
|
|
|
* @param string $uriBase URI基础部分 |
421
|
|
|
* @return $this |
422
|
|
|
*/ |
423
|
|
|
public function setUriBase(string $uriBase) |
424
|
|
|
{ |
425
|
|
|
$this->uriBase = $uriBase; |
426
|
|
|
return $this; |
427
|
|
|
} |
428
|
|
|
} |
429
|
|
|
|