Skip to content

Commit

Permalink
Updating HTTP code with PHP 8.1 features (#629)
Browse files Browse the repository at this point in the history
Co-authored-by: Aleksei Gagarin <roxblnfk@ya.ru>
  • Loading branch information
msmakouz and roxblnfk authored Mar 31, 2022
1 parent 33244a6 commit 8088453
Show file tree
Hide file tree
Showing 34 changed files with 205 additions and 665 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
added return type `mixed` to the method `getContext` in `Spiral\Filters\FilterInterface` interface.
Added return type `mixed` to the method `getValue` in `Spiral\Filters\InputInterface`.
- [spiral/http] Config `Spiral\Config\JsonPayloadConfig` moved to the `Spiral\Bootloader\Http\JsonPayloadConfig`.
- [spiral/http] Added return type `array` and `mixed` parameter type of `$filler` to the method `fetch`,
added return type `mixed` to the method `offsetGet`, added return type `mixed` and `mixed` parameter type
of `$default` to the method `get` in `Spiral\Http\Request\InputBag` class.
- [spiral/http] Added return type `bool` to the method `emit` in `Spiral\Http\EmitterInterface` interface.
- [spiral/config] Added return type `void` to the method `setDefaults` in `Spiral\Config\ConfiguratorInterface` interface.
- [spiral/core] Added return type `mixed` to the method `runScope` in `Spiral\Core\ScopeInterface` interface.
- [spiral/core] Added return type `mixed` and `array|callable|string` parameter type of `$target`
Expand Down
39 changes: 15 additions & 24 deletions src/Http/src/CallableHandler.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Http;

use Closure;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
Expand All @@ -25,44 +19,41 @@ final class CallableHandler implements RequestHandlerInterface
use JsonTrait;

/** @var callable */
private $callable;
private mixed $callable;

/** @var ResponseFactoryInterface */
private $responseFactory;

public function __construct(callable $callable, ResponseFactoryInterface $responseFactory)
{
public function __construct(
callable $callable,
private readonly ResponseFactoryInterface $responseFactory
) {
$this->callable = $callable;
$this->responseFactory = $responseFactory;
}

/**
* @inheritdoc
* @psalm-suppress UnusedVariable
*/
public function handle(Request $request): Response
{
$outputLevel = ob_get_level();
ob_start();
$outputLevel = \ob_get_level();
\ob_start();

$output = $result = null;

$response = $this->responseFactory->createResponse(200);
try {
$result = ($this->callable)($request, $response);
$result = \call_user_func($this->callable, $request, $response);
} catch (\Throwable $e) {
ob_get_clean();
\ob_get_clean();
throw $e;
} finally {
while (ob_get_level() > $outputLevel + 1) {
$output = ob_get_clean() . $output;
while (\ob_get_level() > $outputLevel + 1) {
$output = \ob_get_clean() . $output;
}
}

return $this->wrapResponse(
$response,
$result,
ob_get_clean() . $output
\ob_get_clean() . $output
);
}

Expand All @@ -73,7 +64,7 @@ public function handle(Request $request): Response
* @param mixed $result Generated endpoint output.
* @param string $output Buffer output.
*/
private function wrapResponse(Response $response, $result = null, string $output = ''): Response
private function wrapResponse(Response $response, mixed $result = null, string $output = ''): Response
{
if ($result instanceof Response) {
if (!empty($output) && $result->getBody()->isWritable()) {
Expand All @@ -83,7 +74,7 @@ private function wrapResponse(Response $response, $result = null, string $output
return $result;
}

if (is_array($result) || $result instanceof \JsonSerializable) {
if (\is_array($result) || $result instanceof \JsonSerializable) {
$response = $this->writeJson($response, $result);
} else {
$response->getBody()->write((string)$result);
Expand Down
7 changes: 0 additions & 7 deletions src/Http/src/Config/HttpConfig.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Http\Config;
Expand Down
16 changes: 5 additions & 11 deletions src/Http/src/Emitter/SapiEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ final class SapiEmitter implements EmitterInterface
*
* Emits the status line and headers via the header() function, and the
* body content via the output buffer.
*
* @param ResponseInterface $response
*/
public function emit(ResponseInterface $response): bool
{
Expand Down Expand Up @@ -64,11 +62,11 @@ private function emitBody(ResponseInterface $response): void
*/
private function assertNoPreviousOutput(): void
{
if (headers_sent()) {
if (\headers_sent()) {
throw new EmitterException('Unable to emit response, headers already send.');
}

if (ob_get_level() > 0 && ob_get_length() > 0) {
if (\ob_get_level() > 0 && \ob_get_length() > 0) {
throw new EmitterException('Unable to emit response, found non closed buffered output.');
}
}
Expand All @@ -82,15 +80,13 @@ private function assertNoPreviousOutput(): void
* It is important to mention that this method should be called after
* `emitHeaders()` in order to prevent PHP from changing the status code of
* the emitted response.
*
* @param ResponseInterface $response
*/
private function emitStatusLine(ResponseInterface $response): void
{
$reasonPhrase = $response->getReasonPhrase();
$statusCode = $response->getStatusCode();

header(sprintf(
\header(\sprintf(
'HTTP/%s %d%s',
$response->getProtocolVersion(),
$statusCode,
Expand All @@ -105,8 +101,6 @@ private function emitStatusLine(ResponseInterface $response): void
* is an array with multiple values, ensures that each is sent
* in such a way as to create aggregate headers (instead of replace
* the previous).
*
* @param ResponseInterface $response
*/
private function emitHeaders(ResponseInterface $response): void
{
Expand All @@ -116,7 +110,7 @@ private function emitHeaders(ResponseInterface $response): void
$name = $this->filterHeader($header);
$first = $name === 'Set-Cookie' ? false : true;
foreach ($values as $value) {
header(sprintf(
\header(\sprintf(
'%s: %s',
$name,
$value
Expand All @@ -131,6 +125,6 @@ private function emitHeaders(ResponseInterface $response): void
*/
private function filterHeader(string $header): string
{
return ucwords($header, '-');
return \ucwords($header, '-');
}
}
9 changes: 1 addition & 8 deletions src/Http/src/EmitterInterface.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Http;
Expand All @@ -18,5 +11,5 @@ interface EmitterInterface
/**
* Emit response to the user.
*/
public function emit(ResponseInterface $response);
public function emit(ResponseInterface $response): bool;
}
16 changes: 0 additions & 16 deletions src/Http/src/Exception/AcceptHeaderException.php

This file was deleted.

11 changes: 1 addition & 10 deletions src/Http/src/Exception/ClientException.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Http\Exception;
Expand All @@ -27,8 +20,6 @@ class ClientException extends HttpException

/**
* Code and message positions are reverted.
*
* @param int $code
*/
public function __construct(?int $code = null, string $message = '', ?\Throwable $previous = null)
{
Expand All @@ -37,7 +28,7 @@ public function __construct(?int $code = null, string $message = '', ?\Throwable
}

if (empty($message)) {
$message = "Http Error - {$code}";
$message = \sprintf('Http Error - %s', $code);
}

parent::__construct($message, $code, $previous);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Http\Exception\ClientException;
Expand Down
7 changes: 0 additions & 7 deletions src/Http/src/Exception/ClientException/ForbiddenException.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Http\Exception\ClientException;
Expand Down
7 changes: 0 additions & 7 deletions src/Http/src/Exception/ClientException/NotFoundException.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Http\Exception\ClientException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Http\Exception\ClientException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Http\Exception\ClientException;
Expand Down
7 changes: 0 additions & 7 deletions src/Http/src/Exception/DotNotFoundException.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Http\Exception;
Expand Down
7 changes: 0 additions & 7 deletions src/Http/src/Exception/EmitterException.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Http\Exception;
Expand Down
7 changes: 0 additions & 7 deletions src/Http/src/Exception/HttpException.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Http\Exception;
Expand Down
7 changes: 0 additions & 7 deletions src/Http/src/Exception/InputException.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Http\Exception;
Expand Down
7 changes: 0 additions & 7 deletions src/Http/src/Exception/PipelineException.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Http\Exception;
Expand Down
7 changes: 0 additions & 7 deletions src/Http/src/Exception/ResponseException.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Http\Exception;
Expand Down
Loading

0 comments on commit 8088453

Please sign in to comment.