Skip to content

[HttpFoundation] Support iterable of string in StreamedResponse #59154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Symfony/Component/HttpFoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.3
---

* Add support for iterable of string in `StreamedResponse`

7.2
---

Expand Down Expand Up @@ -40,7 +45,7 @@ CHANGELOG
* Add `UriSigner` from the HttpKernel component
* Add `partitioned` flag to `Cookie` (CHIPS Cookie)
* Add argument `bool $flush = true` to `Response::send()`
* Make `MongoDbSessionHandler` instantiable with the mongodb extension directly
* Make `MongoDbSessionHandler` instantiable with the mongodb extension directly

6.3
---
Expand Down
27 changes: 22 additions & 5 deletions src/Symfony/Component/HttpFoundation/StreamedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* StreamedResponse represents a streamed HTTP response.
*
* A StreamedResponse uses a callback for its content.
* A StreamedResponse uses a callback or an iterable of strings for its content.
*
* The callback should use the standard PHP functions like echo
* to stream the response back to the client. The flush() function
Expand All @@ -32,19 +32,36 @@ class StreamedResponse extends Response
private bool $headersSent = false;

/**
* @param int $status The HTTP status code (200 "OK" by default)
* @param callable|iterable<string>|null $callbackOrChunks
* @param int $status The HTTP status code (200 "OK" by default)
*/
public function __construct(?callable $callback = null, int $status = 200, array $headers = [])
public function __construct(callable|iterable|null $callbackOrChunks = null, int $status = 200, array $headers = [])
{
parent::__construct(null, $status, $headers);

if (null !== $callback) {
$this->setCallback($callback);
if (\is_callable($callbackOrChunks)) {
$this->setCallback($callbackOrChunks);
} elseif ($callbackOrChunks) {
$this->setChunks($callbackOrChunks);
}
$this->streamed = false;
$this->headersSent = false;
}

/**
* @param iterable<string> $chunks
*/
public function setChunks(iterable $chunks): static
{
$this->callback = static function () use ($chunks): void {
foreach ($chunks as $chunk) {
echo $chunk;
}
};

return $this;
}

/**
* Sets the PHP callback associated with this Response.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ public function testConstructor()
$this->assertEquals('text/plain', $response->headers->get('Content-Type'));
}

public function testConstructorWithChunks()
{
$chunks = ['foo', 'bar', 'baz'];
$callback = (new StreamedResponse($chunks))->getCallback();

ob_start();
$callback();

$this->assertSame('foobarbaz', ob_get_clean());
}

public function testPrepareWith11Protocol()
{
$response = new StreamedResponse(function () { echo 'foo'; });
Expand Down
Loading