Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
odan committed Jan 2, 2021
1 parent cdea7ac commit 5d185ff
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
6 changes: 0 additions & 6 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,3 @@ parameters:
level: max
paths:
- src
- tests
- bin
reportUnmatchedIgnoredErrors: false
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
ignoreErrors:
19 changes: 16 additions & 3 deletions src/ZipResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamFactoryInterface;
use UnexpectedValueException;

/**
* A HTTP ZIP responder.
Expand Down Expand Up @@ -31,7 +32,7 @@ public function __construct(StreamFactoryInterface $streamFactory)
* @param ResponseInterface $response The response
* @param string $filename The source ZIP file
* @param string $outputName The output name
* @param string $disposition the content disposition: 'attachment' or 'inline'
* @param string $disposition The content disposition: 'attachment' or 'inline'
*
* @return ResponseInterface The response
*/
Expand All @@ -52,7 +53,7 @@ public function zipFile(
* @param ResponseInterface $response The response
* @param resource $stream The source ZIP stream
* @param string $outputName The output name
* @param string $disposition the content disposition: 'attachment' or 'inline'
* @param string $disposition The content disposition: 'attachment' or 'inline'
*
* @return ResponseInterface The response
*/
Expand All @@ -72,7 +73,7 @@ public function zipStream(
*
* @param ResponseInterface $response The response
* @param string $outputName The output ZIP filename
* @param string $contentDisposition the content disposition
* @param string $contentDisposition The content disposition
*
* @return ResponseInterface The response
*/
Expand Down Expand Up @@ -113,13 +114,25 @@ private function withHttpHeaders(
* @param int $level The level of compression. Can be given as 0 for no compression up to 9 for maximum compression.
* If not given, the default compression level will be the default compression level of the zlib library.
*
* @throws UnexpectedValueException
*
* @return ResponseInterface The response
*/
public function deflateResponse(ResponseInterface $response, int $level = -1): ResponseInterface
{
$response = $response->withHeader('Content-Encoding', 'deflate');
$content = gzdeflate((string)$response->getBody(), $level);

if ($content === false) {
throw new UnexpectedValueException('The HTTP body could not be compressed.');
}

$stream = fopen('php://memory', 'r+');

if (!$stream) {
throw new UnexpectedValueException('The stream could not be created.');
}

fwrite($stream, $content);

return $response->withBody($this->streamFactory->createStreamFromResource($stream));
Expand Down

0 comments on commit 5d185ff

Please sign in to comment.