Skip to content
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

Add ability to set instance of StreamInterface as output stream #172

Merged
merged 2 commits into from Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/Option/Archive.php
Expand Up @@ -3,6 +3,8 @@

namespace ZipStream\Option;

use Psr\Http\Message\StreamInterface;

final class Archive
{
const DEFAULT_DEFLATE_LEVEL = 6;
Expand Down Expand Up @@ -104,7 +106,7 @@ final class Archive
private $deflateLevel = 6;

/**
* @var resource
* @var StreamInterface|resource
*/
private $outputStream;

Expand Down Expand Up @@ -228,15 +230,15 @@ public function setContentType(string $contentType): void
}

/**
* @return resource
* @return StreamInterface|resource
*/
public function getOutputStream()
{
return $this->outputStream;
}

/**
* @param resource $outputStream
* @param StreamInterface|resource $outputStream
*/
public function setOutputStream($outputStream): void
{
Expand Down
8 changes: 7 additions & 1 deletion src/ZipStream.php
Expand Up @@ -459,7 +459,13 @@ public function send(string $str): void
}
$this->need_headers = false;

fwrite($this->opt->getOutputStream(), $str);
$outputStream = $this->opt->getOutputStream();

if ($outputStream instanceof StreamInterface) {
$outputStream->write($str);
} else {
fwrite($outputStream, $str);
}

if ($this->opt->isFlushOutput()) {
// flush output buffer if it is on and flushable
Expand Down
28 changes: 28 additions & 0 deletions test/ZipStreamTest.php
Expand Up @@ -10,6 +10,7 @@
use ZipStream\Option\Archive as ArchiveOptions;
use ZipStream\Option\File as FileOptions;
use ZipStream\Option\Method;
use ZipStream\Stream;
use ZipStream\ZipStream;

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

public function testAddFileFromPsr7StreamWithOutputToPsr7Stream(): void
{
[$tmp, $resource] = $this->getTmpFileStream();
$psr7OutputStream = new Stream($resource);

$options = new ArchiveOptions();
$options->setOutputStream($psr7OutputStream);

$zip = new ZipStream(null, $options);

$body = 'Sample String Data';
$response = new Response(200, [], $body);

$fileOptions = new FileOptions();
$fileOptions->setMethod(Method::STORE());

$zip->addFileFromPsr7Stream('sample.json', $response->getBody(), $fileOptions);
$zip->finish();
$psr7OutputStream->close();

$tmpDir = $this->validateAndExtractZip($tmp);

$files = $this->getRecursiveFileList($tmpDir);
$this->assertEquals(array('sample.json'), $files);
$this->assertStringEqualsFile($tmpDir . '/sample.json', $body);
}

public function testAddFileFromPsr7StreamWithFileSizeSet(): void
{
[$tmp, $stream] = $this->getTmpFileStream();
Expand Down