diff options
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); } } |
