Skip to content

Commit

Permalink
Allow wildcard (*) for "any method" definition.
Browse files Browse the repository at this point in the history
  • Loading branch information
serge-kvashnin committed Oct 6, 2023
1 parent b4b3a7d commit f878555
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 25 deletions.
74 changes: 49 additions & 25 deletions src/Pathfinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ final class Pathfinder
'OPTIONS',
'TRACE',
'PATCH',
'*',
];

private array $static;
Expand All @@ -46,6 +47,41 @@ public function __construct(array $cached = null)
$this->dynamic = $cached[1] ?? self::dynamic();
}

public function get(string $pattern, string $handler): void
{
$this->route('GET', $pattern, $handler);
}

public function post(string $pattern, string $handler): void
{
$this->route('POST', $pattern, $handler);
}

public function put(string $pattern, string $handler): void
{
$this->route('PUT', $pattern, $handler);
}

public function delete(string $pattern, string $handler): void
{
$this->route('DELETE', $pattern, $handler);
}

public function options(string $pattern, string $handler): void
{
$this->route('OPTIONS', $pattern, $handler);
}

public function patch(string $pattern, string $handler): void
{
$this->route('PATCH', $pattern, $handler);
}

public function any(string $pattern, string $handler): void
{
$this->route('*', $pattern, $handler);
}

public function route(string $method, string $pattern, string $handler): void
{
$method = strtoupper($method);
Expand Down Expand Up @@ -101,36 +137,16 @@ public function route(string $method, string $pattern, string $handler): void
$node[self::HANDLERS][$method] = $handler;
}

public function get(string $pattern, string $handler): void
{
$this->route('GET', $pattern, $handler);
}

public function post(string $pattern, string $handler): void
{
$this->route('POST', $pattern, $handler);
}

public function put(string $pattern, string $handler): void
{
$this->route('PUT', $pattern, $handler);
}

public function patch(string $pattern, string $handler): void
{
$this->route('PATCH', $pattern, $handler);
}

public function delete(string $pattern, string $handler): void
{
$this->route('DELETE', $pattern, $handler);
}

public function match(string $method, string $path): array
{
$method = strtoupper($method);
if (isset($this->static[$path])) {
if (!isset($this->static[$path][self::HANDLERS][$method])) {
// any
if (isset($this->static[$path][self::HANDLERS]['*'])) {
return [$this->static[$path][self::HANDLERS]['*'], []];
}

return [self::METHOD_NOT_ALLOWED, []];
}

Expand Down Expand Up @@ -183,6 +199,14 @@ public function match(string $method, string $path): array

if ($node[self::HANDLERS]) {
if (!isset($node[self::HANDLERS][$method])) {
// any
if (isset($node[self::HANDLERS]['*'])) {
return [
$node[self::HANDLERS]['*'],
$params,
];
}

return [self::METHOD_NOT_ALLOWED, []];
}

Expand Down
3 changes: 3 additions & 0 deletions tests/PathfinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ protected function setUp(): void
$this->pathfinder->get('/games/{slug:[\w-]+}', 'c1ee1cc6381d590c');
$this->pathfinder->get('/games/reviews', '69d82a5755545ceb');
$this->pathfinder->post('/games/reviews', 'a08cd78c88bea630');
$this->pathfinder->any('/', '1c97b75828e480bd');
}

public static function dataProvider(): Generator
Expand All @@ -54,6 +55,8 @@ public static function dataProvider(): Generator
yield 'dynamic route "GET games" with slug parameter' => ['GET', '/games/pac-man', 'c1ee1cc6381d590c', ['slug' => 'pac-man']];
yield 'static route "GET games"' => ['GET', '/games/reviews', '69d82a5755545ceb', []];
yield 'static route "POST games"' => ['POST', '/games/reviews', 'a08cd78c88bea630', []];
yield 'static route with wildcard method with "OPTIONS"' => ['OPTIONS', '/', '1c97b75828e480bd', []];
yield 'static route with wildcard method with "GET"' => ['GET', '/', '1c97b75828e480bd', []];
}

/**
Expand Down

0 comments on commit f878555

Please sign in to comment.