Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
Cleanup (#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz committed Jul 12, 2020
1 parent 80aac7f commit ae3a295
Show file tree
Hide file tree
Showing 18 changed files with 48 additions and 59 deletions.
8 changes: 5 additions & 3 deletions src/CookieCollection.php
Expand Up @@ -13,6 +13,8 @@
use IteratorAggregate;
use Psr\Http\Message\ResponseInterface;

use Yiisoft\Http\Header;

use function array_keys;
use function array_values;
use function array_walk;
Expand All @@ -34,7 +36,7 @@ final class CookieCollection implements IteratorAggregate, ArrayAccess, Countabl
/**
* CookieCollection constructor.
*
* @param array $cookies the cookies that this collection initially contains.
* @param Cookie[] $cookies the cookies that this collection initially contains.
*/
public function __construct(array $cookies = [])
{
Expand Down Expand Up @@ -339,7 +341,7 @@ public function addToResponse(ResponseInterface $response): ResponseInterface
*/
public function setToResponse(ResponseInterface $response): ResponseInterface
{
$response = $response->withoutHeader('Set-Cookie');
$response = $response->withoutHeader(Header::SET_COOKIE);
return $this->addToResponse($response);
}

Expand All @@ -353,7 +355,7 @@ public function setToResponse(ResponseInterface $response): ResponseInterface
public static function fromResponse(ResponseInterface $response): self
{
$collection = new self();
foreach ($response->getHeader('Set-Cookie') as $setCookieString) {
foreach ($response->getHeader(Header::SET_COOKIE) as $setCookieString) {
$cookie = Cookie::fromCookieString($setCookieString);
$collection->add($cookie);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Data/Formatter/HtmlDataResponseFormatter.php
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\Yii\Web\Data\Formatter;

use Psr\Http\Message\ResponseInterface;
use Yiisoft\Http\Header;
use Yiisoft\Yii\Web\Data\DataResponse;
use Yiisoft\Yii\Web\Data\DataResponseFormatterInterface;

Expand All @@ -26,7 +27,7 @@ public function format(DataResponse $dataResponse): ResponseInterface
$response = $dataResponse->getResponse();
$response->getBody()->write((string)$data);

return $response->withHeader('Content-Type', $this->contentType . '; charset=' . $this->encoding);
return $response->withHeader(Header::CONTENT_TYPE, $this->contentType . '; charset=' . $this->encoding);
}

public function withEncoding(string $encoding): self
Expand Down
3 changes: 2 additions & 1 deletion src/Data/Formatter/JsonDataResponseFormatter.php
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\Yii\Web\Data\Formatter;

use Psr\Http\Message\ResponseInterface;
use Yiisoft\Http\Header;
use Yiisoft\Serializer\JsonSerializer;
use Yiisoft\Yii\Web\Data\DataResponse;
use Yiisoft\Yii\Web\Data\DataResponseFormatterInterface;
Expand All @@ -25,7 +26,7 @@ public function format(DataResponse $dataResponse): ResponseInterface
$response = $dataResponse->getResponse();
$response->getBody()->write($content);

return $response->withHeader('Content-Type', $this->contentType);
return $response->withHeader(Header::CONTENT_TYPE, $this->contentType);
}

public function withOptions(int $options): self
Expand Down
3 changes: 2 additions & 1 deletion src/Data/Formatter/XmlDataResponseFormatter.php
Expand Up @@ -9,6 +9,7 @@
use DOMException;
use DOMText;
use Psr\Http\Message\ResponseInterface;
use Yiisoft\Http\Header;
use Yiisoft\Strings\StringHelper;
use Yiisoft\Yii\Web\Data\DataResponse;
use Yiisoft\Yii\Web\Data\DataResponseFormatterInterface;
Expand Down Expand Up @@ -63,7 +64,7 @@ public function format(DataResponse $dataResponse): ResponseInterface
$response = $dataResponse->getResponse();
$response->getBody()->write($content);

return $response->withHeader('Content-Type', $this->contentType . '; ' . $this->encoding);
return $response->withHeader(Header::CONTENT_TYPE, $this->contentType . '; ' . $this->encoding);
}

public function withVersion(string $version): self
Expand Down
5 changes: 3 additions & 2 deletions src/ErrorHandler/ErrorCatcher.php
Expand Up @@ -10,12 +10,13 @@
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Yiisoft\Http\Header;
use Yiisoft\Http\HeaderHelper;
use Yiisoft\Http\Status;

/**
* ErrorCatcher catches all throwables from the next middlewares and renders it
* accoring to the content type passed by the client.
* according to the content type passed by the client.
*/
final class ErrorCatcher implements MiddlewareInterface
{
Expand Down Expand Up @@ -83,7 +84,7 @@ private function handleException(\Throwable $e, ServerRequestInterface $request)
}
$content = $this->errorHandler->handleCaughtThrowable($e, $renderer);
$response = $this->responseFactory->createResponse(Status::INTERNAL_SERVER_ERROR)
->withHeader('Content-type', $contentType);
->withHeader(Header::CONTENT_TYPE, $contentType);
$response->getBody()->write($content);
return $response;
}
Expand Down
3 changes: 0 additions & 3 deletions src/ErrorHandler/HtmlRenderer.php
Expand Up @@ -19,12 +19,9 @@ final class HtmlRenderer extends ThrowableRenderer
private string $errorTemplate;
private string $exceptionTemplate;

private array $defaultTemplates;

public function __construct(array $templates = [])
{
$this->defaultTemplatePath = $templates['path'];
$this->defaultTemplates = $templates['default'];
$this->errorTemplate = $templates['error'] ?? $this->defaultTemplatePath . '/error.php';
$this->exceptionTemplate = $templates['exception'] ?? $this->defaultTemplatePath . '/exception.php';
}
Expand Down
4 changes: 2 additions & 2 deletions src/ErrorHandler/XmlRenderer.php
Expand Up @@ -24,9 +24,9 @@ public function renderVerbose(\Throwable $t): string
$out .= "<error>\n";
$out .= $this->tag('type', get_class($t));
$out .= $this->tag('message', $this->cdata($t->getMessage()));
$out .= $this->tag('code', $this->cdata($t->getCode()));
$out .= $this->tag('code', $this->cdata((string) $t->getCode()));
$out .= $this->tag('file', $t->getFile());
$out .= $this->tag('line', $t->getLine());
$out .= $this->tag('line', (string) $t->getLine());
$out .= $this->tag('trace', $t->getTraceAsString());
$out .= '</error>';
return $out;
Expand Down
2 changes: 1 addition & 1 deletion src/ErrorHandler/templates/error.php
Expand Up @@ -13,7 +13,7 @@
}
?>
<!DOCTYPE html>
<html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title><?= $this->htmlEncode($name) ?></title>
Expand Down
1 change: 0 additions & 1 deletion src/ErrorHandler/templates/exception.php
Expand Up @@ -17,7 +17,6 @@
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
Expand Down
1 change: 1 addition & 0 deletions src/ErrorHandler/templates/previousException.php
@@ -1,4 +1,5 @@
<?php
/* @var $throwable \Throwable */
/* @var $this \Yiisoft\Yii\Web\ErrorHandler\HtmlRenderer */
?>
<div class="previous">
Expand Down
1 change: 1 addition & 0 deletions src/Middleware/ForceSecureConnection.php
Expand Up @@ -46,6 +46,7 @@ public function __construct(ResponseFactoryInterface $responseFactory)
{
$this->responseFactory = $responseFactory;
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if ($this->redirect && strcasecmp($request->getUri()->getScheme(), 'http') === 0) {
Expand Down
27 changes: 10 additions & 17 deletions src/Middleware/HttpCache.php
Expand Up @@ -4,11 +4,11 @@

namespace Yiisoft\Yii\Web\Middleware;

use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Yiisoft\Http\Header;
use Yiisoft\Http\Method;
use Yiisoft\Http\Status;

Expand Down Expand Up @@ -68,13 +68,6 @@ final class HttpCache implements MiddlewareInterface
*/
private ?string $cacheControlHeader = self::DEFAULT_HEADER;

private ResponseFactoryInterface $responseFactory;

public function __construct(ResponseFactoryInterface $responseFactory)
{
$this->responseFactory = $responseFactory;
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if (
Expand Down Expand Up @@ -105,16 +98,16 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
}

if ($this->cacheControlHeader !== null) {
$response = $response->withHeader('Cache-Control', $this->cacheControlHeader);
$response = $response->withHeader(Header::CACHE_CONTROL, $this->cacheControlHeader);
}
if ($etag !== null) {
$response = $response->withHeader('Etag', $etag);
$response = $response->withHeader(Header::ETAG, $etag);
}

// https://tools.ietf.org/html/rfc7232#section-4.1
if ($lastModified !== null && (!$cacheIsValid || ($cacheIsValid && $etag === null))) {
if ($lastModified !== null && (!$cacheIsValid || $etag === null)) {
$response = $response->withHeader(
'Last-Modified',
Header::LAST_MODIFIED,
gmdate('D, d M Y H:i:s', $lastModified) . ' GMT'
);
}
Expand All @@ -133,14 +126,14 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
*/
private function validateCache(ServerRequestInterface $request, ?int $lastModified, ?string $etag): bool
{
if ($request->hasHeader('If-None-Match')) {
if ($request->hasHeader(Header::IF_NONE_MATCH)) {
// HTTP_IF_NONE_MATCH takes precedence over HTTP_IF_MODIFIED_SINCE
// http://tools.ietf.org/html/rfc7232#section-3.3
return $etag !== null && \in_array($etag, $this->getETags($request), true);
}

if ($request->hasHeader('If-Modified-Since')) {
$header = $request->getHeaderLine('If-Modified-Since');
if ($request->hasHeader(Header::IF_MODIFIED_SINCE)) {
$header = $request->getHeaderLine(Header::IF_MODIFIED_SINCE);
return $lastModified !== null && @strtotime($header) >= $lastModified;
}

Expand All @@ -166,8 +159,8 @@ private function generateEtag(string $seed): string
*/
private function getETags(ServerRequestInterface $request): array
{
if ($request->hasHeader('If-None-Match')) {
$header = $request->getHeaderLine('If-None-Match');
if ($request->hasHeader(Header::IF_NONE_MATCH)) {
$header = $request->getHeaderLine(Header::IF_NONE_MATCH);
$header = \str_replace('-gzip', '', $header);
return \preg_split('/[\s,]+/', $header, -1, PREG_SPLIT_NO_EMPTY) ?: [];
}
Expand Down
8 changes: 0 additions & 8 deletions src/Middleware/TrustedHostsNetworkResolver.php
Expand Up @@ -5,7 +5,6 @@
namespace Yiisoft\Yii\Web\Middleware;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
Expand Down Expand Up @@ -70,15 +69,8 @@ final class TrustedHostsNetworkResolver implements MiddlewareInterface

private ?string $attributeIps = null;

private ResponseFactoryInterface $responseFactory;

private ?Ip $ipValidator = null;

public function __construct(ResponseFactoryInterface $responseFactory)
{
$this->responseFactory = $responseFactory;
}

public function withIpValidator(Ip $ipValidator): self
{
$new = clone $this;
Expand Down
2 changes: 1 addition & 1 deletion src/NotFoundHandler.php
Expand Up @@ -15,7 +15,7 @@ final class NotFoundHandler implements RequestHandlerInterface
/**
* @var ResponseFactoryInterface
*/
private $responseFactory;
private ResponseFactoryInterface $responseFactory;

/**
* NotFoundHandler constructor.
Expand Down
2 changes: 1 addition & 1 deletion src/ServerRequestFactory.php
Expand Up @@ -107,7 +107,7 @@ private function getUri(array $server): UriInterface
}

if (isset($server['HTTP_HOST'])) {
if (1 === \preg_match('/^(.+)\:(\d+)$/', $server['HTTP_HOST'], $matches)) {
if (1 === \preg_match('/^(.+):(\d+)$/', $server['HTTP_HOST'], $matches)) {
$uri = $uri->withHost($matches[1])->withPort($matches[2]);
} else {
$uri = $uri->withHost($server['HTTP_HOST']);
Expand Down
2 changes: 1 addition & 1 deletion src/Session/SessionInterface.php
Expand Up @@ -29,7 +29,7 @@ public function set(string $key, $value): void;
public function close(): void;

/**
* Start sesion if it is not started yet
* Start session if it is not started yet
*/
public function open(): void;

Expand Down
2 changes: 1 addition & 1 deletion src/Session/SessionMiddleware.php
Expand Up @@ -46,7 +46,7 @@ private function commitSession(ServerRequestInterface $request, ResponseInterfac

$currentSid = $this->session->getID();

// SID changed, neeed to send new cookie
// SID changed, need to send new cookie
if ($this->getSidFromRequest($request) !== $currentSid) {
$cookieParameters = $this->session->getCookieParameters();

Expand Down
30 changes: 15 additions & 15 deletions src/User/User.php
Expand Up @@ -20,6 +20,19 @@ class User
private const SESSION_AUTH_EXPIRE = '__auth_expire';
private const SESSION_AUTH_ABSOLUTE_EXPIRE = '__auth_absolute_expire';

/**
* @var int|null the number of seconds in which the user will be logged out automatically in case of
* remaining inactive. If this property is not set, the user will be logged out after
* the current session expires.
*/
public ?int $authTimeout = null;

/**
* @var int|null the number of seconds in which the user will be logged out automatically
* regardless of activity.
*/
public ?int $absoluteAuthTimeout = null;

private IdentityRepositoryInterface $identityRepository;
private EventDispatcherInterface $eventDispatcher;

Expand All @@ -44,19 +57,6 @@ public function __construct(
$this->session = $session;
}

/**
* @var int|null the number of seconds in which the user will be logged out automatically if he
* remains inactive. If this property is not set, the user will be logged out after
* the current session expires.
*/
public ?int $authTimeout = null;

/**
* @var int|null the number of seconds in which the user will be logged out automatically
* regardless of activity.
*/
public ?int $absoluteAuthTimeout = null;

public function setAccessChecker(AccessCheckerInterface $accessChecker): void
{
$this->accessChecker = $accessChecker;
Expand All @@ -72,7 +72,7 @@ public function setAccessChecker(AccessCheckerInterface $accessChecker): void
* @see logout()
* @see login()
*/
public function getIdentity($autoRenew = true): IdentityInterface
public function getIdentity(bool $autoRenew = true): IdentityInterface
{
if ($this->identity !== null) {
return $this->identity;
Expand Down Expand Up @@ -151,7 +151,7 @@ public function loginByAccessToken(string $token, string $type = null): ?Identit
* @return bool whether the user is logged out
* @throws \Throwable
*/
public function logout($destroySession = true): bool
public function logout(bool $destroySession = true): bool
{
$identity = $this->getIdentity();
if ($this->isGuest()) {
Expand Down

0 comments on commit ae3a295

Please sign in to comment.