psr-15/src/QueueRequestHandler.php

244 lines
7.0 KiB
PHP
Raw Normal View History

2023-11-19 21:28:27 +01:00
<?php
/**
* Queue-based PSR-15 HTTP Server Request Handler
* Copyright (C) 2023 Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace OCC\PSR15;
use Exception;
use RuntimeException;
2024-03-31 19:29:57 +02:00
use GuzzleHttp\Psr7\Response as GuzzleResponse;
use GuzzleHttp\Psr7\ServerRequest as GuzzleRequest;
2023-11-19 21:28:27 +01:00
use OCC\Basics\Traits\Getter;
2024-03-31 19:29:57 +02:00
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as ServerRequest;
use Psr\Http\Server\MiddlewareInterface as Middleware;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use function array_keys;
use function count;
use function filter_var;
use function get_debug_type;
use function header;
use function headers_sent;
use function is_null;
use function sprintf;
2023-11-19 21:28:27 +01:00
/**
* A queue-based PSR-15 HTTP Server Request Handler.
*
* @author Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
2024-03-31 18:36:06 +02:00
* @package PSR15
*
* @property-read MiddlewareQueue $queue
2024-03-31 19:29:57 +02:00
* @property-read ServerRequest $request
* @property-read Response $response
2023-11-19 21:28:27 +01:00
*/
2024-03-31 19:29:57 +02:00
class QueueRequestHandler implements RequestHandler
2023-11-19 21:28:27 +01:00
{
use Getter;
/**
* The PSR-7 HTTP Server Request.
2024-03-31 19:29:57 +02:00
*
* @var ServerRequest
*
* @internal
2023-11-19 21:28:27 +01:00
*/
2024-03-31 19:29:57 +02:00
protected ServerRequest $request;
2023-11-19 21:28:27 +01:00
/**
* The queue of middlewares to process the server request.
2024-03-31 19:29:57 +02:00
*
* @var MiddlewareQueue
*
* @internal
2023-11-19 21:28:27 +01:00
*/
protected MiddlewareQueue $queue;
/**
* The PSR-7 HTTP Response.
2024-03-31 19:29:57 +02:00
*
* @var Response
*
* @internal
2023-11-19 21:28:27 +01:00
*/
2024-03-31 19:29:57 +02:00
protected Response $response;
2023-11-19 21:28:27 +01:00
/**
* Handles a request by invoking a queue of middlewares.
*
2024-03-31 19:29:57 +02:00
* @param ?ServerRequest $request The PSR-7 server request to handle
*
* @return Response A PSR-7 compatible HTTP response
2023-11-19 21:28:27 +01:00
*
2024-03-31 19:29:57 +02:00
* @api
2023-11-19 21:28:27 +01:00
*/
2024-03-31 19:29:57 +02:00
public function handle(?ServerRequest $request = null): Response
2023-11-19 21:28:27 +01:00
{
$this->request = $request ?? $this->request;
2023-11-21 23:05:39 +01:00
if (count($this->queue) > 0) {
$middleware = $this->queue->dequeue();
// It is RECOMMENDED that any application using middleware includes a
// component that catches exceptions and converts them into responses.
// This middleware SHOULD be the first component executed and wrap all
// further processing to ensure that a response is always generated.
try {
2023-11-19 21:28:27 +01:00
$this->response = $middleware->process($this->request, $this);
2024-03-31 19:29:57 +02:00
} catch (Exception $exception) {
2023-11-21 23:05:39 +01:00
$options = [
'options' => [
'default' => 500,
'min_range' => 100,
'max_range' => 599
]
];
$statusCode = filter_var($exception->getCode(), FILTER_VALIDATE_INT, $options);
2024-03-31 19:29:57 +02:00
$this->response = new GuzzleResponse(
2023-11-21 23:05:39 +01:00
$statusCode,
[],
sprintf(
'Exception thrown in middleware %s: %s',
get_debug_type($middleware),
$exception->getMessage()
)
);
2023-11-19 21:28:27 +01:00
}
}
return $this->response;
}
/**
* Return the current response to the client.
*
* @param ?int $exitCode Exit status after sending out the response or NULL to continue
*
* Must be in the range 0 to 254. The status 0 is used to terminate the program successfully.
2023-11-19 21:28:27 +01:00
*
* @return void
2024-01-08 22:53:24 +01:00
*
2024-03-31 19:29:57 +02:00
* @throws RuntimeException if headers were already sent
*
* @api
2023-11-19 21:28:27 +01:00
*/
public function respond(?int $exitCode = null): void
{
$file = 'unknown file';
$line = 0;
if (headers_sent($file, $line)) {
throw new RuntimeException(
sprintf(
'Headers already sent in %s on line %d',
$file,
$line
)
);
}
header(
sprintf(
2024-03-31 19:29:57 +02:00
'HTTP/%s %d %s',
2023-11-19 21:28:27 +01:00
$this->response->getProtocolVersion(),
$this->response->getStatusCode(),
$this->response->getReasonPhrase()
),
true
);
foreach (array_keys($this->response->getHeaders()) as $name) {
2024-03-31 19:29:57 +02:00
/** @var string $name */
2023-11-19 21:28:27 +01:00
$header = sprintf('%s: %s', $name, $this->response->getHeaderLine($name));
header($header, false);
}
echo $this->response->getBody();
if (!is_null($exitCode)) {
$options = [
'options' => [
'default' => 1,
'min_range' => 0,
'max_range' => 254
]
];
$exitCode = filter_var($exitCode, FILTER_VALIDATE_INT, $options);
2023-11-19 21:28:27 +01:00
exit($exitCode);
}
}
/**
* Magic getter method for $this->queue.
*
* @return MiddlewareQueue The queue of PSR-15 middlewares
2024-03-31 19:29:57 +02:00
*
* @internal
2023-11-19 21:28:27 +01:00
*/
2024-04-17 16:31:31 +02:00
protected function _magicGetQueue(): MiddlewareQueue
2023-11-19 21:28:27 +01:00
{
return $this->queue;
}
/**
* Magic getter method for $this->request.
*
2024-03-31 19:29:57 +02:00
* @return ServerRequest The PSR-7 server request
*
* @internal
*/
2024-03-31 19:29:57 +02:00
protected function _magicGetRequest(): ServerRequest
{
return $this->request;
}
2023-12-17 10:54:49 +01:00
/**
* Magic getter method for $this->response.
*
2024-03-31 19:29:57 +02:00
* @return Response The PSR-7 response
*
* @internal
2023-12-17 10:54:49 +01:00
*/
2024-03-31 19:29:57 +02:00
protected function _magicGetResponse(): Response
2023-12-17 10:54:49 +01:00
{
return $this->response;
}
2023-11-19 21:28:27 +01:00
/**
* Create a queue-based PSR-15 HTTP Server Request Handler.
*
2024-03-31 19:29:57 +02:00
* @param iterable<array-key, Middleware> $middlewares Initial set of middlewares
*
* @return void
2023-11-19 21:28:27 +01:00
*/
2023-11-21 23:05:39 +01:00
public function __construct(iterable $middlewares = [])
2023-11-19 21:28:27 +01:00
{
2024-03-31 19:29:57 +02:00
$this->request = GuzzleRequest::fromGlobals();
2023-11-19 21:28:27 +01:00
$this->queue = MiddlewareQueue::getInstance($middlewares);
2024-03-31 19:29:57 +02:00
$this->response = new GuzzleResponse(200);
2023-11-19 21:28:27 +01:00
}
/**
* Allow the request handler to be invoked directly.
*
2024-03-31 19:29:57 +02:00
* @param ?ServerRequest $request The PSR-7 server request to handle
*
* @return Response A PSR-7 compatible HTTP response
2023-11-19 21:28:27 +01:00
*/
2024-03-31 19:29:57 +02:00
public function __invoke(?ServerRequest $request = null): Response
2023-11-19 21:28:27 +01:00
{
return $this->handle($request);
}
}