Passed
Push — master ( c061fa...c1447f )
by 世昌
02:22
created

Response   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 171
rs 10
c 0
b 0
f 0
wmc 20

11 Methods

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