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 Psr\Http\Message\ResponseInterface;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
use Psr\Http\Server\MiddlewareInterface;
|
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract class implementing Psr\Http\Server\MiddlewareInterface.
|
|
|
|
*
|
|
|
|
* @author Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
|
|
|
|
* @package opencultureconsulting/psr15
|
|
|
|
*/
|
|
|
|
abstract class AbstractMiddleware implements MiddlewareInterface
|
|
|
|
{
|
2023-11-20 13:10:51 +01:00
|
|
|
/**
|
|
|
|
* The PSR-15 Server Request Handler.
|
|
|
|
*/
|
2023-12-17 10:54:31 +01:00
|
|
|
protected QueueRequestHandler $requestHandler;
|
2023-11-20 13:10:51 +01:00
|
|
|
|
2023-11-19 21:28:27 +01:00
|
|
|
/**
|
|
|
|
* Process an incoming server request and produce a response.
|
|
|
|
* @see MiddlewareInterface::process()
|
|
|
|
*
|
|
|
|
* @param ServerRequestInterface $request The server request to process
|
|
|
|
* @param RequestHandlerInterface $handler The request handler to delegate to
|
|
|
|
*
|
|
|
|
* @return ResponseInterface The response object
|
|
|
|
*/
|
|
|
|
final public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
|
|
|
{
|
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.
|
|
|
|
*
|
|
|
|
* @param ServerRequestInterface $request The incoming server request
|
|
|
|
*
|
|
|
|
* @return ServerRequestInterface The processed server request
|
|
|
|
*/
|
2023-12-17 09:31:24 +01:00
|
|
|
protected function processRequest(ServerRequestInterface $request): ServerRequestInterface
|
|
|
|
{
|
|
|
|
return $request;
|
|
|
|
}
|
2023-11-19 21:28:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Process an incoming response before returning it to previous middleware.
|
|
|
|
*
|
|
|
|
* @param ResponseInterface $response The incoming response
|
|
|
|
*
|
|
|
|
* @return ResponseInterface The processed response
|
|
|
|
*/
|
2023-12-17 09:31:24 +01:00
|
|
|
protected function processResponse(ResponseInterface $response): ResponseInterface
|
|
|
|
{
|
|
|
|
return $response;
|
|
|
|
}
|
2023-11-19 21:28:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allow the middleware to be invoked directly.
|
|
|
|
* @see AbstractMiddleware::process()
|
|
|
|
*
|
|
|
|
* @param ServerRequestInterface $request The server request to process
|
|
|
|
* @param RequestHandlerInterface $handler The request handler to delegate to
|
|
|
|
*
|
|
|
|
* @return ResponseInterface The response object
|
|
|
|
*/
|
|
|
|
final public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
|
|
|
{
|
|
|
|
return $this->process($request, $handler);
|
|
|
|
}
|
|
|
|
}
|