From cdd5aa5b685b06a6186f5ffda52f7481a9224acf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20Fabr=C3=A9gat?= Date: Fri, 14 Sep 2018 17:29:57 +0200 Subject: [PATCH 1/4] improvement of FileResponse --- src/FileResponse.php | 71 ++++---------------------------------------- 1 file changed, 6 insertions(+), 65 deletions(-) diff --git a/src/FileResponse.php b/src/FileResponse.php index a1ebaa7..a37ef2c 100644 --- a/src/FileResponse.php +++ b/src/FileResponse.php @@ -22,7 +22,6 @@ declare(strict_types = 1); namespace CodeInc\Psr7Responses; use CodeInc\MediaTypes\MediaTypes; -use CodeInc\Psr7Responses\Tests\FileResponseTest; use Psr\Http\Message\StreamInterface; @@ -37,20 +36,10 @@ class FileResponse extends StreamResponse { public const DEFAULT_MIME_TYPE = 'application/octet-stream'; - /** - * @var string - */ - private $filePath; - - /** - * @var string - */ - private $fileName; - /** * FileResponse constructor. * - * @param string|StreamInterface $file Path to the file or stream of its content + * @param StreamInterface $fileStream * @param null|string $fileName * @param null|string $contentType * @param bool $asAttachment @@ -58,45 +47,17 @@ class FileResponse extends StreamResponse * @param array $headers * @param string $version * @param null|string $reason - * @throws ResponseException * @throws \CodeInc\MediaTypes\Exceptions\MediaTypesException */ - public function __construct($file, ?string $fileName = null, ?string $contentType = null, + public function __construct(StreamInterface $fileStream, string $fileName, ?string $contentType = null, bool $asAttachment = true, int $status = 200, array $headers = [], string $version = '1.1', ?string $reason = null) { - if (!$file instanceof StreamInterface) { - if (!is_file($file)) { - throw new ResponseException( - sprintf("The path \"%s\" is not a file or does not exist", $file), - $this - ); - } - if (($f = fopen($file, "r")) === false) { - throw new ResponseException( - sprintf("Unable to open the file \"%s\" for reading", $file), - $this - ); - } - $file = $f; - } - if (!$fileName) { - $fileName = basename($file); - } - - // looking up the mime type using - if (!$contentType && $fileName) { - $contentType = MediaTypes::getFilenameMediaType($fileName); - } - - $this->fileName = $fileName; - $this->filePath = $file; - parent::__construct( - $file, - $contentType ?? self::DEFAULT_MIME_TYPE, - filesize($file) ?: null, - $fileName ?? basename($file), + $fileStream, + $contentType ?? MediaTypes::getFilenameMediaType($fileName, self::DEFAULT_MIME_TYPE), + null, + $fileName, $asAttachment, $status, $headers, @@ -104,24 +65,4 @@ public function __construct($file, ?string $fileName = null, ?string $contentTyp $reason ); } - - /** - * Returns the file name. - * - * @return string - */ - public function getFileName():string - { - return $this->fileName; - } - - /** - * Returns the file path. - * - * @return string - */ - public function getFilePath():string - { - return $this->filePath; - } } \ No newline at end of file From 01fb822fb9d6fa146cc4647d0559624d3be0e5c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20Fabr=C3=A9gat?= Date: Fri, 14 Sep 2018 17:30:11 +0200 Subject: [PATCH 2/4] new response type LocalFileResponse --- src/LocalFileResponse.php | 79 +++++++++++++++++++ ...onseTest.php => LocalFileResponseTest.php} | 5 +- 2 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 src/LocalFileResponse.php rename tests/{FileResponseTest.php => LocalFileResponseTest.php} (91%) diff --git a/src/LocalFileResponse.php b/src/LocalFileResponse.php new file mode 100644 index 0000000..58443f1 --- /dev/null +++ b/src/LocalFileResponse.php @@ -0,0 +1,79 @@ + +// Date: 23/02/2018 +// Time: 19:15 +// Project: Psr7Responses +// +declare(strict_types = 1); +namespace CodeInc\Psr7Responses; +use CodeInc\Psr7Responses\Tests\LocalFileResponseTest; +use function GuzzleHttp\Psr7\stream_for; + + +/** + * Class LocalFileResponse + * + * @see LocalFileResponseTest + * @package CodeInc\Psr7Responses + * @author Joan Fabrégat + */ +class LocalFileResponse extends FileResponse +{ + /** + * LocalFileResponse constructor. + * + * @param string $filePath Local file path + * @param null|string $fileName File's name (determined from the local file path if not specified) + * @param null|string $contentType File's content type (determined from the file's name if not specified) + * @param bool $asAttachment Defines if the file should be sent as an attachment + * @param int $status + * @param array $headers + * @param string $version + * @param null|string $reason + * @throws ResponseException + * @throws \CodeInc\MediaTypes\Exceptions\MediaTypesException + */ + public function __construct(string $filePath, ?string $fileName = null, ?string $contentType = null, + bool $asAttachment = true, int $status = 200, array $headers = [], + string $version = '1.1', ?string $reason = null) + { + if (!is_file($filePath)) { + throw new ResponseException( + sprintf("The path \"%s\" is not a file or does not exist", $filePath), + $this + ); + } + if (($f = fopen($filePath, "r")) === false) { + throw new ResponseException( + sprintf("Unable to open the file \"%s\" for reading", $filePath), + $this + ); + } + + parent::__construct( + stream_for($f), + $fileName ?? basename($filePath), + $contentType, + $asAttachment, + $status, + $headers, + $version, + $reason + ); + } +} \ No newline at end of file diff --git a/tests/FileResponseTest.php b/tests/LocalFileResponseTest.php similarity index 91% rename from tests/FileResponseTest.php rename to tests/LocalFileResponseTest.php index f14caae..8ddfe64 100644 --- a/tests/FileResponseTest.php +++ b/tests/LocalFileResponseTest.php @@ -22,6 +22,7 @@ declare(strict_types=1); namespace CodeInc\Psr7Responses\Tests; use CodeInc\Psr7Responses\FileResponse; +use CodeInc\Psr7Responses\LocalFileResponse; /** @@ -33,7 +34,7 @@ * @license MIT * @link https://github.com/CodeIncHQ/Psr7Responses */ -class FileResponseTest extends AbstractResponseTestCase +class LocalFileResponseTest extends AbstractResponseTestCase { /** * @throws \CodeInc\MediaTypes\Exceptions\MediaTypesException @@ -41,7 +42,7 @@ class FileResponseTest extends AbstractResponseTestCase */ public function test():void { - $response = new FileResponse(__DIR__.'/Assets/file.txt'); + $response = new LocalFileResponse(__DIR__.'/Assets/file.txt'); self::assertIsResponse($response); self::assertResponseStatusCode(200, $response); self::assertResponseHasBody($response); From bd5b8e5ebf5f20c9a7737895b7c7aed64ff81c48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20Fabr=C3=A9gat?= Date: Fri, 14 Sep 2018 17:30:19 +0200 Subject: [PATCH 3/4] ResponseException now extends RuntimeException --- src/ResponseException.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/ResponseException.php b/src/ResponseException.php index 9111e77..8654c29 100644 --- a/src/ResponseException.php +++ b/src/ResponseException.php @@ -22,8 +22,6 @@ declare(strict_types = 1); namespace CodeInc\Psr7Responses; use Psr\Http\Message\ResponseInterface; -use Throwable; -use Exception; /** @@ -34,7 +32,7 @@ * @license MIT * @link https://github.com/CodeIncHQ/Psr7Responses */ -class ResponseException extends Exception +class ResponseException extends \RuntimeException { /** * @var ResponseInterface @@ -47,10 +45,10 @@ class ResponseException extends Exception * @param string $message * @param ResponseInterface $response * @param int|null $code - * @param null|Throwable $previous + * @param null|\Throwable $previous */ public function __construct(string $message, ResponseInterface $response, ?int $code = null, - ?Throwable $previous = null) + ?\Throwable $previous = null) { $this->response = $response; parent::__construct($message, $code ?? 0, $previous); From 42778e461beedd47d63bd3f7a10ce5cbb3ea0e81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20Fabr=C3=A9gat?= Date: Fri, 14 Sep 2018 17:30:39 +0200 Subject: [PATCH 4/4] v1.4.4 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index bbe3959..5cd562a 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "codeinc/psr7-responses", - "version": "1.4.3", + "version": "1.4.4", "description": "A collection of PSR-7 responses", "homepage": "https://github.com/CodeIncHQ/Psr7Responses", "type": "library",