Skip to content

Commit

Permalink
Merge pull request #98 from utopia-php/feat-trailing-wildcards-patch
Browse files Browse the repository at this point in the history
feat: add trailing wildcards
  • Loading branch information
TorstenDittmann authored Jun 2, 2023
2 parents feb8941 + a928620 commit a2fb128
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,14 +407,18 @@ 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 ??= $path;

switch ($method) {
case App::REQUEST_METHOD_GET:
$expected = App::get($path);
Expand Down

0 comments on commit a2fb128

Please sign in to comment.