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 #21 from CodeIncHQ/2.x
Browse files Browse the repository at this point in the history
v2.1.0
  • Loading branch information
Joan Fabrégat authored Oct 8, 2018
2 parents 4598f7d + 53dab44 commit 4577b32
Show file tree
Hide file tree
Showing 19 changed files with 132 additions and 378 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This PHP 7.1 library provides a collection of [PSR-7](https://www.php-fig.org/ps
* [`HtmlResponse`](src/HtmlResponse.php)
* [`HttpProxyResponse`](src/HttpProxyResponse.php)
* [`JsonResponse`](src/JsonResponse.php)
* [`LocalFileResponse`](src/LocalFileResponse.php)
* [`NotFoundResponse`](src/NotFoundResponse.php)
* [`RedirectResponse`](src/RedirectResponse.php)
* [`StreamResponse`](src/StreamResponse.php)
Expand Down
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": "2.0.2",
"version": "2.1.0",
"description": "A collection of PSR-7 responses",
"homepage": "https://github.com/CodeIncHQ/Psr7Responses",
"type": "library",
Expand Down
38 changes: 0 additions & 38 deletions src/CharsetResponseInterface.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/EmptyResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class EmptyResponse extends Response
* @param array $headers
* @param string $version
*/
public function __construct(int $code = 404, string $reasonPhrase = '',
public function __construct(int $code = 200, string $reasonPhrase = '',
array $headers = [], string $version = '1.1')
{
parent::__construct($code, $headers, '', $version, $reasonPhrase);
Expand Down
4 changes: 1 addition & 3 deletions src/ErrorResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,18 @@ class ErrorResponse extends HtmlResponse
* @param \Throwable $error
* @param int $code
* @param string $reasonPhrase
* @param string $charset
* @param array $headers
* @param string $version
* @throws \ReflectionException
*/
public function __construct(\Throwable $error, int $code = 200, string $reasonPhrase = '',
string $charset = 'utf-8', array $headers = [], string $version = '1.1')
array $headers = self::DEFAULT_HEADERS, string $version = '1.1')
{
$this->error = $error;
parent::__construct(
(new HtmlErrorRenderer($error))->get(),
$code,
$reasonPhrase,
$charset,
$headers,
$version
);
Expand Down
67 changes: 25 additions & 42 deletions src/FileResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,86 +3,69 @@
// +---------------------------------------------------------------------+
// | CODE INC. SOURCE CODE |
// +---------------------------------------------------------------------+
// | Copyright (c) 2017 - Code Inc. SAS - All Rights Reserved. |
// | Copyright (c) 2018 - 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 |
// | 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
// Date: 08/10/2018
// Project: Psr7Responses
//
declare(strict_types = 1);
declare(strict_types=1);
namespace CodeInc\Psr7Responses;
use CodeInc\MediaTypes\MediaTypes;
use function GuzzleHttp\Psr7\stream_for;
use Psr\Http\Message\StreamInterface;


/**
* Class FileResponse
*
* @see FileResponseTest
* @package CodeInc\Psr7Responses
* @author Joan Fabrégat <[email protected]>
* @license MIT <https://github.com/CodeIncHQ/Psr7Responses/blob/master/LICENSE>
* @link https://github.com/CodeIncHQ/Psr7Responses
* @version 2
*/
class FileResponse extends StreamResponse
{
/**
* FileResponse constructor.
*
* @param string|resource|StreamInterface $file
* @param string $fileName
* @param $data
* @param int $code
* @param string $reasonPhrase
* @param null|string $contentType
* @param int|null $contentLength
* @param bool $asAttachment
* @param array $headers
* @param string $version
* @throws \CodeInc\MediaTypes\Exceptions\MediaTypesException
*/
public function __construct($file, string $fileName, int $code = 200, string $reasonPhrase = '',
?string $contentType = null, bool $asAttachment = true, array $headers = [], string $version = '1.1')
{
if (is_string($file)) {
if (!is_file($file)) {
throw new ResponseException(
sprintf("The path \"%s\" is not a file or does not exist", $file),
$this
);
}
if (($handler = fopen($file, "r")) === false) {
throw new ResponseException(
sprintf("Unable to open the file \"%s\" for reading", $file),
$this
);
}
$stream = stream_for($handler);
public function __construct(string $fileName, $data, int $code = 200, string $reasonPhrase = '',
?string $contentType = null, ?int $contentLength = null, bool $asAttachment = true,
array $headers = [], string $version = '1.1')
{
$stream = stream_for($data);

// adding headers
if (!isset($headers['Content-Type']) && $contentType) {
$headers['Content-Type'] = $contentType
?? MediaTypes::getFilenameMediaType($fileName)
?? 'application/octet-stream';
}
if (!isset($headers['Content-Disposition'])) {
$headers['Content-Disposition'] = sprintf('%s; filename="%s"',
($asAttachment ? 'attachment' : 'inline'), $fileName);
}
else {
$stream = stream_for($file);
if (!isset($headers['Content-Length']) && ($contentLength !== null || ($contentLength = $stream->getSize()) !== null)) {
$headers['Content-Length'] = $contentLength;
}

parent::__construct(
$stream,
$code,
$reasonPhrase,
$contentType ?? MediaTypes::getFilenameMediaType($fileName, 'application/octet-stream'),
null,
$fileName,
$asAttachment,
$headers,
$version
);
}
parent::__construct($stream, $code, $reasonPhrase, $headers, $version);
}
}
11 changes: 8 additions & 3 deletions src/ForbiddenResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,18 @@ class ForbiddenResponse extends HtmlResponse
* @param string $html
* @param int $code
* @param string $reasonPhrase
* @param string $charset
* @param array $headers
* @param string $version
*/
public function __construct(string $html = '', int $code = 403, string $reasonPhrase = '',
string $charset = 'utf-8', array $headers = [], string $version = '1.1')
array $headers = self::DEFAULT_HEADERS, string $version = '1.1')
{
parent::__construct($html, $code, $reasonPhrase, $charset, $headers, $version);
parent::__construct(
$html,
$code,
$reasonPhrase,
$headers,
$version
);
}
}
35 changes: 14 additions & 21 deletions src/HtmlResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,37 @@
* @link https://github.com/CodeIncHQ/Psr7Responses
* @version 2
*/
class HtmlResponse extends Response implements CharsetResponseInterface
class HtmlResponse extends Response
{
/**
* @var string
*/
private $html;
public const DEFAULT_HEADERS = [
'Content-Type' => 'text/html; charset=utf-8'
];

/**
* @var string
*/
private $charset;
private $html;

/**
* HtmlResponse constructor.
*
* @param string $html
* @param int $code
* @param string $reasonPhrase
* @param string $charset
* @param array $headers
* @param string $version
*/
public function __construct(string $html, int $code = 200, string $reasonPhrase = '',
string $charset = 'utf-8', array $headers = [], string $version = '1.1')
array $headers = self::DEFAULT_HEADERS, string $version = '1.1')
{
$headers['Content-Type'] = sprintf('text/html; charset=%s', $charset);
$this->html = $html;
$this->charset = $charset;
parent::__construct($code, $headers, $html, $version, $reasonPhrase);
$this->html = $html;
parent::__construct(
$code,
$headers,
$html,
$version,
$reasonPhrase
);
}

/**
Expand All @@ -73,13 +75,4 @@ public function getHtml():string
{
return $this->html;
}

/**
* @inheritdoc
* @return string
*/
public function getCharset():string
{
return $this->charset;
}
}
37 changes: 17 additions & 20 deletions src/HttpProxyResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ class HttpProxyResponse extends Response
/**
* @var string[]
*/
private $acceptableResponseHeaders = [
'content-type',
'content-length',
'content-disposition',
'date',
'last-modified',
'etag',
'pragma',
'expires',
'cache-control'
private $acceptableProxyHeaders = [
'Content-Type',
'Content-Length',
'Content-Disposition',
'Date',
'Last-Modified',
'Etag',
'Pragma',
'Expires',
'Cache-Control'
];

/**
Expand All @@ -75,10 +75,7 @@ class HttpProxyResponse extends Response
public function __construct(string $remoteUrl, array $headers = [], string $version = '1.1')
{
if (!filter_var($remoteUrl, FILTER_VALIDATE_URL)) {
throw new ResponseException(
sprintf("%s is not a valid URL", $remoteUrl),
$this
);
throw new \RuntimeException(sprintf("%s is not a valid URL", $remoteUrl));
}
$this->remoteUrl = $remoteUrl;

Expand Down Expand Up @@ -115,7 +112,7 @@ public function getResponseHeaders():array
{
$headers = [];
foreach ($this->getResponse()->getHeaders() as $header => $values) {
if (in_array(strtolower($header), $this->acceptableResponseHeaders)) {
if (preg_grep('/^'.preg_quote($header, '$/').'/ui', $this->acceptableProxyHeaders)) {
$headers[$header] = $values;
}
}
Expand All @@ -125,17 +122,17 @@ public function getResponseHeaders():array
/**
* @return string[]
*/
public function getAcceptableResponseHeaders():array
public function getAcceptableProxyHeaders():array
{
return $this->acceptableResponseHeaders;
return $this->acceptableProxyHeaders;
}

/**
* @param string[] $acceptableResponseHeaders
* @param string[] $acceptableProxyHeaders
*/
public function setAcceptableResponseHeaders(array $acceptableResponseHeaders):void
public function setAcceptableProxyHeaders(array $acceptableProxyHeaders):void
{
$this->acceptableResponseHeaders = $acceptableResponseHeaders;
$this->acceptableProxyHeaders = $acceptableProxyHeaders;
}

/**
Expand Down
Loading

0 comments on commit 4577b32

Please sign in to comment.