psr-15/src/AbstractMiddleware.php

108 lines
3.4 KiB
PHP
Raw Permalink 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;
2024-03-31 19:29:57 +02:00
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as ServerRequest;
2023-11-19 21:28:27 +01:00
use Psr\Http\Server\MiddlewareInterface;
2024-03-31 19:29:57 +02:00
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
2023-11-19 21:28:27 +01:00
/**
2024-03-31 19:29:57 +02:00
* Abstract class implementing \Psr\Http\Server\MiddlewareInterface.
2023-11-19 21:28:27 +01:00
*
* @author Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
2024-03-31 18:36:06 +02:00
* @package PSR15
2023-11-19 21:28:27 +01:00
*/
abstract class AbstractMiddleware implements MiddlewareInterface
{
/**
* The PSR-15 Server Request Handler.
2024-03-31 19:29:57 +02:00
*
* @var QueueRequestHandler
*
* @internal
*/
2023-12-17 10:54:31 +01:00
protected QueueRequestHandler $requestHandler;
2023-11-19 21:28:27 +01:00
/**
* Process an incoming server request and produce a response.
*
2024-03-31 19:29:57 +02:00
* @param ServerRequest $request The server request to process
* @param RequestHandler $handler The request handler to delegate to
2023-11-19 21:28:27 +01:00
*
2024-03-31 19:29:57 +02:00
* @return Response The response object
*
* @api
2023-11-19 21:28:27 +01:00
*/
2024-03-31 19:29:57 +02:00
final public function process(ServerRequest $request, RequestHandler $handler): Response
2023-11-19 21:28:27 +01:00
{
2023-12-17 10:54:31 +01:00
/** @var QueueRequestHandler $handler */
$this->requestHandler = $handler;
2023-11-19 21:28:27 +01:00
// Manipulate request if necessary.
2023-12-17 09:31:24 +01:00
$request = $this->processRequest($request);
2023-11-19 21:28:27 +01:00
// Delegate request to next middleware and get response.
2023-12-17 09:31:24 +01:00
$response = $handler->handle($request);
2023-11-19 21:28:27 +01:00
// Manipulate response if necessary.
2023-12-17 09:31:24 +01:00
$response = $this->processResponse($response);
2023-11-19 21:28:27 +01:00
// Return response to previous middleware.
2023-12-17 09:31:24 +01:00
return $response;
2023-11-19 21:28:27 +01:00
}
/**
* Process an incoming server request before delegating to next middleware.
*
2024-03-31 19:29:57 +02:00
* @param ServerRequest $request The incoming server request
2023-11-19 21:28:27 +01:00
*
2024-03-31 19:29:57 +02:00
* @return ServerRequest The processed server request
2023-11-19 21:28:27 +01:00
*/
2024-03-31 19:29:57 +02:00
protected function processRequest(ServerRequest $request): ServerRequest
2023-12-17 09:31:24 +01:00
{
return $request;
}
2023-11-19 21:28:27 +01:00
/**
* Process an incoming response before returning it to previous middleware.
*
2024-03-31 19:29:57 +02:00
* @param Response $response The incoming response
2023-11-19 21:28:27 +01:00
*
2024-03-31 19:29:57 +02:00
* @return Response The processed response
2023-11-19 21:28:27 +01:00
*/
2024-03-31 19:29:57 +02:00
protected function processResponse(Response $response): Response
2023-12-17 09:31:24 +01:00
{
return $response;
}
2023-11-19 21:28:27 +01:00
/**
* Allow the middleware to be invoked directly.
*
2024-03-31 19:29:57 +02:00
* @param ServerRequest $request The server request to process
* @param RequestHandler $handler The request handler to delegate to
*
* @return Response The response object
2023-11-19 21:28:27 +01:00
*/
2024-03-31 19:29:57 +02:00
final public function __invoke(ServerRequest $request, RequestHandler $handler): Response
2023-11-19 21:28:27 +01:00
{
return $this->process($request, $handler);
}
}