1212
1313namespace Hyperf \HttpMessage \Server ;
1414
15- use Hyperf \Contract \Arrayable ;
16- use Hyperf \Helper \JsonHelper ;
17- use Hyperf \Helper \StringHelper ;
1815use Hyperf \HttpMessage \Cookie \Cookie ;
1916use Hyperf \HttpMessage \Stream \SwooleStream ;
2017
2118class Response extends \Hyperf \HttpMessage \Base \Response
2219{
2320 /**
24- * @var null|\Throwable
25- */
26- protected $ exception ;
27-
28- /**
29- * @var \Swoole\Http\Response
21+ * @var null|\Swoole\Http\Response
3022 */
3123 protected $ swooleResponse ;
3224
@@ -36,114 +28,81 @@ class Response extends \Hyperf\HttpMessage\Base\Response
3628 protected $ cookies = [];
3729
3830 /**
39- * @param \Swoole\Http\Response $response
31+ * @param null| \Swoole\Http\Response $response
4032 */
41- public function __construct (\Swoole \Http \Response $ response )
33+ public function __construct (\Swoole \Http \Response $ response = null )
4234 {
4335 $ this ->swooleResponse = $ response ;
4436 }
4537
4638 /**
47- * Redirect to a URL.
48- *
49- * @param string $url
50- * @param null|int $status
51- * @return static
39+ * Handle response and send.
5240 */
53- public function redirect ( $ url , $ status = 302 )
41+ public function send ( )
5442 {
55- $ response = $ this ;
56- return $ response ->withAddedHeader ('Location ' , (string ) $ url )->withStatus ($ status );
43+ if (! $ this ->getSwooleResponse ()) {
44+ return ;
45+ }
46+
47+ $ this ->buildSwooleResponse ($ this ->swooleResponse , $ this );
48+
49+ $ this ->swooleResponse ->end ($ this ->getBody ()->getContents ());
5750 }
5851
5952 /**
60- * return a Raw format response.
61- *
62- * @param string $data The data
63- * @param int $status the HTTP status code
64- * @return \Hyperf\HttpMessage\Server\Response when $data not jsonable
53+ * Returns an instance with body content.
6554 */
66- public function raw (string $ data = '' , int $ status = 200 ): Response
55+ public function withContent (string $ content ): self
6756 {
68- $ response = $ this ;
69-
70- // Headers
71- $ response = $ response ->withoutHeader ('Content-Type ' )->withAddedHeader ('Content-Type ' , 'text/plain ' );
72- $ this ->getCharset () && $ response = $ response ->withCharset ($ this ->getCharset ());
73-
74- // Content
75- $ data && $ response = $ response ->withContent ($ data );
76-
77- // Status code
78- $ status && $ response = $ response ->withStatus ($ status );
79-
80- return $ response ;
57+ $ new = clone $ this ;
58+ $ new ->stream = new SwooleStream ($ content );
59+ return $ new ;
8160 }
8261
8362 /**
84- * return a Json format response.
85- *
86- * @param array|Arrayable $data The data
87- * @param int $status the HTTP status code
88- * @param int $encodingOptions Json encoding options
89- * @throws \InvalidArgumentException
90- * @return static when $data not jsonable
63+ * Return an instance with specified cookies.
9164 */
92- public function json ( $ data = [], int $ status = 200 , int $ encodingOptions = JSON_UNESCAPED_UNICODE ): Response
65+ public function withCookie ( Cookie $ cookie ): self
9366 {
94- $ response = $ this ;
95-
96- // Headers
97- $ response = $ response ->withoutHeader ('Content-Type ' )->withAddedHeader ('Content-Type ' , 'application/json ' );
98- $ this ->getCharset () && $ response = $ response ->withCharset ($ this ->getCharset ());
99-
100- // Content
101- if ($ data && ($ this ->isArrayable ($ data ) || is_string ($ data ))) {
102- is_string ($ data ) && $ data = ['data ' => $ data ];
103- $ content = JsonHelper::encode ($ data , $ encodingOptions );
104- $ response = $ response ->withContent ($ content );
105- } else {
106- $ response = $ response ->withContent ('{} ' );
107- }
67+ $ clone = clone $ this ;
68+ $ clone ->cookies [$ cookie ->getDomain ()][$ cookie ->getPath ()][$ cookie ->getName ()] = $ cookie ;
69+ return $ clone ;
70+ }
10871
109- // Status code
110- $ status && $ response = $ response ->withStatus ($ status );
72+ public function getSwooleResponse (): ?\Swoole \Http \Response
73+ {
74+ return $ this ->swooleResponse ;
75+ }
11176
112- return $ response ;
77+ public function setSwooleResponse (\Swoole \Http \Response $ swooleResponse ): self
78+ {
79+ $ this ->swooleResponse = $ swooleResponse ;
80+ return $ this ;
11381 }
11482
11583 /**
116- * 处理 Response 并发送数据.
84+ * Keep this method at public level,
85+ * allows the proxy class to override this method,
86+ * or override the method that used this method.
11787 */
118- public function send ()
88+ public function buildSwooleResponse ( \ Swoole \ Http \ Response $ swooleResponse , Response $ response ): void
11989 {
120- $ response = $ this ;
121-
12290 /*
12391 * Headers
12492 */
125- // Write Headers to swoole response
12693 foreach ($ response ->getHeaders () as $ key => $ value ) {
127- $ this -> swooleResponse ->header ($ key , implode ('; ' , $ value ));
94+ $ swooleResponse ->header ($ key , implode ('; ' , $ value ));
12895 }
12996
13097 /*
13198 * Cookies
13299 */
133- foreach ((array ) $ this ->cookies as $ domain => $ paths ) {
100+ foreach ((array )$ this ->cookies as $ domain => $ paths ) {
134101 foreach ($ paths ?? [] as $ path => $ item ) {
135102 foreach ($ item ?? [] as $ name => $ cookie ) {
136103 if ($ cookie instanceof Cookie) {
137104 $ value = $ cookie ->isRaw () ? $ cookie ->getValue () : rawurlencode ($ cookie ->getValue ());
138- $ this ->swooleResponse ->rawcookie (
139- $ cookie ->getName (),
140- $ value ,
141- $ cookie ->getExpiresTime (),
142- $ cookie ->getPath (),
143- $ cookie ->getDomain (),
144- $ cookie ->isSecure (),
145- $ cookie ->isHttpOnly ()
146- );
105+ $ swooleResponse ->rawcookie ($ cookie ->getName (), $ value , $ cookie ->getExpiresTime (), $ cookie ->getPath (), $ cookie ->getDomain (), $ cookie ->isSecure (), $ cookie ->isHttpOnly ());
147106 }
148107 }
149108 }
@@ -152,96 +111,6 @@ public function send()
152111 /*
153112 * Status code
154113 */
155- $ this ->swooleResponse ->status ($ response ->getStatusCode ());
156-
157- /*
158- * Body
159- */
160- $ this ->swooleResponse ->end ($ response ->getBody ()->getContents ());
161- }
162-
163- /**
164- * 设置Body内容,使用默认的Stream.
165- *
166- * @param string $content
167- * @return static
168- */
169- public function withContent ($ content ): Response
170- {
171- if ($ this ->stream ) {
172- return $ this ;
173- }
174-
175- $ new = clone $ this ;
176- $ new ->stream = new SwooleStream ($ content );
177- return $ new ;
178- }
179-
180- /**
181- * Return an instance with specified cookies.
182- *
183- * @param Cookie $cookie
184- * @return static
185- */
186- public function withCookie (Cookie $ cookie )
187- {
188- $ clone = clone $ this ;
189- $ clone ->cookies [$ cookie ->getDomain ()][$ cookie ->getPath ()][$ cookie ->getName ()] = $ cookie ;
190- return $ clone ;
191- }
192-
193- /**
194- * @return null|\Throwable
195- */
196- public function getException ()
197- {
198- return $ this ->exception ;
199- }
200-
201- /**
202- * @param \Throwable $exception
203- * @return $this
204- */
205- public function setException (\Throwable $ exception )
206- {
207- $ this ->exception = $ exception ;
208- return $ this ;
209- }
210-
211- /**
212- * @param mixed $value
213- * @return bool
214- */
215- public function isArrayable ($ value ): bool
216- {
217- return is_array ($ value ) || $ value instanceof Arrayable;
218- }
219-
220- /**
221- * @param string $accept
222- * @param string $keyword
223- * @return bool
224- */
225- public function isMatchAccept (string $ accept , string $ keyword ): bool
226- {
227- return StringHelper::contains ($ accept , $ keyword ) === true ;
228- }
229-
230- /**
231- * @return \Swoole\Http\Response
232- */
233- public function getSwooleResponse (): \Swoole \Http \Response
234- {
235- return $ this ->swooleResponse ;
236- }
237-
238- /**
239- * @param \Swoole\Http\Response $swooleResponse
240- * @return $this
241- */
242- public function setSwooleResponse (\Swoole \Http \Response $ swooleResponse )
243- {
244- $ this ->swooleResponse = $ swooleResponse ;
245- return $ this ;
114+ $ swooleResponse ->status ($ response ->getStatusCode ());
246115 }
247116}
0 commit comments