Completed
Push — 6.0 ( fca71f...2ef662 )
by liu
07:18
created

Response::output()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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