Passed
Push — 6.0 ( 3dc798...7ca19f )
by liu
02:48
created

Response::isAllowCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
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
use think\response\Redirect as RedirectResponse;
16
17
class Response
1 ignored issue
show
Coding Style introduced by
Missing doc comment for class Response
Loading history...
18
{
19
    /**
20
     * 原始数据
21
     * @var mixed
22
     */
23
    protected $data;
24
25
    /**
26
     * 当前contentType
27
     * @var string
28
     */
29
    protected $contentType = 'text/html';
30
31
    /**
32
     * 字符集
33
     * @var string
34
     */
35
    protected $charset = 'utf-8';
36
37
    /**
38
     * 状态码
39
     * @var integer
40
     */
41
    protected $code = 200;
42
43
    /**
44
     * 是否允许请求缓存
45
     * @var bool
46
     */
47
    protected $allowCache = true;
48
49
    /**
50
     * 输出参数
51
     * @var array
52
     */
53
    protected $options = [];
54
55
    /**
56
     * header参数
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
57
     * @var array
58
     */
59
    protected $header = [];
60
61
    /**
62
     * 输出内容
63
     * @var string
64
     */
65
    protected $content = null;
66
67
    /**
68
     * Cookie对象
69
     * @var Cookie
70
     */
71
    protected $cookie;
72
73
    /**
74
     * Session对象
75
     * @var Session
76
     */
77
    protected $session;
78
79
    /**
80
     * 架构函数
81
     * @access public
82
     * @param  mixed $data    输出数据
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
83
     * @param  int   $code
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
84
     */
85
    public function __construct($data = '', int $code = 200)
86
    {
87
        $this->data($data);
88
        $this->code = $code;
89
90
        $this->contentType($this->contentType, $this->charset);
91
    }
92
93
    /**
94
     * 创建Response对象
95
     * @access public
96
     * @param  mixed  $data    输出数据
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
97
     * @param  string $type    输出类型
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
98
     * @param  int    $code
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
99
     * @return Response
100
     */
101
    public static function create($data = '', string $type = '', int $code = 200): Response
102
    {
103
        $class = false !== strpos($type, '\\') ? $type : '\\think\\response\\' . ucfirst(strtolower($type));
104
105
        if (class_exists($class)) {
106
            return Container::getInstance()->invokeClass($class, [$data, $code]);
107
        }
108
109
        return new static($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
        if (!($this instanceof RedirectResponse)) {
167
            $this->session->flush();
168
        }
169
    }
170
171
    /**
172
     * 处理数据
173
     * @access protected
174
     * @param  mixed $data 要处理的数据
175
     * @return mixed
176
     */
177
    protected function output($data)
178
    {
179
        return $data;
180
    }
181
182
    /**
183
     * 输出数据
184
     * @access protected
185
     * @param string $data 要处理的数据
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
186
     * @return void
187
     */
188
    protected function sendData(string $data): void
189
    {
190
        echo $data;
191
    }
192
193
    /**
194
     * 输出的参数
195
     * @access public
196
     * @param  mixed $options 输出参数
197
     * @return $this
198
     */
199
    public function options(array $options = [])
200
    {
201
        $this->options = array_merge($this->options, $options);
202
203
        return $this;
204
    }
205
206
    /**
207
     * 输出数据设置
208
     * @access public
209
     * @param  mixed $data 输出数据
210
     * @return $this
211
     */
212
    public function data($data)
213
    {
214
        $this->data = $data;
215
216
        return $this;
217
    }
218
219
    /**
220
     * 是否允许请求缓存
221
     * @access public
222
     * @param  bool $cache 允许请求缓存
223
     * @return $this
224
     */
225
    public function allowCache(bool $cache)
226
    {
227
        $this->allowCache = $cache;
228
229
        return $this;
230
    }
231
232
    /**
233
     * 是否允许请求缓存
234
     * @access public
235
     * @return $this
236
     */
237
    public function isAllowCache()
238
    {
239
        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...
240
    }
241
242
    /**
243
     * 设置响应头
244
     * @access public
245
     * @param  array $header  参数
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
246
     * @return $this
247
     */
248
    public function header(array $header = [])
249
    {
250
        $this->header = array_merge($this->header, $header);
251
252
        return $this;
253
    }
254
255
    /**
256
     * 设置页面输出内容
257
     * @access public
258
     * @param  mixed $content
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
259
     * @return $this
260
     */
261
    public function content($content)
262
    {
263
        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...
264
            $content,
265
            '__toString',
266
        ])
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...
267
        ) {
268
            throw new \InvalidArgumentException(sprintf('variable type error: %s', gettype($content)));
269
        }
