From 5eed77aa5112341a382aa707b2309c35c935aff8 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Thu, 1 Jun 2023 16:42:47 +0200 Subject: [PATCH] feat: trailing wildcards --- src/App.php | 9 ++++++--- tests/AppTest.php | 6 ++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/App.php b/src/App.php index c9a3c3c5..04252cd2 100755 --- a/src/App.php +++ b/src/App.php @@ -510,9 +510,12 @@ public function match(Request $request, bool $fresh = false): ?Route continue; } - // Check the paths have the same amount of segments - if (\substr_count($routeUrl, '/') !== \substr_count($url, '/')) { - continue; + // check for trailing wildcard + if (!str_ends_with($routeUrl, '/*')) { + // Check the paths have the same amount of segments + if (\substr_count($routeUrl, '/') !== \substr_count($url, '/')) { + continue; + } } \array_shift($this->matches); diff --git a/tests/AppTest.php b/tests/AppTest.php index 8c14567d..75c8fa31 100755 --- a/tests/AppTest.php +++ b/tests/AppTest.php @@ -407,15 +407,17 @@ public function providerRouteMatching(): array '1 separators' => [App::REQUEST_METHOD_GET, '/a/'], '2 separators' => [App::REQUEST_METHOD_GET, '/a/b'], '3 separators' => [App::REQUEST_METHOD_GET, '/a/b/c'], + 'trailing wildcard' => [App::REQUEST_METHOD_GET, '/wildcard/*', '/wildcard/lorem/ipsum'], + 'trailing wildcard - root' => [App::REQUEST_METHOD_GET, '/wildcard/*', '/wildcard'], ]; } /** * @dataProvider providerRouteMatching */ - public function testCanMatchRoute(string $method, string $path): void + public function testCanMatchRoute(string $method, string $path, string $expected = null): void { - $expected = ''; + $expected ??= $path; switch ($method) { case App::REQUEST_METHOD_GET: