Skip to content

Commit d635333

Browse files
authored
custom header callback handler (#130)
1 parent 3416fa0 commit d635333

File tree

5 files changed

+84
-9
lines changed

5 files changed

+84
-9
lines changed

src/Builder.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class Builder implements Responsable
2525

2626
protected int $bytesSent = 0;
2727

28+
protected bool $sendHeaders = true;
29+
2830
protected Collection $meta;
2931

3032
protected Queue $queue;
@@ -137,6 +139,8 @@ public function saveTo($output): int
137139
default => throw new InvalidArgumentException('Invalid output provided'),
138140
};
139141

142+
$this->sendHeaders = false;
143+
140144
return $this->process();
141145
}
142146

@@ -151,8 +155,8 @@ public function process(): int
151155

152156
if ($this->canPredictZipSize()) {
153157
$size = $zip->finish();
154-
header('Content-Length: '.$size);
155-
header('X-Accel-Buffering: no');
158+
$this->header('Content-Length: '.$size);
159+
$this->header('X-Accel-Buffering: no');
156160

157161
event(new ZipStreaming($this, $zip, $size));
158162

@@ -214,6 +218,8 @@ public function getFinalSize(): int
214218

215219
public function response(): StreamedResponse
216220
{
221+
$this->sendHeaders = true;
222+
217223
return new StreamedResponse(function () {
218224
$this->process();
219225
}, 200);
@@ -230,8 +236,9 @@ protected function prepare(): ZipStream
230236
operationMode: $this->canPredictZipSize() ? OperationMode::SIMULATE_STRICT : OperationMode::NORMAL,
231237
comment: $this->getComment(),
232238
outputStream: $this->getOutputStream(),
239+
httpHeaderCallback: $this->header(...),
233240
outputName: $this->getOutputName(),
234-
flushOutput: true,
241+
flushOutput: true
235242
);
236243

237244
$this->queue->each->prepare($zip);
@@ -251,4 +258,13 @@ protected function getOutputStream(): StreamInterface
251258

252259
return $this->outputStream;
253260
}
261+
262+
protected function header(string $header): static
263+
{
264+
if($this->sendHeaders) {
265+
header($header);
266+
}
267+
268+
return $this;
269+
}
254270
}

tests/ConflictTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22

3-
namespace STS\ZipStream\Tests;
4-
53
use GuzzleHttp\Psr7\BufferStream;
64
use Illuminate\Support\Str;
75
use STS\ZipStream\Builder;

tests/FileTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22

3-
namespace STS\ZipStream\Tests;
4-
53
use Orchestra\Testbench\TestCase;
64
use STS\ZipStream\Models\File;
75
use STS\ZipStream\Models\HttpFile;

tests/StreamingTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
use Orchestra\Testbench\TestCase;
4+
use STS\ZipStream\Facades\Zip;
5+
use STS\ZipStream\ZipStreamServiceProvider;
6+
use Symfony\Component\HttpFoundation\StreamedResponse;
7+
8+
class StreamingTest extends TestCase
9+
{
10+
protected function getPackageProviders($app)
11+
{
12+
return [ZipStreamServiceProvider::class];
13+
}
14+
15+
protected function getPackageAliases($app)
16+
{
17+
return [
18+
'Zip' => Zip::class
19+
];
20+
}
21+
22+
protected function defineRoutes($router)
23+
{
24+
$router->get('/stream', function () {
25+
$testrun = microtime();
26+
file_put_contents("/tmp/test1.txt", "this is the first test file for test run $testrun");
27+
28+
return Zip::create("test.zip")
29+
->add("/tmp/test1.txt")
30+
->then(fn() => unlink("/tmp/test1.txt"));
31+
});
32+
33+
$router->get('/save', function () {
34+
$testrun = microtime();
35+
file_put_contents("/tmp/test1.txt", "this is the first test file for test run $testrun");
36+
37+
$dir = "/tmp/" . Str::random();
38+
39+
Zip::create("test.zip")
40+
->add("/tmp/test1.txt")
41+
->saveTo($dir);
42+
43+
return "$dir/test.zip";
44+
});
45+
}
46+
47+
public function testZipStream()
48+
{
49+
$response = $this->get('/stream');
50+
51+
// All we really care about is that the response is a StreamedResponse
52+
$this->assertInstanceOf(StreamedResponse::class, $response->baseResponse);
53+
}
54+
55+
public function testZipSaveToDiskOnly()
56+
{
57+
$response = $this->get('/save');
58+
59+
// Make sure we did NOT get a stream response this time, and that the zip exists
60+
$this->assertNotInstanceOf(StreamedResponse::class, $response->baseResponse);
61+
$this->assertTrue(file_exists($response->getContent()));
62+
63+
unlink($response->getContent());
64+
}
65+
}

tests/ZipTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22

3-
namespace STS\ZipStream\Tests;
4-
53
use GuzzleHttp\Psr7\BufferStream;
64
use Illuminate\Support\Str;
75
use STS\ZipStream\Builder;

0 commit comments

Comments
 (0)