From 026cf0843b6544f8148a50078107ff29962ae7af Mon Sep 17 00:00:00 2001 From: Rodolfo Berrios <20590102+rodber@users.noreply.github.com> Date: Tue, 4 Feb 2025 13:28:17 -0300 Subject: [PATCH] improve api --- src/functions.php | 49 +++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/src/functions.php b/src/functions.php index 8620de7..47538f3 100644 --- a/src/functions.php +++ b/src/functions.php @@ -68,7 +68,11 @@ function getPath(string $path, string|BindInterface ...$bind): string { $routePath = new Path($path); foreach ($bind as $item) { - $controllerName = controllerName($item)->__toString(); + try { + $controllerName = controllerName($item)->__toString(); + } catch (Throwable) { + continue; + } $controllerName::assert(); foreach ($routePath->variables()->keys() as $variable) { $variableBracket = << $middleware HTTP server middleware. * @param BindInterface|string ...$bind Binding for HTTP controllers (GET, POST, PUT, DELETE, etc). - * - * $bind examples: - * GET: bind(ClassName, 'view'), - * POST: bind(ClassName, middleware: Middleware1::class,...), - * PATCH: ClassName, */ function route( string $path, @@ -131,7 +138,17 @@ function route( $path = getPath($path, ...$bind); $route = new Route(new Path($path), $name); foreach ($bind as $method => $item) { - $controllerName = controllerName($item); + if ($item instanceof BindInterface) { + $controllerName = $item->controllerName(); + } else { + try { + $controllerName = controllerName($item); + $item = bind($item, ''); + } catch (Throwable) { + $item = bind(NullController::class, $item); + $controllerName = $item->controllerName(); + } + } $httpMethod = strval($method); $method = EndpointInterface::KNOWN_METHODS[$method] ?? null; if ($method === null) { @@ -143,10 +160,6 @@ function route( ) ); } - $isBind = $item instanceof BindInterface; - $itemView = $isBind - ? $item->view() - : ''; /** @var MethodInterface $object */ $object = new $method(); // @phpstan-ignore-line $middlewares = match (true) { @@ -154,14 +167,12 @@ function route( $middleware === null => middlewares(), default => middlewares($middleware), }; - if ($item instanceof BindInterface) { - $middlewares = $middlewares->withAppend( - ...iterator_to_array( - $item->middlewares() - ) - ); - } - $bind = (new Bind($controllerName, $middlewares))->withView($itemView); + $middlewares = $middlewares->withAppend( + ...iterator_to_array( + $item->middlewares() + ) + ); + $bind = (new Bind($controllerName, $middlewares))->withView($item->view()); $endpoint = new Endpoint($object, $bind); $route = $route->withEndpoint($endpoint); }