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

Commit

Permalink
new response type LocalFileResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
Joan Fabrégat committed Sep 14, 2018
1 parent cdd5aa5 commit 01fb822
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
79 changes: 79 additions & 0 deletions src/LocalFileResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?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: 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 <[email protected]>
*/
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
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
declare(strict_types=1);
namespace CodeInc\Psr7Responses\Tests;
use CodeInc\Psr7Responses\FileResponse;
use CodeInc\Psr7Responses\LocalFileResponse;


/**
Expand All @@ -33,15 +34,15 @@
* @license MIT <https://github.com/CodeIncHQ/Psr7Responses/blob/master/LICENSE>
* @link https://github.com/CodeIncHQ/Psr7Responses
*/
class FileResponseTest extends AbstractResponseTestCase
class LocalFileResponseTest extends AbstractResponseTestCase
{
/**
* @throws \CodeInc\MediaTypes\Exceptions\MediaTypesException
* @throws \CodeInc\Psr7Responses\ResponseException
*/
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);
Expand Down

0 comments on commit 01fb822

Please sign in to comment.