From c14579871fa1241713128a2d0d5514af004e3371 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Sun, 10 Aug 2025 10:46:41 +0200 Subject: initial commit --- src/Router.php | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/Router.php (limited to 'src/Router.php') diff --git a/src/Router.php b/src/Router.php new file mode 100644 index 0000000..deb852e --- /dev/null +++ b/src/Router.php @@ -0,0 +1,52 @@ +routes = new RouteCollection(); + $this->configurator = new RouteConfigurator($this->routes, $this->routes); + + $this->addRoutes(); + } + + public function run(): Response + { + $request = Request::createFromGlobals(); + + $context = new RequestContext(); + $context->fromRequest($request); + + try { + $matcher = new UrlMatcher($this->routes, $context); + $match = $matcher->matchRequest($request); + + $class = $match["_controller"][0]; + $method = $match["_controller"][1]; + + return (new $class)->$method(); + } catch (\Exception $exception) { + return new Response("500: " . $exception->getMessage(), 500); + } + } + + private function addRoutes(): void + { + $routes = include_once(__DIR__ . "/routes.php"); + $routes($this->configurator); + } +} -- cgit v1.2.3