Add \wcf\http\Pipeline and \wcf\http\RequestHandlerMiddleware
authorTim Düsterhus <duesterhus@woltlab.com>
Thu, 19 May 2022 14:01:42 +0000 (16:01 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Thu, 19 May 2022 14:20:36 +0000 (16:20 +0200)
wcfsetup/install/files/lib/http/Pipeline.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/http/RequestHandlerMiddleware.class.php [new file with mode: 0644]

diff --git a/wcfsetup/install/files/lib/http/Pipeline.class.php b/wcfsetup/install/files/lib/http/Pipeline.class.php
new file mode 100644 (file)
index 0000000..cbc2ad1
--- /dev/null
@@ -0,0 +1,47 @@
+<?php
+
+namespace wcf\http;
+
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Server\MiddlewareInterface;
+use Psr\Http\Server\RequestHandlerInterface;
+
+/**
+ * Represents a middleware pipeline that sends the ServerRequestInterface
+ * through all MiddlewareInterfaces in the order they were given. The last
+ * MiddlewareInterface will be connected to the RequestHandlerInterface.
+ *
+ * @author  Tim Duesterhus
+ * @copyright   2001-2022 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @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 (file)
index 0000000..2311417
--- /dev/null
@@ -0,0 +1,39 @@
+<?php
+
+namespace wcf\http;
+
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Server\MiddlewareInterface;
+use Psr\Http\Server\RequestHandlerInterface;
+
+/**
+ * Connects the given RequestHandlerInterface to the given MiddlewareInterface to transform the
+ * MiddlewareInterface into a RequestHandlerInterface.
+ *
+ * @author  Tim Duesterhus
+ * @copyright   2001-2022 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @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);
+    }
+}