Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions Slim/Routing/FastRouteDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
class FastRouteDispatcher extends GroupCountBased
{
/**
* @var string[][]
* @var string|null
*/
private ?string $allowedMethodsUri = null;

/**
* @var string[]
*/
private array $allowedMethods = [];

Expand Down Expand Up @@ -86,8 +91,8 @@ private function routingResults(string $httpMethod, string $uri): array
*/
public function getAllowedMethods(string $uri): array
{
if (isset($this->allowedMethods[$uri])) {
return $this->allowedMethods[$uri];
if ($this->allowedMethodsUri === $uri) {
return $this->allowedMethods;
}

$allowedMethods = [];
Expand All @@ -104,6 +109,8 @@ public function getAllowedMethods(string $uri): array
}
}

return $this->allowedMethods[$uri] = array_keys($allowedMethods);
$this->allowedMethodsUri = $uri;

return $this->allowedMethods = array_keys($allowedMethods);
}
}
15 changes: 15 additions & 0 deletions tests/Routing/FastRouteDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,21 @@ public function testGetAllowedMethods($method, $uri, $callback, $allowedMethods)
$this->assertSame($results, $allowedMethods);
}

public function testGetAllowedMethodsReusesLastUriOnly()
{
/** @var FastRouteDispatcher $dispatcher */
$dispatcher = simpleDispatcher(function (RouteCollector $r) {
$r->addRoute('GET', '/user', 'handler0');
$r->addRoute('POST', '/post', 'handler1');
}, $this->generateDispatcherOptions());

$this->assertSame(['GET'], $dispatcher->getAllowedMethods('/user'));
$this->assertSame(['POST'], $dispatcher->getAllowedMethods('/post'));

// Repeating the first URI recomputes, as only the last result is kept.
$this->assertSame(['GET'], $dispatcher->getAllowedMethods('/user'));
}

public function testDuplicateVariableNameError()
{
$this->expectException(BadRouteException::class);
Expand Down
Loading