Completed
Push — 6.0 ( af00ec...037a27 )
by liu
02:49
created

Response::content()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 6
nc 2
nop 1
dl 0
loc 13
ccs 0
cts 7
cp 0
crap 30
rs 9.6111
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
class Response
1 ignored issue
show
Coding Style introduced by
Missing doc comment for class Response
Loading history...
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参数
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
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    输出数据
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
81
     * @param  int   $code
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
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    输出数据
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
95
     * @param  string $type    输出类型
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
96
     * @param  int    $code
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
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 要处理的数据
1 ignored issue
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
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;
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...
233
    }
234
235
    /**
236
     * 设置响应头
237
     * @access public
238
     * @param  array $header  参数
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
252
     * @return $this
253
     */
254
    public function content($content)
255
    {
256
        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...
257
            $content,
258
            '__toString',
259
        ])
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...
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
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
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([
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...
384
                $content,
385
                '__toString',
386
            ])
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...
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