Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b1a7fbf

Browse files
[HttpFoundation] Add StreamedResponse::createFromTempFile()
1 parent 4e16f7b commit b1a7fbf

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

src/Symfony/Component/HttpFoundation/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* Make `HeaderBag::getDate()`, `Response::getDate()`, `getExpires()` and `getLastModified()` return a `DateTimeImmutable`
88
* Support root-level `Generator` in `StreamedJsonResponse`
99
* Add `UriSigner` from the HttpKernel component
10+
* Add `StreamedResponse::createFromTempFile()`
1011

1112
6.3
1213
---

src/Symfony/Component/HttpFoundation/StreamedResponse.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class StreamedResponse extends Response
2929
protected $callback;
3030
protected $streamed;
3131
private bool $headersSent;
32+
private static int $chunkSize = 8 * 1024;
3233

3334
/**
3435
* @param int $status The HTTP status code (200 "OK" by default)
@@ -124,4 +125,29 @@ public function getContent(): string|false
124125
{
125126
return false;
126127
}
128+
129+
public static function createFromTempFile(\SplTempFileObject $file, int $status = 200, array $headers = [], int $chunkSize = null): static
130+
{
131+
if (null !== $chunkSize && 1 > $chunkSize) {
132+
throw new \InvalidArgumentException(sprintf('Chunk size of streamed response from a temporary file must be at least 1, got %d.', $chunkSize));
133+
}
134+
135+
return new self(static function () use ($file, $chunkSize) {
136+
$out = fopen('php://output', 'w');
137+
138+
ignore_user_abort(true);
139+
140+
$file->rewind();
141+
while (!$file->eof()) {
142+
fwrite($out, $file->fread($chunkSize ?? static::$chunkSize));
143+
flush();
144+
145+
if (connection_aborted()) {
146+
break;
147+
}
148+
}
149+
150+
fclose($out);
151+
}, $status, $headers);
152+
}
127153
}

src/Symfony/Component/HttpFoundation/Tests/StreamedResponseTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,29 @@ public function testSendInformationalResponse()
135135

136136
$response->sendHeaders();
137137
}
138+
139+
public function testCreateFromTemporaryFileInvalidChunkSize()
140+
{
141+
$this->expectException(\InvalidArgumentException::class);
142+
$this->expectExceptionMessage('Chunk size of streamed response from a temporary file must be at least 1, got -10.');
143+
StreamedResponse::createFromTempFile(new \SplTempFileObject(), chunkSize: -10);
144+
}
145+
146+
public function testCreateFromTemporaryFile()
147+
{
148+
$file = new \SplTempFileObject();
149+
$file->fwrite('foo,bar');
150+
151+
$response = StreamedResponse::createFromTempFile($file, 201, [
152+
'Content-Type' => 'text/csv',
153+
]);
154+
155+
$this->assertSame(201, $response->getStatusCode());
156+
$this->assertSame('text/csv', $response->headers->get('Content-Type'));
157+
158+
ob_start();
159+
$response->sendContent();
160+
$string = ob_get_clean();
161+
$this->assertSame('foo,bar', $string);
162+
}
138163
}

0 commit comments

Comments
 (0)