From 4c7bde1400820f36caf8b2a5374007384c3018f3 Mon Sep 17 00:00:00 2001 From: winter Date: Mon, 9 Dec 2024 21:31:54 +0000 Subject: rename to Digitigrade / PawPub :3 --- Digitigrade/Router.php | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 Digitigrade/Router.php (limited to 'Digitigrade/Router.php') diff --git a/Digitigrade/Router.php b/Digitigrade/Router.php new file mode 100644 index 0000000..fb984d5 --- /dev/null +++ b/Digitigrade/Router.php @@ -0,0 +1,98 @@ +requestPath = $_GET['requestPath'] ?? $_SERVER['PATH_INFO'] ?? explode('?', $_SERVER['REQUEST_URI'], 2)[0] ?? '/'; + $this->routes = []; + } + + /** + * Processes and sends a response to the current request + * @return void + */ + public function processRequest() { + try { + $this->processRequestInner(); + } catch (HttpResponseStatus $e) { + http_response_code($e->httpCode()); + echo '
' . $e->getMessage() . ''; + } + } + + private function processRequestInner() { + $context = null; + try { + $route = $this->matchPath($this->requestPath); + if ($route == null) { + throw new NoHandlerFound($this->requestPath); + } + $context = 'handler for /' . implode('/', $route[0]); + $route[1]($route[2]); // call handler with args + } catch (\Exception $e) { + if (is_a($e, HttpResponseStatus::class)) { + throw $e; + } + throw new InternalServerError($e, $context); + } catch (\Error $e) { + throw new InternalServerError($e, $context); + } + } + + public function mount(string $pathSpec, callable $handler) { + $this->routes[] = [$this->explodePathPieces($pathSpec), $handler]; + } + + private function explodePathPieces(string $pathSpec): array { + $pieces = explode('/', $pathSpec); + if ($pieces[0] == '') + array_shift($pieces); + return $pieces; + } + + private function matchPath(string $requestPath): ?array { + // find matching candidates + $pieces = $this->explodePathPieces($requestPath); + $candidates = []; + foreach ($this->routes as $route) { + $routePieces = $route[0]; + if (count($routePieces) != count($pieces)) + continue; + $args = []; + $matches = true; + for ($i = 0; $i < count($pieces); $i++) { + if (str_starts_with($routePieces[$i], ':')) { // this is a parameter + $args[substr($routePieces[$i], 1)] = $pieces[$i]; + } elseif ($routePieces[$i] != $pieces[$i]) { + $matches = false; + break; + } + } + if ($matches) { + $candidates[] = [$route[0], $route[1], $args]; + } + } + + // select the best matching one (has the longest path, for now) + $bestScore = 0; + $bestItem = null; + foreach ($candidates as $c) { + $score = count($c[0]) - count(array_filter($c[0], function ($item) { + return str_starts_with($item, ':'); + })) * 0.1; + if ($score > $bestScore) { + $bestItem = $c; + $bestScore = $score; + } + } + return $bestItem; + } + +} -- cgit v1.3