Skip to content
This repository has been archived by the owner on Feb 5, 2019. It is now read-only.

Commit

Permalink
adding StreamResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
Joan Fabrégat committed Mar 2, 2018
1 parent b999d62 commit 1690f2b
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 14 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "codeinchq/lib-psr7responses",
"version": "1.1.1",
"version": "1.1.2",
"description": "A collection of PSR-7 responses",
"homepage": "https://github.com/codeinchq/lib-psr7responses",
"type": "library",
Expand Down
25 changes: 12 additions & 13 deletions src/FileResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
//
declare(strict_types = 1);
namespace CodeInc\Psr7Responses;
use GuzzleHttp\Psr7\Response;
use function GuzzleHttp\Psr7\stream_for;


/**
Expand All @@ -31,7 +29,7 @@
* @package CodeInc\Psr7Responses
* @author Joan Fabrégat <[email protected]>
*/
class FileResponse extends Response {
class FileResponse extends StreamResponse {
/**
* FileResponse constructor.
*
Expand All @@ -55,22 +53,23 @@ public function __construct(string $filePath, ?string $fileName = null, ?string
$this
);
}

$headers["Content-Type"] = $mimeType ?? mime_content_type($filePath);
$headers["Content-Disposition"] = sprintf("%s; filename=\"%s\"",
$asAttachment !== false ? "attachment" : "inline",
$fileName ?? basename($filePath));
if ($size = filesize($filePath)) {
$headers["Content-Length"] = $size;
}

if (($f = fopen($filePath, "r")) === false) {
throw new ResponseException(
sprintf("Unable to open the file \"%s\" for reading", $filePath),
$this
);
}

parent::__construct($status, $headers, stream_for($f), $version, $reason);
parent::__construct(
$f,
$mimeType ?? mime_content_type($filePath),
filesize($filePath) ?: null,
$fileName ?? basename($filePath),
$asAttachment,
$status,
$headers,
$version,
$reason
);
}
}
66 changes: 66 additions & 0 deletions src/StreamResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
//
// +---------------------------------------------------------------------+
// | CODE INC. SOURCE CODE |
// +---------------------------------------------------------------------+
// | Copyright (c) 2017 - Code Inc. SAS - All Rights Reserved. |
// | Visit https://www.codeinc.fr for more information about licensing. |
// +---------------------------------------------------------------------+
// | NOTICE: All information contained herein is, and remains the |
// | property of Code Inc. SAS. The intellectual and technical concepts |
// | contained herein are proprietary to Code Inc. SAS are protected by |
// | trade secret or copyright law. Dissemination of this information or |
// | reproduction of this material is strictly forbidden unless prior |
// | written permission is obtained from Code Inc. SAS. |
// +---------------------------------------------------------------------+
//
// Author: Joan Fabrégat <[email protected]>
// Date: 23/02/2018
// Time: 19:15
// Project: lib-psr7responses
//
declare(strict_types = 1);
namespace CodeInc\Psr7Responses;
use GuzzleHttp\Psr7\Response;
use function GuzzleHttp\Psr7\stream_for;
use Psr\Http\Message\StreamInterface;


/**
* Class StreamResponse
*
* @package CodeInc\Psr7Responses
* @author Joan Fabrégat <[email protected]>
*/
class StreamResponse extends Response {
/**
* StreamResponse constructor.
*
* @param resource|string|null|int|float|bool|StreamInterface|callable $resource
* @param null|string $fileName
* @param null|string $mimeType
* @param int|null $contentLength
* @param bool|null $asAttachment
* @param int $status
* @param array $headers
* @param string $version
* @param null|string $reason
*/
public function __construct($resource, ?string $mimeType = null, ?int $contentLength = null,
?string $fileName = null, ?bool $asAttachment = null, int $status = 200, array $headers = [],
string $version = '1.1', ?string $reason = null)
{
if ($mimeType) {
$headers["Content-Type"] = $mimeType;
}
$headers["Content-Disposition"] = $asAttachment !== false ? "attachment" : "inline";
if ($fileName) {
$headers["Content-Disposition"] .= sprintf("; filename=\"%s\"", $fileName);
}
if ($contentLength !== null) {
$headers["Content-Length"] = $contentLength;
}

parent::__construct($status, $headers, stream_for($resource), $version, $reason);
}
}

0 comments on commit 1690f2b

Please sign in to comment.