Passed
Push — master ( 8b0ed1...c061fa )
by 世昌
02:48
created

Response::proccess()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 12
nop 4
dl 0
loc 22
rs 8.8333
c 0
b 0
f 0
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
        $echo = '';
130
        $value = null;
131
132
        if ($buffer) {
133
            \ob_start();
134
            $value = $runnable->run($request, $this);
135
            $echo = \ob_get_clean();
136
        } else {
137
            $value = $runnable->run($request, $this);
138
        }
139
        $this->header->send();
140
        $wantEcho = strlen($echo) > 0 ;
141
        $noReturn = $wantEcho && is_null($value);
142
        if ($noReturn && $wantEcho) {
143
            $this->send($echo);
144
        } else {
145
            if ($allowEcho &&  $wantEcho) {
146
                echo $echo;
147
            }
148
            Content::send($this, $value);
149
        }
150
    }
151
152
    /**
153
     * Get 环境
154
     *
155
     * @return  Context
156
     */
157
    public function getContext()
158
    {
159
        return $this->context;
160
    }
161
162
    /**
163
     * Set 环境
164
     *
165
     * @param  Context  $context  环境
166
     *
167
     * @return  self
168
     */
169
    public function setContext(Context $context)
170
    {
171
        $this->context = $context;
172
173
        return $this;
174
    }
175
}
176