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

Commit

Permalink
dev for optimised compatibility with PSR-17
Browse files Browse the repository at this point in the history
  • Loading branch information
Joan Fabrégat committed Oct 8, 2018
1 parent bd7eaa7 commit 5d66240
Show file tree
Hide file tree
Showing 15 changed files with 216 additions and 219 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"license": "MIT",
"require": {
"php": ">=7.1",
"ext-json": "*",
"psr/http-message": "^1.0",
"guzzlehttp/psr7": "^1.4",
"codeinc/error-renderer": "^1.1",
Expand Down
41 changes: 12 additions & 29 deletions src/DebugResponse.php → src/CharsetResponseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,36 @@
// +---------------------------------------------------------------------+
// | 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: 04/03/2018
// Time: 11:55
// Date: 08/10/2018
// Project: Psr7Responses
//
declare(strict_types = 1);
declare(strict_types=1);
namespace CodeInc\Psr7Responses;
use CodeInc\Psr7Responses\Tests\DebugResponseTest;


/**
* Class DebugResponse
* Interface CharsetResponseInterface
*
* @see DebugResponseTest
* @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
*/
class DebugResponse extends HtmlResponse
interface CharsetResponseInterface
{
/**
* DebugResponse constructor.
*
* @param $debugInfos
* @param null|string $charset
* @param int $status
* @param array $headers
* @param string $version
* @param null|string $reason
*/
public function __construct($debugInfos, ?string $charset = null, int $status = 200, array $headers = [],
string $version = '1.1', ?string $reason = null)
{
ob_start();
var_dump($debugInfos);
parent::__construct(ob_get_clean(), $charset, $status, $headers, $version, $reason);
}
/**
* Returns the response's charset.
*
* @return string
*/
public function getCharset():string;
}
39 changes: 24 additions & 15 deletions src/ErrorResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
* @uses ErrorResponseTest
* @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 ErrorResponse extends HtmlResponse
{
Expand All @@ -39,23 +42,29 @@ class ErrorResponse extends HtmlResponse
*/
private $error;

/**
* ErrorResponse constructor.
*
* @param \Throwable $error
* @param null|string $charset
* @param int $status
* @param array $headers
* @param string $version
* @param null|string $reason
*/
public function __construct(\Throwable $error, ?string $charset = null,
int $status = 500, array $headers = [], string $version = '1.1',
?string $reason = null)
/**
* ErrorResponse constructor.
*
* @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')
{
$this->error = $error;
parent::__construct((new HtmlErrorRenderer($error))->get(),
$charset, $status, $headers, $version, $reason);
parent::__construct(
(new HtmlErrorRenderer($error))->get(),
$code,
$reasonPhrase,
$charset,
$headers,
$version
);
}

/**
Expand Down
24 changes: 12 additions & 12 deletions src/FileResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,27 @@
* @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
{
public const DEFAULT_MIME_TYPE = 'application/octet-stream';

/**
* FileResponse constructor.
*
* @param string|resource|StreamInterface $file
* @param null|string $fileName
* @param string $fileName
* @param int $code
* @param string $reasonPhrase
* @param null|string $contentType
* @param bool $asAttachment
* @param int $status
* @param array $headers
* @param string $version
* @param null|string $reason
* @throws \CodeInc\MediaTypes\Exceptions\MediaTypesException
*/
public function __construct($file, string $fileName, ?string $contentType = null,
bool $asAttachment = true, int $status = 200, array $headers = [],
string $version = '1.1', ?string $reason = null)
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)) {
Expand All @@ -75,14 +75,14 @@ public function __construct($file, string $fileName, ?string $contentType = null

parent::__construct(
$stream,
$contentType ?? MediaTypes::getFilenameMediaType($fileName, self::DEFAULT_MIME_TYPE),
$code,
$reasonPhrase,
$contentType ?? MediaTypes::getFilenameMediaType($fileName, 'application/octet-stream'),
null,
$fileName,
$asAttachment,
$status,
$headers,
$version,
$reason
$version
);
}
}
2 changes: 1 addition & 1 deletion src/ForbiddenResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ForbiddenResponse extends HtmlResponse
* @param string $version
* @param null|string $reason
*/
public function __construct(string $html = null, ?string $charset = null, array $headers = [],
public function __construct(string $html = null, string $charset = 'utf-8', array $headers = [],
$body = null, string $version = '1.1', ?string $reason = null)
{
parent::__construct($html ?? '', $charset, 403, $headers, $version, $reason);
Expand Down
40 changes: 20 additions & 20 deletions src/HtmlResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,38 +31,37 @@
* @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 HtmlResponse extends Response
class HtmlResponse extends Response implements CharsetResponseInterface
{
public const DEFAULT_CHARSET = "utf-8";

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

/**
* @var null|string
* @var string
*/
private $charset;

/**
* HtmlResponse constructor.
*
* @param string $html
* @param null|string $charset
* @param int $status
* @param array $headers
* @param string $version
* @param null|string $reason
*/
public function __construct(string $html, ?string $charset = null, int $status = 200, array $headers = [],
string $version = '1.1', ?string $reason = null)
/**
* 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')
{
$headers["Content-Type"] = "text/html; charset=".($charset ?? self::DEFAULT_CHARSET);
$headers['Content-Type'] = sprintf('text/html; charset=%s', $charset);
$this->html = $html;
$this->charset = $charset;
parent::__construct($status, $headers, $html, $version, $reason);
parent::__construct($code, $headers, $html, $version, $reasonPhrase);
}

/**
Expand All @@ -76,9 +75,10 @@ public function getHtml():string
}

/**
* @return null|string
* @inheritdoc
* @return string
*/
public function getCharset():?string
public function getCharset():string
{
return $this->charset;
}
Expand Down
35 changes: 13 additions & 22 deletions src/HttpProxyResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* @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 HttpProxyResponse extends Response
{
Expand All @@ -59,31 +60,13 @@ class HttpProxyResponse extends Response
* ProxyResponse constructor.
*
* @param string $remoteUrl
* @param int $status
* @param int $code
* @param string $reasonPhrase
* @param array $headers
* @param string $version
* @param null|string $reason
* @throws ResponseException
*/
public function __construct(string $remoteUrl, int $status = 200, array $headers = [],
string $version = '1.1', ?string $reason = null)
{
$this->setRemoteUrl($remoteUrl);

parent::__construct(
$status,
$this->importHttpHeader($headers),
$this->getStream(),
$version,
$reason
);
}

/**
* @param string $remoteUrl
* @throws ResponseException
*/
private function setRemoteUrl(string $remoteUrl):void
public function __construct(string $remoteUrl, int $code = 200, string $reasonPhrase = '',
array $headers = [], string $version = '1.1')
{
if (!filter_var($remoteUrl, FILTER_VALIDATE_URL)) {
throw new ResponseException(
Expand All @@ -92,6 +75,14 @@ private function setRemoteUrl(string $remoteUrl):void
);
}
$this->remoteUrl = $remoteUrl;

parent::__construct(
$code,
$this->importHttpHeader($headers),
$this->getStream(),
$version,
$reasonPhrase
);
}

/**
Expand Down
Loading

0 comments on commit 5d66240

Please sign in to comment.