Completed
Push — 6.0 ( 487697...a3b118 )
by yun
08:06
created

Response::expires()   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
0 ignored issues
show
Coding Style introduced by
Package name "think" is not valid; consider "Think" instead
Loading history...
18
 */
19
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参数
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
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 public
84
     * @param  mixed $data    输出数据
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
85
     * @param  int   $code
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
86
     */
87
    public function __construct($data = '', int $code = 200)
88
    {
89
        $this->data($data);
90
        $this->code = $code;
91
92
        $this->contentType($this->contentType, $this->charset);
93
    }
94
95
    /**
96
     * 创建Response对象
97
     * @access public
98
     * @param  mixed  $data    输出数据
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
99
     * @param  string $type    输出类型
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
100
     * @param  int    $code
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
101
     * @return Response
102
     */
103
    public static function create($data = '', string $type = '', int $code = 200): Response
104
    {
105
        $class = false !== strpos($type, '\\') ? $type : '\\think\\response\\' . ucfirst(strtolower($type));
106
107
        if (class_exists($class)) {
108
            return Container::getInstance()->invokeClass($class, [$data, $code]);
109
        }
110
111
        return new static($data, $code);
112
    }
113
114
    /**
115
     * 设置Cookie对象
116
     * @access public
117
     * @param  Cookie $cookie Cookie对象
118
     * @return $this
119
     */
120
    public function setCookie(Cookie $cookie)
121
    {
122
        $this->cookie = $cookie;
123
        return $this;
124
    }
125
126
    /**
127
     * 设置Session对象
128
     * @access public
129
     * @param  Session $session Session对象
130
     * @return $this
131
     */
132
    public function setSession(Session $session)
133
    {
134
        $this->session = $session;
135
        return $this;
136
    }
137
138
    /**
139
     * 发送数据到客户端
140
     * @access public
141
     * @return void
142
     * @throws \InvalidArgumentException
143
     */
144
    public function send(): void
145
    {
146
        // 处理输出数据
147
        $data = $this->getContent();
148
149
        if (!headers_sent() && !empty($this->header)) {
150
            // 发送状态码
151
            http_response_code($this->code);
152
            // 发送头部信息
153
            foreach ($this->header as $name => $val) {
154
                header($name . (!is_null($val) ? ':' . $val : ''));
155
            }
156
        }
157
158
        $this->cookie->save();
159
160
        $this->sendData($data);
161
162
        if (function_exists('fastcgi_finish_request')) {
163
            // 提高页面响应
164
            fastcgi_finish_request();
165
        }
166
    }
167
168
    /**
169
     * 处理数据
170
     * @access protected
171
     * @param  mixed $data 要处理的数据
172
     * @return mixed
173
     */
174
    protected function output($data)
175
    {
176
        return $data;
177
    }
178
179
    /**
180
     * 输出数据
181
     * @access protected
182
     * @param string $data 要处理的数据
183
     * @return void
184
     */
185
    protected function sendData(string $data): void
186
    {
187
        echo $data;
188
    }
189
190
    /**
191
     * 输出的参数
192
     * @access public
193
     * @param  mixed $options 输出参数
194
     * @return $this
195
     */
196
    public function options(array $options = [])
197
    {
198
        $this->options = array_merge($this->options, $options);
199
200
        return $this;
201
    }
202
203
    /**
204
     * 输出数据设置
205
     * @access public
206
     * @param  mixed $data 输出数据
207
     * @return $this
208
     */
209
    public function data($data)
210
    {
211
        $this->data = $data;
212
213
        return $this;
214
    }
215
216
    /**
217
     * 是否允许请求缓存
218
     * @access public
219
     * @param  bool $cache 允许请求缓存
220
     * @return $this
221
     */
222
    public function allowCache(bool $cache)
223
    {
224
        $this->allowCache = $cache;
225
226
        return $this;
227
    }
228
229
    /**
230
     * 是否允许请求缓存
231
     * @access public
232
     * @return $this
233
     */
234
    public function isAllowCache()
235
    {
236
        return $this->allowCache;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->allowCache returns the type boolean which is incompatible with the documented return type think\Response.
Loading history...
237
    }
238
239
    /**
240
     * 设置响应头
241
     * @access public
242
     * @param  array $header  参数
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
243
     * @return $this
244
     */
245
    public function header(array $header = [])
246
    {
247
        $this->header = array_merge($this->header, $header);
248
249
        return $this;
250
    }
251
252
    /**
253
     * 设置页面输出内容
254
     * @access public
255
     * @param  mixed $content
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
256
     * @return $this
257
     */
258
    public function content($content)
259
    {
260
        if (null !== $content && !is_string($content) && !is_numeric($content) && !is_callable([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
261
            $content,
262
            '__toString',
263
        ])
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
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
    public function code(int $code)
280
    {
281
        $this->code = $code;
282
283
        return $this;
284
    }
285
286
    /**
287
     * LastModified
288
     * @access public
289
     * @param  string $time
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
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
    public function contentType(string $contentType, string $charset = 'utf-8')
346
    {
347
        $this->header['Content-Type'] = $contentType . '; charset=' . $charset;
348
349
        return $this;
350
    }
351
352
    /**
353
     * 获取头部信息
354
     * @access public
355
     * @param  string $name 头部名称
356
     * @return mixed
357
     */
358
    public function getHeader(string $name = '')
359
    {
360
        if (!empty($name)) {
361
            return $this->header[$name] ?? null;
362
        }
363
364
        return $this->header;
365
    }
366
367
    /**
368
     * 获取原始数据
369
     * @access public
370
     * @return mixed
371
     */
372
    public function getData()
373
    {
374
        return $this->data;
375
    }
376
377
    /**
378
     * 获取输出数据
379
     * @access public
380
     * @return string
381
     */
382
    public function getContent(): string
383
    {
384
        if (null == $this->content) {
385
            $content = $this->output($this->data);
386
387
            if (null !== $content && !is_string($content) && !is_numeric($content) && !is_callable([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
388
                $content,
389
                '__toString',
390
            ])
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
391
            ) {
392
                throw new \InvalidArgumentException(sprintf('variable type error: %s', gettype($content)));
393
            }
394
395
            $this->content = (string) $content;
396
        }
397
398
        return $this->content;
399
    }
400
401
    /**
402
     * 获取状态码
403
     * @access public
404
     * @return integer
405
     */
406
    public function getCode(): int
407
    {
408
        return $this->code;
409
    }
410
}
411