Skip to content

Commit

Permalink
improve api
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Feb 4, 2025
1 parent 76a1516 commit 026cf08
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<<STRING
Expand Down Expand Up @@ -111,15 +115,18 @@ function getPath(string $path, string|BindInterface ...$bind): string
/**
* Creates Route binding.
*
* `$bind` examples:
*
* ```php
* GET: MyController::class,
* POST: 'my-view.twig',
* PATCH: bind(...),
* ```
*
* @param string $path Route path.
* @param string $name If not provided it will be same as the route path.
* @param null|MiddlewaresInterface|MiddlewareNameInterface|class-string<MiddlewareInterface> $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,
Expand All @@ -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) {
Expand All @@ -143,25 +160,19 @@ function route(
)
);
}
$isBind = $item instanceof BindInterface;
$itemView = $isBind
? $item->view()
: '';
/** @var MethodInterface $object */
$object = new $method(); // @phpstan-ignore-line
$middlewares = match (true) {
$middleware instanceof MiddlewaresInterface => $middleware,
$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);
}
Expand Down

0 comments on commit 026cf08

Please sign in to comment.