File tree Expand file tree Collapse file tree 7 files changed +132
-1
lines changed Expand file tree Collapse file tree 7 files changed +132
-1
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+ /**
5+ * This file is part of Hyperf.
6+ *
7+ * @link https://www.hyperf.io
8+ * @document https://hyperf.wiki
9+ 10+ * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+ */
12+ namespace Hyperf \HttpMessage \Server \Chunk ;
13+
14+ interface Chunkable
15+ {
16+ public function write (string $ data ): bool ;
17+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+ /**
5+ * This file is part of Hyperf.
6+ *
7+ * @link https://www.hyperf.io
8+ * @document https://hyperf.wiki
9+ 10+ * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+ */
12+ namespace Hyperf \HttpMessage \Server \Chunk ;
13+
14+ trait HasChunk
15+ {
16+ public function write (string $ content ): bool
17+ {
18+ if (isset ($ this ->connection ) && $ this ->connection instanceof Chunkable) {
19+ return $ this ->connection ->write ($ content );
20+ }
21+
22+ return false ;
23+ }
24+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+ /**
5+ * This file is part of Hyperf.
6+ *
7+ * @link https://www.hyperf.io
8+ * @document https://hyperf.wiki
9+ 10+ * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+ */
12+ namespace Hyperf \HttpMessage \Server \Connection ;
13+
14+ use Hyperf \HttpMessage \Server \Chunk \Chunkable ;
15+ use Hyperf \HttpMessage \Server \ConnectionInterface ;
16+ use Swoole \Http \Response ;
17+
18+ class SwooleConnection implements ConnectionInterface, Chunkable
19+ {
20+ /**
21+ * @var Response
22+ */
23+ protected $ response ;
24+
25+ public function __construct (Response $ response )
26+ {
27+ $ this ->response = $ response ;
28+ }
29+
30+ public function write (string $ data ): bool
31+ {
32+ return $ this ->response ->write ($ data );
33+ }
34+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+ /**
5+ * This file is part of Hyperf.
6+ *
7+ * @link https://www.hyperf.io
8+ * @document https://hyperf.wiki
9+ 10+ * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+ */
12+ namespace Hyperf \HttpMessage \Server ;
13+
14+ interface ConnectionInterface
15+ {
16+ }
Original file line number Diff line number Diff line change 1212namespace Hyperf \HttpMessage \Server ;
1313
1414use Hyperf \HttpMessage \Cookie \Cookie ;
15+ use Hyperf \HttpMessage \Server \Chunk \Chunkable ;
16+ use Hyperf \HttpMessage \Server \Chunk \HasChunk ;
1517use Hyperf \HttpMessage \Stream \SwooleStream ;
1618
17- class Response extends \Hyperf \HttpMessage \Base \Response
19+ class Response extends \Hyperf \HttpMessage \Base \Response implements Chunkable
1820{
21+ use HasChunk;
22+
1923 /**
2024 * @var array
2125 */
@@ -26,6 +30,11 @@ class Response extends \Hyperf\HttpMessage\Base\Response
2630 */
2731 protected $ trailers = [];
2832
33+ /**
34+ * @var null|ConnectionInterface
35+ */
36+ protected $ connection ;
37+
2938 /**
3039 * Returns an instance with body content.
3140 */
@@ -80,4 +89,15 @@ public function getTrailers(): array
8089 {
8190 return $ this ->trailers ;
8291 }
92+
93+ public function setConnection (ConnectionInterface $ connection )
94+ {
95+ $ this ->connection = $ connection ;
96+ return $ this ;
97+ }
98+
99+ public function getConnection (): ?ConnectionInterface
100+ {
101+ return $ this ->connection ;
102+ }
83103}
Original file line number Diff line number Diff line change @@ -40,6 +40,11 @@ public function testCookies()
4040 parent ::testCookies ();
4141 }
4242
43+ public function testWrite ()
44+ {
45+ $ this ->markTestSkipped ('Response proxy does not support chunk. ' );
46+ }
47+
4348 protected function newResponse ()
4449 {
4550 $ response = new ResponseStub ();
Original file line number Diff line number Diff line change 1212namespace HyperfTest \HttpMessage ;
1313
1414use Hyperf \HttpMessage \Cookie \Cookie ;
15+ use Hyperf \HttpMessage \Server \Connection \SwooleConnection ;
1516use Hyperf \HttpMessage \Server \Response ;
17+ use Mockery ;
1618use PHPUnit \Framework \TestCase ;
19+ use Swoole \Http \Response as SwooleResponse ;
1720
1821/**
1922 * @internal
@@ -52,6 +55,18 @@ public function testCookies()
5255 $ this ->assertSame (['hyperf.io ' => ['/ ' => ['test ' => $ cookie ]]], $ response ->getCookies ());
5356 }
5457
58+ public function testWrite ()
59+ {
60+ $ content = 'hello ' ;
61+ $ swooleResponse = Mockery::mock (SwooleResponse::class);
62+ $ swooleResponse ->shouldReceive ('write ' )->with ($ content )->once ()->andReturn (true );
63+
64+ $ response = $ this ->newResponse ();
65+ $ response ->setConnection (new SwooleConnection ($ swooleResponse ));
66+ $ status = $ response ->write ($ content );
67+ $ this ->assertTrue ($ status );
68+ }
69+
5570 protected function newResponse ()
5671 {
5772 return new Response ();
You can’t perform that action at this time.
0 commit comments