Passed
Push — master ( a42169...1c163d )
by 世昌
02:12
created

Response::proccess()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 4
nop 4
dl 0
loc 15
rs 9.9
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->content = new Content;
0 ignored issues
show
Bug Best Practice introduced by
The property content does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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 Context
55
     */
56
    protected $context;
57
58
    /**
59
     * Get 头部信息
60
     *
61
     * @return  Header
62
     */
63
    public function getHeader()
64
    {
65
        return $this->header;
66
    }
67
68
    /**
69
     * 设置头部
70
     *
71
     * @param string $name
72
     * @param string $content
73
     * @return void
74
     */
75
    public function setHeader(string $name, string $content)
76
    {
77
        $this->header->addHeader($name, $content);
78
    }
79
80
    /**
81
     * 发送请求头信息
82
     *
83
     * @param integer $code
84
     * @return void
85
     */
86
    public function setStatus(int $code)
87
    {
88
        $this->header->sendHeaderRaw('HTTP/1.1 '.$code.' '. Status::toText($code));
89
        $this->header->sendHeader('Status', $code.' '. Status::toText($code));
90
    }
91
92
    /**
93
     * 设置类型
94
     *
95
     * @param string $extension
96
     * @return void
97
     */
98
    public function setType(string $extension)
99
    {
100
        $this->header->sendHeader('Content-Type', MimeType::getMimeType($extension));
101
    }
102
103
    /**
104
     * 发送数据
105
     *
106
     * @param string $content
107
     * @return void
108
     */
109
    public function send(string $content)
110
    {
111
        $debug = defined('NEBULA_DBUG') && constant('NEBULA_DBUG') === true;
112
        if ($debug === false) {
113
            $this->setHeader('Content-Length', strlen($content));
114
        }
115
        $this->header->send();
116
        echo $content;
117
    }
118
119
    /**
120
     * 处理响应
121
     *
122
     * @param Runnable $runnable 可执行对象
123
     * @param Request $request 响应对象
124
     * @param boolean $buffer 缓存输出
125
     * @param boolean $allowEcho 实时输出
126
     * @return void
127
     */
128
    public function proccess(Runnable $runnable, Request $request, bool $buffer = true, bool $allowEcho = true)
129
    {
130
        $echo = '';
131
        if ($buffer) {
132
            \ob_start();
133
            $value = $runnable->run($request, $this);
134
            $echo = \ob_get_clean();
135
        } else {
136
            $value = $runnable->run($request, $this);
137
        }
138
        $this->header->send();
139
        if ($allowEcho && strlen($echo) > 0) {
140
            echo $echo;
141
        }
142
        Content::send($this, $value);
143
    }
144
145
    /**
146
     * Get 环境
147
     *
148
     * @return  Context
149
     */
150
    public function getContext()
151
    {
152
        return $this->context;
153
    }
154
155
    /**
156
     * Set 环境
157
     *
158
     * @param  Context  $context  环境
159
     *
160
     * @return  self
161
     */
162
    public function setContext(Context $context)
163
    {
164
        $this->context = $context;
165
166
        return $this;
167
    }
168
}
169