| 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 | 81 | public static function configure(Closure $fn): void |
|
| 22 | { |
||
| 23 | 81 | $routes = new Routes(); |
|
| 24 | 81 | $bindings = new Bindings(); |
|
| 25 | 81 | $handlers = new Handlers(); |
|
| 26 | |||
| 27 | 81 | $params = array_map(static fn ($param) => match ((string)$param->getType()) { |
|
| 28 | 81 | Routes::class => $routes, |
|
| 29 | 81 | Bindings::class => $bindings, |
|
| 30 | 81 | Handlers::class => $handlers, |
|
| 31 | 81 | default => null, |
|
| 32 | 81 | }, (new ReflectionFunction($fn))->getParameters()); |
|
| 33 | |||
| 34 | 81 | $fn(...$params); |
|
| 35 | |||
| 36 | try { |
||
| 37 | 80 | echo self::findRoute($routes) |
|
| 38 | 80 | ->run($bindings); |
|
| 39 | 10 | } catch (Exception $exception) { |
|
| 40 | 10 | echo (string) self::findHandler($handlers, $exception)($exception); |
|
| 41 | } |
||
| 42 | } |
||
| 43 | |||
| 44 | 80 | private static function findRoute(Routes $routes): Route |
|
| 45 | { |
||
| 46 | 80 | foreach ($routes->getAllRoutes() as $route) { |
|
| 47 | 78 | if ($route->requestMatches()) { |
|
| 48 | 73 | return $route; |
|
| 49 | } |
||
| 50 | } |
||
| 51 | |||
| 52 | 7 | throw new NotFound404Exception(); |
|
| 53 | } |
||
| 54 | |||
| 55 | 10 | private static function findHandler(Handlers $handlers, Exception $exception): callable |
|
| 64 | } |
||
| 65 | } |
||
| 66 |