From 719b5dcbaa1892129ca384067c90ba7c1c4eca9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Thu, 6 Mar 2025 11:52:03 +0100 Subject: [PATCH 1/2] Reapply "Avoid breaking change" This reverts commit 71d7e75889f8828f82fca029f1f6597048a3d4f7. --- src/Route.php | 12 +++++++++--- src/Router.php | 4 ++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/Route.php b/src/Route.php index aa06a29..6828cff 100755 --- a/src/Route.php +++ b/src/Route.php @@ -154,7 +154,7 @@ public function getHook(): bool * @param int $index * @return void */ - public function setPathParam(string $path, string $key, int $index): void + public function setPathParam(string $key, int $index, string $path = ''): void { $this->pathParams[$path][$key] = $index; } @@ -165,12 +165,18 @@ public function setPathParam(string $path, string $key, int $index): void * @param \Utopia\Request $request * @return array */ - public function getPathValues(Request $request, string $path): array + public function getPathValues(Request $request, string $path = ''): array { $pathValues = []; $parts = explode('/', ltrim($request->getURI(), '/')); - foreach (($this->pathParams[$path] ?? []) as $key => $index) { + if(empty($path)) { + $pathParams = $this->pathParams[$path] ?? $this->pathParams[0] ?? []; + } else { + $pathParams = $this->pathParams[$path] ?? []; + } + + foreach ($pathParams as $key => $index) { if (array_key_exists($index, $parts)) { $pathValues[$key] = $parts[$index]; } diff --git a/src/Router.php b/src/Router.php index bc6e63c..eb2cea8 100644 --- a/src/Router.php +++ b/src/Router.php @@ -86,7 +86,7 @@ public static function addRoute(Route $route): void } foreach ($params as $key => $index) { - $route->setPathParam($path, $key, $index); + $route->setPathParam($key, $index, $path); } self::$routes[$route->getMethod()][$path] = $route; @@ -108,7 +108,7 @@ public static function addRouteAlias(string $path, Route $route): void } foreach ($params as $key => $index) { - $route->setPathParam($alias, $key, $index); + $route->setPathParam($key, $index, $alias); } self::$routes[$route->getMethod()][$alias] = $route; From 916424c2e4a8159333c837af2de7949dfe05b6d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Thu, 6 Mar 2025 10:54:51 +0000 Subject: [PATCH 2/2] quality fix --- src/Route.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Route.php b/src/Route.php index 6828cff..714b0a7 100755 --- a/src/Route.php +++ b/src/Route.php @@ -170,8 +170,8 @@ public function getPathValues(Request $request, string $path = ''): array $pathValues = []; $parts = explode('/', ltrim($request->getURI(), '/')); - if(empty($path)) { - $pathParams = $this->pathParams[$path] ?? $this->pathParams[0] ?? []; + if (empty($path)) { + $pathParams = $this->pathParams[$path] ?? \array_values($this->pathParams)[0] ?? []; } else { $pathParams = $this->pathParams[$path] ?? []; }