From f61c5e09ffc1086ca3895c315f65030b1c7fe57c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Thu, 19 May 2022 16:01:42 +0200 Subject: [PATCH] Add \wcf\http\Pipeline and \wcf\http\RequestHandlerMiddleware --- .../install/files/lib/http/Pipeline.class.php | 47 +++++++++++++++++++ .../http/RequestHandlerMiddleware.class.php | 39 +++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 wcfsetup/install/files/lib/http/Pipeline.class.php create mode 100644 wcfsetup/install/files/lib/http/RequestHandlerMiddleware.class.php 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); + } +} -- 2.20.1