270
271
        $this->content = (string) $content;
272
273
        return $this;
274
    }
275
276
    /**
277
     * 发送HTTP状态
278
     * @access public
279
     * @param  integer $code 状态码
280
     * @return $this
281
     */
282
    public function code(int $code)
283
    {
284
        $this->code = $code;
285
286
        return $this;
287
    }
288
289
    /**
290
     * LastModified
291
     * @access public
292
     * @param  string $time
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
293
     * @return $this
294
     */
295
    public function lastModified(string $time)
296
    {
297
        $this->header['Last-Modified'] = $time;
298
299
        return $this;
300
    }
301
302
    /**
303
     * Expires
304
     * @access public
305
     * @param  string $time
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
306
     * @return $this
307
     */
308
    public function expires(string $time)
309
    {
310
        $this->header['Expires'] = $time;
311
312
        return $this;
313
    }
314
315
    /**
316
     * ETag
317
     * @access public
318
     * @param  string $eTag
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
319
     * @return $this
320
     */
321
    public function eTag(string $eTag)
322
    {
323
        $this->header['ETag'] = $eTag;
324
325
        return $this;
326
    }
327
328
    /**
329
     * 页面缓存控制
330
     * @access public
331
     * @param  string $cache 状态码
332
     * @return $this
333
     */
334
    public function cacheControl(string $cache)
335
    {
336
        $this->header['Cache-control'] = $cache;
337
338
        return $this;
339
    }
340
341
    /**
342
     * 页面输出类型
343
     * @access public
344
     * @param  string $contentType 输出类型
345
     * @param  string $charset     输出编码
346
     * @return $this
347
     */
348
    public function contentType(string $contentType, string $charset = 'utf-8')
349
    {
350
        $this->header['Content-Type'] = $contentType . '; charset=' . $charset;
351
352
        return $this;
353
    }
354
355
    /**
356
     * 获取头部信息
357
     * @access public
358
     * @param  string $name 头部名称
359
     * @return mixed
360
     */
361
    public function getHeader(string $name = '')
362
    {
363
        if (!empty($name)) {
364
            return $this->header[$name] ?? null;
365
        }
366
367
        return $this->header;
368
    }
369
370
    /**
371
     * 获取原始数据
372
     * @access public
373
     * @return mixed
374
     */
375
    public function getData()
376
    {
377
        return $this->data;
378
    }
379
380
    /**
381
     * 获取输出数据
382
     * @access public
383
     * @return string
384
     */
385
    public function getContent(): string
386
    {
387
        if (null == $this->content) {
388
            $content = $this->output($this->data);
389
390
            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...
391
                $content,
392
                '__toString',
393
            ])
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...
394
            ) {
395
                throw new \InvalidArgumentException(sprintf('variable type error: %s', gettype($content)));
396
            }
397
398
            $this->content = (string) $content;
399
        }
400
401
        return $this->content;
402
    }
403
404
    /**
405
     * 获取状态码
406
     * @access public
407
     * @return integer
408
     */
409
    public function getCode(): int
410
    {
411
        return $this->code;
412
    }
413
}
414