Passed
Push — master ( c1447f...6911a9 )
by 世昌
01:47
created

Response::render()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 9
nc 6
nop 3
dl 0
loc 12
rs 9.2222
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 = Header::instance();
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
        if (!$this->getContext()->isDebug()) {
111
            $this->setHeader('Content-Length', strlen($content));
112
        }
113
        $this->header->send();
114
        echo $content;
115
    }
116
117
    /**
118
     * 处理响应
119
     *
120
     * @param Runnable $runnable 可执行对象
121
     * @param Request $request 响应对象
122
     * @param boolean $buffer 缓存输出
123
     * @param boolean $allowEcho 实时输出
124
     * @return void
125
     */
126
    public function proccess(Runnable $runnable, Request $request, bool $buffer = true, bool $allowEcho = true)
0 ignored issues
show
Unused Code introduced by
The parameter $allowEcho is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

126
    public function proccess(Runnable $runnable, Request $request, bool $buffer = true, /** @scrutinizer ignore-unused */ bool $allowEcho = true)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
127
    {
128
        list($echo, $value) = $this->getRenderInfo($runnable, $request, $buffer);
129
        $this->render($value, $echo);
130
    }
131
132
    /**
133
     * 渲染输出数据
134
     *
135
     * @param [type] $value
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
136
     * @param string $echo
137
     * @param boolean $allowEcho
138
     * @return void
139
     */
140
    public function render($value, string $echo = '', bool $allowEcho = true)
141
    {
142
        $this->header->send();
143
        $wantEcho = strlen($echo) > 0 ;
144
        $noReturn = $wantEcho && is_null($value);
145
        if ($noReturn && $wantEcho) {
146
            $this->send($echo);
147
        } else {
148
            if ($allowEcho && $wantEcho) {
149
                echo $echo;
150
            }
151
            Content::send($this, $value);
152
        }
153
    }
154
155
    /**
156
     * 获取渲染信息
157
     *
158
     * @param Runnable $runnable
159
     * @param Request $request
160
     * @param boolean $buffer
161
     * @return array
162
     */
163
    private function getRenderInfo(Runnable $runnable, Request $request, bool $buffer = true)
164
    {
165
        $echo = '';
166
        $value = null;
167
        if ($buffer) {
168
            \ob_start();
169
            $value = $runnable->run($request, $this);
170
            $echo = \ob_get_clean();
171
        } else {
172
            $value = $runnable->run($request, $this);
173
        }
174
        return [$echo, $value];
175
    }
176
177
    /**
178
     * Get 环境
179
     *
180
     * @return  Context
181
     */
182
    public function getContext()
183
    {
184
        return $this->context;
185
    }
186
187
    /**
188
     * Set 环境
189
     *
190
     * @param  Context  $context  环境
191
     *
192
     * @return  self
193
     */
194
    public function setContext(Context $context)
195
    {
196
        $this->context = $context;
197
198
        return $this;
199
    }
200
}
201