From: Tim Düsterhus Date: Thu, 19 May 2022 14:01:42 +0000 (+0200) Subject: Add \wcf\http\Pipeline and \wcf\http\RequestHandlerMiddleware X-Git-Tag: 6.0.0_Alpha_1~1266^2~9 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=f61c5e09ffc1086ca3895c315f65030b1c7fe57c;p=GitHub%2FWoltLab%2FWCF.git Add \wcf\http\Pipeline and \wcf\http\RequestHandlerMiddleware --- diff --git a/wcfsetup/install/files/lib/http/Pipeline.class.php b/wcfsetup/install/files/lib/http/Pipeline.class.php new file mode 100644 index 0000000000..cbc2ad1bc9 --- /dev/null +++ b/wcfsetup/install/files/lib/http/Pipeline.class.php @@ -0,0 +1,47 @@ + + * @package WoltLabSuite\Core\Http + * @since 5.6 + */ +final class Pipeline implements MiddlewareInterface +{ + /** + * @var MiddlewareInterface[] + */ + private array $middlewares; + + /** + * @param MiddlewareInterface[] $middlewares + */ + public function __construct(array $middlewares) + { + $this->middlewares = $middlewares; + } + + /** + * @inheritDoc + */ + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + foreach (\array_reverse($this->middlewares) as $middleware) { + $handler = new RequestHandlerMiddleware($middleware, $handler); + } + + return $handler->handle($request); + } +} diff --git a/wcfsetup/install/files/lib/http/RequestHandlerMiddleware.class.php b/wcfsetup/install/files/lib/http/RequestHandlerMiddleware.class.php new file mode 100644 index 0000000000..23114177a9 --- /dev/null +++ b/wcfsetup/install/files/lib/http/RequestHandlerMiddleware.class.php @@ -0,0 +1,39 @@ + + * @package WoltLabSuite\Core\Http + * @since 5.6 + */ +final class RequestHandlerMiddleware implements RequestHandlerInterface +{ + private RequestHandlerInterface $handler; + + private MiddlewareInterface $middleware; + + public function __construct(MiddlewareInterface $middleware, RequestHandlerInterface $handler) + { + $this->middleware = $middleware; + $this->handler = $handler; + } + + /** + * @inheritDoc + */ + public function handle(ServerRequestInterface $request): ResponseInterface + { + return $this->middleware->process($request, $this->handler); + } +}