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