Total Complexity | 7 |
Total Lines | 48 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
16 | final class Router |
||
17 | { |
||
18 | /** |
||
19 | * @throws ReflectionException |
||
20 | */ |
||
21 | 79 | public static function configure(Closure $fn): void |
|
22 | { |
||
23 | 79 | $routes = new Routes(); |
|
24 | 79 | $bindings = new Bindings(); |
|
25 | 79 | $handlers = new Handlers(); |
|
26 | |||
27 | 79 | $params = array_map(static fn ($param) => match ((string)$param->getType()) { |
|
28 | 79 | Routes::class => $routes, |
|
29 | 79 | Bindings::class => $bindings, |
|
30 | 79 | Handlers::class => $handlers, |
|
31 | 79 | default => null, |
|
32 | 79 | }, (new ReflectionFunction($fn))->getParameters()); |
|
33 | |||
34 | 79 | $fn(...$params); |
|
35 | |||
36 | try { |
||
37 | 78 | echo self::findRoute($routes)->run($bindings); |
|
38 | 2 | } catch (Exception $exception) { |
|
39 | 2 | echo self::handleException($handlers, $exception); |
|
40 | } |
||
41 | } |
||
42 | |||
43 | 78 | private static function findRoute(Routes $routes): Route |
|
44 | { |
||
45 | 78 | foreach ($routes->getAllRoutes() as $route) { |
|
46 | 77 | if ($route->requestMatches()) { |
|
47 | 72 | return $route; |
|
48 | } |
||
49 | } |
||
50 | |||
51 | 6 | return new Route('', '/', NotFound404Controller::class); |
|
52 | } |
||
53 | |||
54 | 2 | private static function handleException(Handlers $handlers, Exception $exception): string |
|
64 | } |
||
65 | } |
||
66 |