Skip to content

Commit 48a1637

Browse files
authored
Add ability to set instance of StreamInterface as output stream (#172)
* Add ability to set instance of StreamInterface as output stream PSR-7's StreamInterface ZipStream\Option\Archive::outputStream * Format some code to run CI trigger
1 parent a1882eb commit 48a1637

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

src/Option/Archive.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
namespace ZipStream\Option;
55

6+
use Psr\Http\Message\StreamInterface;
7+
68
final class Archive
79
{
810
const DEFAULT_DEFLATE_LEVEL = 6;
@@ -104,7 +106,7 @@ final class Archive
104106
private $deflateLevel = 6;
105107

106108
/**
107-
* @var resource
109+
* @var StreamInterface|resource
108110
*/
109111
private $outputStream;
110112

@@ -228,15 +230,15 @@ public function setContentType(string $contentType): void
228230
}
229231

230232
/**
231-
* @return resource
233+
* @return StreamInterface|resource
232234
*/
233235
public function getOutputStream()
234236
{
235237
return $this->outputStream;
236238
}
237239

238240
/**
239-
* @param resource $outputStream
241+
* @param StreamInterface|resource $outputStream
240242
*/
241243
public function setOutputStream($outputStream): void
242244
{

src/ZipStream.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,13 @@ public function send(string $str): void
459459
}
460460
$this->need_headers = false;
461461

462-
fwrite($this->opt->getOutputStream(), $str);
462+
$outputStream = $this->opt->getOutputStream();
463+
464+
if ($outputStream instanceof StreamInterface) {
465+
$outputStream->write($str);
466+
} else {
467+
fwrite($outputStream, $str);
468+
}
463469

464470
if ($this->opt->isFlushOutput()) {
465471
// flush output buffer if it is on and flushable

test/ZipStreamTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use ZipStream\Option\Archive as ArchiveOptions;
1111
use ZipStream\Option\File as FileOptions;
1212
use ZipStream\Option\Method;
13+
use ZipStream\Stream;
1314
use ZipStream\ZipStream;
1415

1516
/**
@@ -504,6 +505,33 @@ public function testAddFileFromPsr7Stream(): void
504505
$this->assertStringEqualsFile($tmpDir . '/sample.json', $body);
505506
}
506507

508+
public function testAddFileFromPsr7StreamWithOutputToPsr7Stream(): void
509+
{
510+
[$tmp, $resource] = $this->getTmpFileStream();
511+
$psr7OutputStream = new Stream($resource);
512+
513+
$options = new ArchiveOptions();
514+
$options->setOutputStream($psr7OutputStream);
515+
516+
$zip = new ZipStream(null, $options);
517+
518+
$body = 'Sample String Data';
519+
$response = new Response(200, [], $body);
520+
521+
$fileOptions = new FileOptions();
522+
$fileOptions->setMethod(Method::STORE());
523+
524+
$zip->addFileFromPsr7Stream('sample.json', $response->getBody(), $fileOptions);
525+
$zip->finish();
526+
$psr7OutputStream->close();
527+
528+
$tmpDir = $this->validateAndExtractZip($tmp);
529+
$files = $this->getRecursiveFileList($tmpDir);
530+
531+
$this->assertEquals(array('sample.json'), $files);
532+
$this->assertStringEqualsFile($tmpDir . '/sample.json', $body);
533+
}
534+
507535
public function testAddFileFromPsr7StreamWithFileSizeSet(): void
508536
{
509537
[$tmp, $stream] = $this->getTmpFileStream();

0 commit comments

Comments
 (0)