diff options
| author | fox <fox@fakecake.org> | 2026-05-28 13:27:53 +0300 |
|---|---|---|
| committer | fox <fox@fakecake.org> | 2026-05-28 13:27:53 +0300 |
| commit | fd09747bbdb3d5995e9f93e548cab340745d535a (patch) | |
| tree | b8010df4fd2e28d11d0c979f16c3c476560c5d93 /classes/Router.php | |
| parent | 62a2c2e96e983d479897ebe5b7722b49b13a5180 (diff) | |
| parent | 3ba4ede4952059906a15d6a89a2648aa8aef676e (diff) | |
| download | the-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.php | 30 |
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); } } |
