Passed
Push — master ( ae009e...1bc377 )
by 世昌
02:06
created

Response::instance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
namespace nebula\application\response;
3
4
use nebula\application\response\provider\Header;
5
use nebula\application\response\provider\Status;
6
use nebula\application\response\provider\Content;
7
8
9
10
/**
11
 * 响应
12
 */
13
class Response
14
{
15
    /**
16
     * 静态实例
17
     *
18
     * @var self
19
     */
20
    protected static $instance;
21
22
23
    protected function __construct() {
24
        $this->content = new Content;
25
        $this->header = new Header;
26
    }
27
28
    /**
29
     * 返回实例
30
     *
31
     * @return self
32
     */
33
    public static function instance()
34
    {
35
        if (isset(static::$instance)) {
36
            return static::$instance;
37
        }
38
        return static::$instance = new static;
39
    }
40
41
    /**
42
     * 头部信息
43
     *
44
     * @var Header
45
     */
46
    protected $header;
47
48
    /**
49
     * 内容提供者
50
     *
51
     * @var Content
52
     */
53
    protected $content;
54
55
56
    /**
57
     * Get 头部信息
58
     *
59
     * @return  Header
60
     */ 
61
    public function getHeader()
62
    {
63
        return $this->header;
64
    }
65
66
    /**
67
     * 设置头部
68
     *
69
     * @param string $name
70
     * @param string $content
71
     * @return void
72
     */
73
    public function setHeader(string $name, string $content) {
74
        $this->header->addHeader($name, $content);
75
    }
76
77
    /**
78
     * Get 内容提供者
79
     *
80
     * @return  Content
81
     */ 
82
    public function getContent()
83
    {
84
        return $this->content;
85
    }
86
87
    /**
88
     * 发送请求头信息
89
     *
90
     * @param integer $code
91
     * @return void
92
     */
93
    public function setStatus(int $code) {
94
        $this->header->sendHeaderRaw('HTTP/1.1 '.$code.' '. Status::toText($code));
95
        $this->header->sendHeader('Status', $code.' '. Status::toText($code));
96
    }
97
98
    /**
99
     * 设置类型
100
     *
101
     * @param integer $extension
102
     * @return void
103
     */
104
    public function setType(int $extension) {
105
        $this->header->sendHeader('Content-Type', MimeType::getMimeType($extension));
0 ignored issues
show
Bug introduced by
The type nebula\application\response\MimeType was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
106
    }
107
108
    /**
109
     * 发送数据
110
     *
111
     * @param string $content
112
     * @return void
113
     */
114
    public function send(string $content) {
115
        $debug = defined('NEBULA_DBUG') && constant('NEBULA_DBUG') === true;
116
        if ($debug === false) {
117
            $this->setHeader('Content-Length', strlen($content));
118
        }
119
        $this->header->send();
120
        echo $content;
121
    }
122
}
123