Passed
Push — master ( b45126...8af3f1 )
by 世昌
03:18
created

Response::getStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace suda\framework;
3
4
use Exception;
5
use suda\framework\http\Cookie;
6
use suda\framework\http\Stream;
7
use suda\framework\http\stream\DataStream;
8
use suda\framework\response\MimeType;
9
use suda\framework\response\ContentWrapper;
10
use suda\framework\response\ResponseWrapper;
11
use suda\framework\http\Response as ResponseInterface;
12
13
class Response extends ResponseWrapper
14
{
15
    /**
16
     * 包装器
17
     *
18
     * @var ContentWrapper
19
     */
20
    protected $wrapper;
21
22
    /**
23
     * 响应数据
24
     *
25
     * @var Stream|string
26
     */
27
    protected $data;
28
29
30
    public function __construct(ResponseInterface $response)
31
    {
32
        parent::__construct($response);
33
        $this->wrapper = new ContentWrapper;
34
    }
35
36
    /**
37
     * 设置类型
38
     *
39
     * @param string $extension
40
     * @return $this
41
     */
42
    public function setType(string $extension)
43
    {
44
        $this->header('content-type', MimeType::getMimeType($extension), true);
45
        return $this;
46
    }
47
48
    /**
49
     * 设置头部
50
     *
51
     * @param string $name
52
     * @param string $content
53
     * @param boolean $replace
54
     * @return $this
55
     */
56
    public function setHeader(string $name, string $content, bool $replace = false)
57
    {
58
        $this->header($name, $content, $replace);
59
        return $this;
60
    }
61
62
    /**
63
     * 设置请求内容
64
     *
65
     * @param mixed $content
66
     * @return $this
67
     */
68
    public function setContent($content)
69
    {
70
        if (is_string($content) || $content instanceof Stream) {
71
            $this->data = $content;
72
        } else {
73
            $wrapper = $this->wrapper->getWrapper($content);
74
            $this->data = $wrapper->getWrappedContent($this);
75
        }
76
        return $this;
77
    }
78
79
    /**
80
     * 发送文件内容
81
     *
82
     * @param string $filename
83
     * @param integer $offset
84
     * @param integer $length
85
     * @return void
86
     * @throws Exception
87
     */
88
    public function sendFile(string $filename, int $offset = 0, int $length = null)
89
    {
90
        $data = new DataStream($filename, $offset, $length);
91
        $this->setHeader('content-length', $data->length());
92
        $this->response->sendFile($filename, $offset, $length);
93
    }
94
95
    /**
96
     * 发送缓存的内容
97
     */
98
    public function end()
99
    {
100
        $this->sendContent($this->data);
101
    }
102
103
    /**
104
     * 发送内容数据
105
     *
106
     * @param array|string|Stream|null $data
107
     * @return $this
108
     */
109
    protected function sendContentLength($data)
110
    {
111
        if ($data === null) {
112
            $this->setHeader('content-length', 0);
113
        } elseif (is_array($data)) {
114
            $this->setHeader('content-length', $this->getDataLengthArray($data), true);
115
        } else {
116
            $this->setHeader('content-length', $this->getDataLengthItem($data), true);
117
        }
118
        return $this;
119
    }
120
121
    /**
122
     * 获取数据长度
123
     *
124
     * @param Stream[] $data
125
     * @return int
126
     */
127
    protected function getDataLengthArray(array $data):int
128
    {
129
        $length = 0;
130
        foreach ($data as $item) {
131
            $length += $this->getDataLengthItem($item);
132
        }
133
        return $length;
134
    }
135
136
    /**
137
     * 获取数据长度
138
     *
139
     * @param Stream|string $data
140
     * @return integer
141
     */
142
    protected function getDataLengthItem($data):int
143
    {
144
        if (is_string($data)) {
145
            return strlen($data);
146
        } else {
147
            return $data->length();
148
        }
149
    }
150
151
    /**
152
     * 设置 Cookie
153
     *
154
     * @param string $name
155
     * @param string $value
156
     * @param integer $expire
157
     * @return Cookie
158
     */
159
    public function setCookie(string $name, string $value, int $expire = 0):Cookie
160
    {
161
        $cookie = new Cookie($name, $value, $expire);
162
        $this->cookie($cookie);
163
        return $cookie;
164
    }
165
166
    /**
167
     * 直接发送数据
168
     *
169
     * @param mixed $content
170
     * @return void
171
     */
172
    public function sendContent($content = null)
173
    {
174
        if ($content !== null) {
175
            $this->setContent($content);
176
        }
177
        $this->sendContentLength($this->data);
178
        $this->send($this->data);
179
    }
180
181
    /**
182
     * @return ContentWrapper
183
     */
184
    public function getWrapper(): ContentWrapper
185
    {
186
        return $this->wrapper;
187
    }
188
189
    /**
190
     * @param ContentWrapper $wrapper
191
     */
192
    public function setWrapper(ContentWrapper $wrapper)
193
    {
194
        $this->wrapper = $wrapper;
195
    }
196
}
197