|
1
|
|
|
<?php |
|
2
|
|
|
// +---------------------------------------------------------------------- |
|
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
|
|
|
/** |
|
16
|
|
|
* 响应输出基础类 |
|
17
|
|
|
* @package think |
|
|
|
|
|
|
18
|
|
|
*/ |
|
19
|
|
|
abstract class Response |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* 原始数据 |
|
23
|
|
|
* @var mixed |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $data; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* 当前contentType |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $contentType = 'text/html'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* 字符集 |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $charset = 'utf-8'; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* 状态码 |
|
41
|
|
|
* @var integer |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $code = 200; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* 是否允许请求缓存 |
|
47
|
|
|
* @var bool |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $allowCache = true; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* 输出参数 |
|
53
|
|
|
* @var array |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $options = []; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* header参数 |
|
|
|
|
|
|
59
|
|
|
* @var array |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $header = []; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* 输出内容 |
|
65
|
|
|
* @var string |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $content = null; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Cookie对象 |
|
71
|
|
|
* @var Cookie |
|
72
|
|
|
*/ |
|
73
|
|
|
protected $cookie; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Session对象 |
|
77
|
|
|
* @var Session |
|
78
|
|
|
*/ |
|
79
|
|
|
protected $session; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* 架构函数 |
|
83
|
|
|
* @access protected |
|
84
|
|
|
* @param Cookie $cookie Cookie对象 |
|
85
|
|
|
* @param mixed $data 输出数据 |
|
|
|
|
|
|
86
|
|
|
* @param int $code 状态码 |
|
|
|
|
|
|
87
|
|
|
*/ |
|
88
|
|
|
protected function __construct(Cookie $cookie, $data = '', int $code = 200) |
|
89
|
|
|
{ |
|
90
|
|
|
$this->data($data); |
|
91
|
|
|
$this->code = $code; |
|
92
|
|
|
$this->cookie = $cookie; |
|
93
|
|
|
|
|
94
|
|
|
$this->contentType($this->contentType, $this->charset); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* 创建Response对象 |
|
99
|
|
|
* @access public |
|
100
|
|
|
* @param mixed $data 输出数据 |
|
101
|
|
|
* @param string $type 输出类型 |
|
102
|
|
|
* @param int $code 状态码 |
|
103
|
|
|
* @return Response |
|
104
|
|
|
*/ |
|
105
|
|
|
public static function create($data = '', string $type = 'html', int $code = 200): Response |
|
106
|
|
|
{ |
|
107
|
|
|
$class = false !== strpos($type, '\\') ? $type : '\\think\\response\\' . ucfirst(strtolower($type)); |
|
108
|
|
|
|
|
109
|
|
|
return Container::getInstance()->invokeClass($class, [$data, $code]); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* 设置Cookie对象 |
|
114
|
|
|
* @access public |
|
115
|
|
|
* @param Cookie $cookie Cookie对象 |
|
116
|
|
|
* @return $this |
|
117
|
|
|
*/ |
|
118
|
|
|
public function setCookie(Cookie $cookie) |
|
119
|
|
|
{ |
|
120
|
|
|
$this->cookie = $cookie; |
|
121
|
|
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* 设置Session对象 |
|
126
|
|
|
* @access public |
|
127
|
|
|
* @param Session $session Session对象 |
|
128
|
|
|
* @return $this |
|
129
|
|
|
*/ |
|
130
|
|
|
public function setSession(Session $session) |
|
131
|
|
|
{ |
|
132
|
|
|
$this->session = $session; |
|
133
|
|
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* 发送数据到客户端 |
|
138
|
|
|
* @access public |
|
139
|
|
|
* @return void |
|
140
|
|
|
* @throws \InvalidArgumentException |
|
141
|
|
|
*/ |
|
142
|
|
|
public function send(): void |
|
143
|
|
|
{ |
|
144
|
|
|
// 处理输出数据 |
|
145
|
|
|
$data = $this->getContent(); |
|
146
|
|
|
|
|
147
|
|
|
if (!headers_sent() && !empty($this->header)) { |
|
148
|
|
|
// 发送状态码 |
|
149
|
|
|
http_response_code($this->code); |
|
150
|
|
|
// 发送头部信息 |
|
151
|
|
|
foreach ($this->header as $name => $val) { |
|
152
|
|
|
header($name . (!is_null($val) ? ':' . $val : '')); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
$this->cookie->save(); |
|
157
|
|
|
|
|
158
|
|
|
$this->sendData($data); |
|
159
|
|
|
|
|
160
|
|
|
if (function_exists('fastcgi_finish_request')) { |
|
161
|
|
|
// 提高页面响应 |
|
162
|
|
|
fastcgi_finish_request(); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* 处理数据 |
|
168
|
|
|
* @access protected |
|
169
|
|
|
* @param mixed $data 要处理的数据 |
|
170
|
|
|
* @return mixed |
|
171
|
|
|
*/ |
|
172
|
|
|
protected function output($data) |
|
173
|
|
|
{ |
|
174
|
|
|
return $data; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* 输出数据 |
|
179
|
|
|
* @access protected |
|
180
|
|
|
* @param string $data 要处理的数据 |
|
181
|
|
|
* @return void |
|
182
|
|
|
*/ |
|
183
|
|
|
protected function sendData(string $data): void |
|
184
|
|
|
{ |
|
185
|
|
|
echo $data; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* 输出的参数 |
|
190
|
|
|
* @access public |
|
191
|
|
|
* @param mixed $options 输出参数 |
|
192
|
|
|
* @return $this |
|
193
|
|
|
*/ |
|
194
|
|
|
public function options(array $options = []) |
|
195
|
|
|
{ |
|
196
|
|
|
$this->options = array_merge($this->options, $options); |
|
197
|
|
|
|
|
198
|
|
|
return $this; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* 输出数据设置 |
|
203
|
|
|
* @access public |
|
204
|
|
|
* @param mixed $data 输出数据 |
|
205
|
|
|
* @return $this |
|
206
|
|
|
*/ |
|
207
|
|
|
public function data($data) |
|
208
|
|
|
{ |
|
209
|
|
|
$this->data = $data; |
|
210
|
|
|
|
|
211
|
|
|
return $this; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* 是否允许请求缓存 |
|
216
|
|
|
* @access public |
|
217
|
|
|
* @param bool $cache 允许请求缓存 |
|
218
|
|
|
* @return $this |
|
219
|
|
|
*/ |
|
220
|
|
|
public function allowCache(bool $cache) |
|
221
|
|
|
{ |
|
222
|
|
|
$this->allowCache = $cache; |
|
223
|
|
|
|
|
224
|
|
|
return $this; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* 是否允许请求缓存 |
|
229
|
|
|
* @access public |
|
230
|
|
|
* @return $this |
|
231
|
|
|
*/ |
|
232
|
|
|
public function isAllowCache() |
|
233
|
|
|
{ |
|
234
|
|
|
return $this->allowCache; |
|
|
|
|
|
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* 设置Cookie |
|
239
|
|
|
* @access public |
|
240
|
|
|
* @param string $name cookie名称 |
|
|
|
|
|
|
241
|
|
|
* @param string $value cookie值 |
|
|
|
|
|
|
242
|
|
|
* @param mixed $option 可选参数 |
|
243
|
|
|
* @return $this |
|
244
|
|
|
*/ |
|
245
|
|
|
public function cookie(string $name, string $value, $option = null) |
|
246
|
|
|
{ |
|
247
|
|
|
$this->cookie->set($name, $value, $option); |
|
248
|
|
|
|
|
249
|
|
|
return $this; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* 设置响应头 |
|
254
|
|
|
* @access public |
|
255
|
|
|
* @param array $header 参数 |
|
|
|
|
|
|
256
|
|
|
* @return $this |
|
257
|
|
|
*/ |
|
258
|
|
|
public function header(array $header = []) |
|
259
|
|
|
{ |
|
260
|
|
|
$this->header = array_merge($this->header, $header); |
|
261
|
|
|
|
|
262
|
|
|
return $this; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* 设置页面输出内容 |
|
267
|
|
|
* @access public |
|
268
|
|
|
* @param mixed $content |
|
|
|
|
|
|
269
|
|
|
* @return $this |
|
270
|
|
|
*/ |
|
271
|
|
|
public function content($content) |
|
272
|
|
|
{ |
|
273
|
|
|
if (null !== $content && !is_string($content) && !is_numeric($content) && !is_callable([ |
|
|
|
|
|
|
274
|
|
|
$content, |
|
275
|
|
|
'__toString', |
|
276
|
|
|
]) |
|
|
|
|
|
|
277
|
|
|
) { |
|
278
|
|
|
throw new \InvalidArgumentException(sprintf('variable type error: %s', gettype($content))); |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
$this->content = (string) $content; |
|
282
|
|
|
|
|
283
|
|
|
return $this; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* 发送HTTP状态 |
|
288
|
|
|
* @access public |
|
289
|
|
|
* @param integer $code 状态码 |
|
290
|
|
|
* @return $this |
|
291
|
|
|
*/ |
|
292
|
|
|
public function code(int $code) |
|
293
|
|
|
{ |
|
294
|
|
|
$this->code = $code; |
|
295
|
|
|
|
|
296
|
|
|
return $this; |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* LastModified |
|
301
|
|
|
* @access public |
|
302
|
|
|
* @param string $time |
|
|
|
|
|
|
303
|
|
|
* @return $this |
|
304
|
|
|
*/ |
|
305
|
|
|
public function lastModified(string $time) |
|
306
|
|
|
{ |
|
307
|
|
|
$this->header['Last-Modified'] = $time; |
|
308
|
|
|
|
|
309
|
|
|
return $this; |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* Expires |
|
314
|
|
|
* @access public |
|
315
|
|
|
* @param string $time |
|
|
|
|
|
|
316
|
|
|
* @return $this |
|
317
|
|
|
*/ |
|
318
|
|
|
public function expires(string $time) |
|
319
|
|
|
{ |
|
320
|
|
|
$this->header['Expires'] = $time; |
|
321
|
|
|
|
|
322
|
|
|
return $this; |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
/** |
|
326
|
|
|
* ETag |
|
327
|
|
|
* @access public |
|
328
|
|
|
* @param string $eTag |
|
|
|
|
|
|
329
|
|
|
* @return $this |
|
330
|
|
|
*/ |
|
331
|
|
|
public function eTag(string $eTag) |
|
332
|
|
|
{ |
|
333
|
|
|
$this->header['ETag'] = $eTag; |
|
334
|
|
|
|
|
335
|
|
|
return $this; |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
/** |
|
339
|
|
|
* 页面缓存控制 |
|
340
|
|
|
* @access public |
|
341
|
|
|
* @param string $cache 状态码 |
|
342
|
|
|
* @return $this |
|
343
|
|
|
*/ |
|
344
|
|
|
public function cacheControl(string $cache) |
|
345
|
|
|
{ |
|
346
|
|
|
$this->header['Cache-control'] = $cache; |
|
347
|
|
|
|
|
348
|
|
|
return $this; |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
/** |
|
352
|
|
|
* 页面输出类型 |
|
353
|
|
|
* @access public |
|
354
|
|
|
* @param string $contentType 输出类型 |
|
355
|
|
|
* @param string $charset 输出编码 |
|
356
|
|
|
* @return $this |
|
357
|
|
|
*/ |
|
358
|
|
|
public function contentType(string $contentType, string $charset = 'utf-8') |
|
359
|
|
|
{ |
|
360
|
|
|
$this->header['Content-Type'] = $contentType . '; charset=' . $charset; |
|
361
|
|
|
|
|
362
|
|
|
return $this; |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
|
/** |
|
366
|
|
|
* 获取头部信息 |
|
367
|
|
|
* @access public |
|
368
|
|
|
* @param string $name 头部名称 |
|
369
|
|
|
* @return mixed |
|
370
|
|
|
*/ |
|
371
|
|
|
public function getHeader(string $name = '') |
|
372
|
|
|
{ |
|
373
|
|
|
if (!empty($name)) { |
|
374
|
|
|
return $this->header[$name] ?? null; |
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
|
|
return $this->header; |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
|
|
/** |
|
381
|
|
|
* 获取原始数据 |
|
382
|
|
|
* @access public |
|
383
|
|
|
* @return mixed |
|
384
|
|
|
*/ |
|
385
|
|
|
public function getData() |
|
386
|
|
|
{ |
|
387
|
|
|
return $this->data; |
|
388
|
|
|
} |
|
389
|
|
|
|
|
390
|
|
|
/** |
|
391
|
|
|
* 获取输出数据 |
|
392
|
|
|
* @access public |
|
393
|
|
|
* @return string |
|
394
|
|
|
*/ |
|
395
|
|
|
public function getContent(): string |
|
396
|
|
|
{ |
|
397
|
|
|
if (null == $this->content) { |
|
398
|
|
|
$content = $this->output($this->data); |
|
399
|
|
|
|
|
400
|
|
|
if (null !== $content && !is_string($content) && !is_numeric($content) && !is_callable([ |
|
|
|
|
|
|
401
|
|
|
$content, |
|
402
|
|
|
'__toString', |
|
403
|
|
|
]) |
|
|
|
|
|
|
404
|
|
|
) { |
|
405
|
|
|
throw new \InvalidArgumentException(sprintf('variable type error: %s', gettype($content))); |
|
406
|
|
|
} |
|
407
|
|
|
|
|
408
|
|
|
$this->content = (string) $content; |
|
409
|
|
|
} |
|
410
|
|
|
|
|
411
|
|
|
return $this->content; |
|
412
|
|
|
} |
|
413
|
|
|
|
|
414
|
|
|
/** |
|
415
|
|
|
* 获取状态码 |
|
416
|
|
|
* @access public |
|
417
|
|
|
* @return integer |
|
418
|
|
|
*/ |
|
419
|
|
|
public function getCode(): int |
|
420
|
|
|
{ |
|
421
|
|
|
return $this->code; |
|
422
|
|
|
} |
|
423
|
|
|
} |
|
424
|
|
|
|