Skip to content

Commit 8ae29d3

Browse files
committed
Merge branch 'master' into 3.0-merge
# Conflicts: # src/http-message/src/Server/Response.php # src/metric/src/Adapter/Prometheus/MetricFactory.php
2 parents 247d687 + d6a8e48 commit 8ae29d3

File tree

7 files changed

+123
-1
lines changed

7 files changed

+123
-1
lines changed

src/Server/Chunk/Chunkable.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
* @contact [email protected]
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+
}

src/Server/Chunk/HasChunk.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
* @contact [email protected]
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+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
* @contact [email protected]
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+
public function __construct(protected Response $response)
21+
{
22+
}
23+
24+
public function write(string $data): bool
25+
{
26+
return $this->response->write($data);
27+
}
28+
}

src/Server/ConnectionInterface.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace Hyperf\HttpMessage\Server;
13+
14+
interface ConnectionInterface
15+
{
16+
}

src/Server/Response.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,20 @@
1212
namespace Hyperf\HttpMessage\Server;
1313

1414
use Hyperf\HttpMessage\Cookie\Cookie;
15+
use Hyperf\HttpMessage\Server\Chunk\Chunkable;
16+
use Hyperf\HttpMessage\Server\Chunk\HasChunk;
1517
use 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
protected array $cookies = [];
2024

2125
protected array $trailers = [];
2226

27+
protected ?ConnectionInterface $connection = null;
28+
2329
/**
2430
* Returns an instance with body content.
2531
*/
@@ -74,4 +80,15 @@ public function getTrailers(): array
7480
{
7581
return $this->trailers;
7682
}
83+
84+
public function setConnection(ConnectionInterface $connection)
85+
{
86+
$this->connection = $connection;
87+
return $this;
88+
}
89+
90+
public function getConnection(): ?ConnectionInterface
91+
{
92+
return $this->connection;
93+
}
7794
}

tests/ResponseProxyTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff 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();

tests/ResponseTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
namespace HyperfTest\HttpMessage;
1313

1414
use Hyperf\HttpMessage\Cookie\Cookie;
15+
use Hyperf\HttpMessage\Server\Connection\SwooleConnection;
1516
use Hyperf\HttpMessage\Server\Response;
17+
use Mockery;
1618
use 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();

0 commit comments

Comments
 (0)