about summary refs log tree commit diff stats
path: root/classes/Router.php
diff options
context:
space:
mode:
authorfox <fox@fakecake.org>2026-05-28 13:27:53 +0300
committerfox <fox@fakecake.org>2026-05-28 13:27:53 +0300
commitfd09747bbdb3d5995e9f93e548cab340745d535a (patch)
treeb8010df4fd2e28d11d0c979f16c3c476560c5d93 /classes/Router.php
parent62a2c2e96e983d479897ebe5b7722b49b13a5180 (diff)
parent3ba4ede4952059906a15d6a89a2648aa8aef676e (diff)
downloadthe-epube-fd09747bbdb3d5995e9f93e548cab340745d535a.tar.gz
the-epube-fd09747bbdb3d5995e9f93e548cab340745d535a.tar.xz
Merge branch 'dev' into 'master'
Dev

See merge request git/the-epube!43
Diffstat (limited to 'classes/Router.php')
-rw-r--r--classes/Router.php30
1 files changed, 20 insertions, 10 deletions
diff --git a/classes/Router.php b/classes/Router.php
index bae01ec..d5d4349 100644
--- a/classes/Router.php
+++ b/classes/Router.php
@@ -18,25 +18,37 @@ class Router
 		$this->middleware[] = $middleware;
 	}
 
-	public function dispatch(string $op): Response
+	/**
+	 * Dispatch an operation and send the response.
+	 * Calls exit after sending the response.
+	 */
+	public function dispatch(string $op): void
+	{
+		$response = $this->dispatchResponse($op);
+		$response->send();
+		exit;
+	}
+
+	/**
+	 * Get the Response for a dispatch without sending it.
+	 * Used for testing and for cases where the caller
+	 * wants to modify the response before sending.
+	 */
+	public function dispatchResponse(string $op): Response
 	{
 		$handler = $this->routes[$op] ?? null;
 
 		if (!$handler) {
-			$response = (new Response())
+			return (new Response())
 				->withStatus(404)
 				->withBody(json_encode(["error" => "Method not found"]));
-			$response->send();
-			exit;
 		}
 
 		// Check auth for handlers that require it
 		if ($handler instanceof AuthRequired && empty($_SESSION["owner"])) {
-			$response = (new Response())
+			return (new Response())
 				->withStatus(401)
 				->withBody(json_encode(["error" => "Unauthorized"]));
-			$response->send();
-			exit;
 		}
 
 		$pipe = $handler->handle(...);
@@ -50,8 +62,6 @@ class Router
 		$request = new Request();
 		$request = $request->withAttribute('op', $op);
 
-		$response = $pipe($request);
-		$response->send();
-		exit;
+		return $pipe($request);
 	}
 }