Skip to content

Commit

Permalink
Merge pull request #97 from utopia-php/feat-trailing-wildcards
Browse files Browse the repository at this point in the history
feat: trailing wildcards
  • Loading branch information
TorstenDittmann committed Jun 2, 2023
2 parents bc0144f + 5eed77a commit 9b6171e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 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: 4 additions & 2 deletions tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 9b6171e

Please sign in to comment.