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