Skip to content

Commit f1b0307

Browse files
committed
Router::toUri: update hard-coded uri heuristic, no longer ignores reflection exceptions
1 parent 215671f commit f1b0307

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

packages/router/src/GenericRouter.php

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -131,27 +131,20 @@ private function getCallable(MatchedRoute $matchedRoute): HttpMiddlewareCallable
131131

132132
public function toUri(array|string $action, ...$params): string
133133
{
134-
try {
135-
if (is_array($action)) {
136-
$controllerClass = $action[0];
137-
$reflection = new ClassReflector($controllerClass);
138-
$controllerMethod = $reflection->getMethod($action[1]);
139-
} else {
140-
$controllerClass = $action;
141-
$reflection = new ClassReflector($controllerClass);
142-
$controllerMethod = $reflection->getMethod('__invoke');
143-
}
134+
if (is_string($action) && str_starts_with($action, '/')) {
135+
$uri = $action;
136+
} else {
137+
[$controllerClass, $controllerMethod] = is_array($action) ? $action : [$action, '__invoke'];
144138

145-
/** @var Route|null $routeAttribute */
146-
$routeAttribute = $controllerMethod->getAttribute(Route::class);
139+
$routeAttribute = new ClassReflector($controllerClass)
140+
->getMethod($controllerMethod)
141+
->getAttribute(Route::class);
147142

148-
$uri = $routeAttribute->uri;
149-
} catch (ReflectionException) {
150-
if (is_array($action)) {
151-
throw new InvalidRouteException($action[0], $action[1]);
143+
if ($routeAttribute === null) {
144+
throw new InvalidRouteException($controllerClass, $controllerMethod);
152145
}
153146

154-
$uri = $action;
147+
$uri = $routeAttribute->uri;
155148
}
156149

157150
$uri = str($uri);

0 commit comments

Comments
 (0)