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

Commit

Permalink
Merge pull request #16 from CodeIncHQ/1.x
Browse files Browse the repository at this point in the history
v1.4.4
  • Loading branch information
Joan Fabrégat authored Sep 14, 2018
2 parents 566a965 + 42778e4 commit 4365e9c
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 73 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
71 changes: 6 additions & 65 deletions src/FileResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand All @@ -37,91 +36,33 @@ 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
* @param int $status
* @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,
$version,
$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;
}
}
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
);
}
}
8 changes: 3 additions & 5 deletions src/ResponseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
declare(strict_types = 1);
namespace CodeInc\Psr7Responses;
use Psr\Http\Message\ResponseInterface;
use Throwable;
use Exception;


/**
Expand All @@ -34,7 +32,7 @@
* @license MIT <https://github.com/CodeIncHQ/Psr7Responses/blob/master/LICENSE>
* @link https://github.com/CodeIncHQ/Psr7Responses
*/
class ResponseException extends Exception
class ResponseException extends \RuntimeException
{
/**
* @var ResponseInterface
Expand All @@ -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);
Expand Down
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 4365e9c

Please sign in to comment.