Skip to content

Commit a9bfc0d

Browse files
committed
simplify response streams when using generators
1 parent 1bfad30 commit a9bfc0d

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/Illuminate/Routing/ResponseFactory.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Illuminate\Support\Js;
1313
use Illuminate\Support\Str;
1414
use Illuminate\Support\Traits\Macroable;
15+
use ReflectionFunction;
1516
use Symfony\Component\HttpFoundation\BinaryFileResponse;
1617
use Symfony\Component\HttpFoundation\StreamedJsonResponse;
1718
use Symfony\Component\HttpFoundation\StreamedResponse;
@@ -192,6 +193,16 @@ public function eventStream(Closure $callback, array $headers = [], StreamedEven
192193
*/
193194
public function stream($callback, $status = 200, array $headers = [])
194195
{
196+
if ((new ReflectionFunction($callback))->isGenerator()) {
197+
return new StreamedResponse(function () use ($callback) {
198+
foreach ($callback() as $chunk) {
199+
echo $chunk;
200+
ob_flush();
201+
flush();
202+
}
203+
}, $status, array_merge($headers, ['X-Accel-Buffering' => 'no']));
204+
}
205+
195206
return new StreamedResponse($callback, $status, $headers);
196207
}
197208

0 commit comments

Comments
 (0)