Completed
Push — 6.0 ( 1e69d2...64aa8b )
by yun
02:11
created

Response::cacheControl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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