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