Skip to content

Commit 6169a5b

Browse files
committed
support callable streaming responses
1 parent 9ba3fe7 commit 6169a5b

4 files changed

Lines changed: 34 additions & 16 deletions

File tree

‎system/HTTP/ResponseTrait.php‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace CodeIgniter\HTTP;
1515

16-
use Closure;
1716
use CodeIgniter\Config\Services;
1817
use CodeIgniter\Cookie\Cookie;
1918
use CodeIgniter\Cookie\CookieStore;
@@ -815,21 +814,21 @@ public function download(string $filename = '', $data = '', bool $setMime = fals
815814
* Creates a response that streams its body to the client as it is
816815
* generated, instead of buffering the complete body first.
817816
*
818-
* @param (Closure(StreamResponse): void)|iterable<string> $callbackOrChunks A callback that
819-
* streams output via write(), or an iterable of
820-
* string chunks to be written in order
817+
* @param (callable(StreamResponse): void)|iterable<string> $callbackOrChunks A callback that
818+
* streams output via write(), or an iterable of
819+
* string chunks to be written in order
821820
*/
822-
public function stream(Closure|iterable $callbackOrChunks): StreamResponse
821+
public function stream(callable|iterable $callbackOrChunks): StreamResponse
823822
{
824823
return new StreamResponse($callbackOrChunks);
825824
}
826825

827826
/**
828827
* Creates a response for streaming Server-Sent Events (SSE).
829828
*
830-
* @param Closure(SSEResponse): void $callback
829+
* @param callable(SSEResponse): void $callback
831830
*/
832-
public function eventStream(Closure $callback): SSEResponse
831+
public function eventStream(callable $callback): SSEResponse
833832
{
834833
return new SSEResponse($callback);
835834
}

‎system/HTTP/SSEResponse.php‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace CodeIgniter\HTTP;
1515

16-
use Closure;
1716
use JsonException;
1817

1918
/**
@@ -26,9 +25,9 @@ class SSEResponse extends StreamResponse
2625
/**
2726
* Constructor.
2827
*
29-
* @param Closure(SSEResponse): void $callback
28+
* @param callable(SSEResponse): void $callback
3029
*/
31-
public function __construct(Closure $callback)
30+
public function __construct(callable $callback)
3231
{
3332
parent::__construct($callback);
3433
}

‎system/HTTP/StreamResponse.php‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,18 @@ class StreamResponse extends Response implements NonBufferedResponseInterface
2929
private readonly Closure $callback;
3030

3131
/**
32-
* @param (Closure(static): void)|iterable<string> $callbackOrChunks A callback that
33-
* streams output via write(), or an iterable of
34-
* string chunks to be written in order
32+
* @param (callable(static): void)|iterable<string> $callbackOrChunks A callback that
33+
* streams output via write(), or an iterable of
34+
* string chunks to be written in order. A value
35+
* that is both callable and iterable is treated
36+
* as a callback.
3537
*/
36-
public function __construct(Closure|iterable $callbackOrChunks)
38+
public function __construct(callable|iterable $callbackOrChunks)
3739
{
3840
parent::__construct();
3941

40-
$this->callback = $callbackOrChunks instanceof Closure
41-
? $callbackOrChunks
42+
$this->callback = is_callable($callbackOrChunks)
43+
? $callbackOrChunks(...)
4244
: static function (self $response) use ($callbackOrChunks): void {
4345
foreach ($callbackOrChunks as $chunk) {
4446
if (! $response->write($chunk)) {

‎tests/system/HTTP/StreamResponseTest.php‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,24 @@ public function testStatusCodeIsSettable(): void
7878
$this->assertSame(206, $response->getStatusCode());
7979
}
8080

81+
public function testConstructorTreatsCallableIterableAsCallable(): void
82+
{
83+
// An array callable is both callable and iterable; callable must win.
84+
$response = new StreamResponse([$this, 'writeHello']);
85+
$response->pretend();
86+
87+
ob_start();
88+
$response->send();
89+
$output = ob_get_clean();
90+
91+
$this->assertSame('Hello', $output);
92+
}
93+
94+
public function writeHello(StreamResponse $stream): void
95+
{
96+
$stream->write('Hello');
97+
}
98+
8199
public function testStreamFactoryReturnsStreamResponse(): void
82100
{
83101
$response = (new Response())->stream(static function (): void {

0 commit comments

Comments
 (0)