Passed
Push — master ( 185757...a51c77 )
by 世昌
02:03
created

Response   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 178
rs 10
c 0
b 0
f 0
wmc 17

11 Methods

Rating   Name   Duplication   Size   Complexity  
A instance() 0 6 2
A __construct() 0 4 1
A setType() 0 3 1
A proccess() 0 15 4
A getContent() 0 3 1
A send() 0 8 3
A getHeader() 0 3 1
A setStatus() 0 4 1
A getContext() 0 3 1
A setContext() 0 5 1
A setHeader() 0 3 1
1
<?php
2
namespace nebula\application\response;
3
4
use nebula\Context;
5
use nebula\component\request\Request;
6
use nebula\component\runnable\Runnable;
7
use nebula\application\response\provider\Header;
8
use nebula\application\response\provider\Status;
9
use nebula\application\response\provider\Content;
10
use nebula\application\response\provider\MimeType;
11
12
/**
13
 * 响应
14
 */
15
class Response
16
{
17
    /**
18
     * 静态实例
19
     *
20
     * @var self
21
     */
22
    protected static $instance;
23
24
25
    protected function __construct()
26
    {
27
        $this->content = new Content;
28
        $this->header = new Header;
29
    }
30
31
    /**
32
     * 返回实例
33
     *
34
     * @return self
35
     */
36
    public static function instance()
37
    {
38
        if (isset(static::$instance)) {
39
            return static::$instance;
40
        }
41
        return static::$instance = new static;
42
    }
43
44
    /**
45
     * 头部信息
46
     *
47
     * @var Header
48
     */
49
    protected $header;
50
51
    /**
52
     * 内容提供者
53
     *
54
     * @var Content
55
     */
56
    protected $content;
57
58
59
    /**
60
     * 环境
61
     *
62
     * @var Context
63
     */
64
    protected $context;
65
66
    /**
67
     * Get 头部信息
68
     *
69
     * @return  Header
70
     */
71
    public function getHeader()
72
    {
73
        return $this->header;
74
    }
75
76
    /**
77
     * 设置头部
78
     *
79
     * @param string $name
80
     * @param string $content
81
     * @return void
82
     */
83
    public function setHeader(string $name, string $content)
84
    {
85
        $this->header->addHeader($name, $content);
86
    }
87
88
    /**
89
     * Get 内容提供者
90
     *
91
     * @return  Content
92
     */
93
    public function getContent()
94
    {
95
        return $this->content;
96
    }
97
98
    /**
99
     * 发送请求头信息
100
     *
101
     * @param integer $code
102
     * @return void
103
     */
104
    public function setStatus(int $code)
105
    {
106
        $this->header->sendHeaderRaw('HTTP/1.1 '.$code.' '. Status::toText($code));
107
        $this->header->sendHeader('Status', $code.' '. Status::toText($code));
108
    }
109
110
    /**
111
     * 设置类型
112
     *
113
     * @param string $extension
114
     * @return void
115
     */
116
    public function setType(string $extension)
117
    {
118
        $this->header->sendHeader('Content-Type', MimeType::getMimeType($extension));
119
    }
120
121
    /**
122
     * 发送数据
123
     *
124
     * @param string $content
125
     * @return void
126
     */
127
    public function send(string $content)
128
    {
129
        $debug = defined('NEBULA_DBUG') && constant('NEBULA_DBUG') === true;
130
        if ($debug === false) {
131
            $this->setHeader('Content-Length', strlen($content));
132
        }
133
        $this->header->send();
134
        echo $content;
135
    }
136
137
    /**
138
     * 处理响应
139
     *
140
     * @param Runnable $runnable 可回调程序
141
     * @param boolean $buffer 是否实时输出
142
     * @return void
143
     */
144
145
    /**
146
     * 处理响应
147
     *
148
     * @param Runnable $runnable 可执行对象
149
     * @param Request $request 响应对象
150
     * @param boolean $buffer 缓存输出
151
     * @param boolean $allowEcho 实时输出
152
     * @return void
153
     */
154
    public function proccess(Runnable $runnable, Request $request, bool $buffer = true, bool $allowEcho = true)
155
    {
156
        $echo = '';
157
        if ($buffer) {
158
            \ob_start();
159
            $value = $runnable->run($request, $this);
160
            $echo = \ob_get_clean();
161
        } else {
162
            $value = $runnable->run($request, $this);
163
        }
164
        $this->header->send();
165
        if ($allowEcho && strlen($echo) > 0) {
166
            echo $echo;
167
        }
168
        $this->content->send($value);
169
    }
170
171
    /**
172
     * Get 环境
173
     *
174
     * @return  Context
175
     */ 
176
    public function getContext()
177
    {
178
        return $this->context;
179
    }
180
181
    /**
182
     * Set 环境
183
     *
184
     * @param  Context  $context  环境
185
     *
186
     * @return  self
187
     */ 
188
    public function setContext(Context $context)
189
    {
190
        $this->context = $context;
191
192
        return $this;
193
    }
194
}
195