fix: [BC] feature testing may use incorrect HTTP verb for auto routing improved

Fixes AutoRouterInterface::getRoute().
This commit is contained in:
kenjis 2023-06-06 13:04:10 +09:00
parent d4bcffdd77
commit 7dbf0d1c10
No known key found for this signature in database
GPG Key ID: BD254878922AF198
4 changed files with 17 additions and 12 deletions

View File

@ -80,7 +80,7 @@ final class AutoRouter implements AutoRouterInterface
*
* @return array [directory_name, controller_name, controller_method, params]
*/
public function getRoute(string $uri): array
public function getRoute(string $uri, string $httpVerb): array
{
$segments = explode('/', $uri);

View File

@ -60,8 +60,10 @@ final class AutoRouterImproved implements AutoRouterInterface
/**
* HTTP verb for the request.
*
* @deprecated No longer used.
*/
private string $httpVerb;
private string $httpVerb; // @phpstan-ignore-line
/**
* The namespace for controllers.
@ -74,15 +76,17 @@ final class AutoRouterImproved implements AutoRouterInterface
private string $defaultController;
/**
* The name of the default method
* The name of the default method without HTTP verb prefix.
*/
private string $defaultMethod;
/**
* @param class-string[] $protectedControllers
* @param string $defaultController Short classname
*
* @deprecated $httpVerb is deprecated. No longer used.
*/
public function __construct(
public function __construct(// @phpstan-ignore-line
array $protectedControllers,
string $namespace,
string $defaultController,
@ -93,13 +97,11 @@ final class AutoRouterImproved implements AutoRouterInterface
$this->protectedControllers = $protectedControllers;
$this->namespace = rtrim($namespace, '\\') . '\\';
$this->translateURIDashes = $translateURIDashes;
$this->httpVerb = $httpVerb;
$this->defaultController = $defaultController;
$this->defaultMethod = $httpVerb . ucfirst($defaultMethod);
$this->defaultMethod = $defaultMethod;
// Set the default values
$this->controller = $this->defaultController;
$this->method = $this->defaultMethod;
}
/**
@ -107,8 +109,11 @@ final class AutoRouterImproved implements AutoRouterInterface
*
* @return array [directory_name, controller_name, controller_method, params]
*/
public function getRoute(string $uri): array
public function getRoute(string $uri, string $httpVerb): array
{
$defaultMethod = $httpVerb . ucfirst($this->defaultMethod);
$this->method = $defaultMethod;
$segments = explode('/', $uri);
// WARNING: Directories get shifted out of the segments array.
@ -144,10 +149,10 @@ final class AutoRouterImproved implements AutoRouterInterface
$methodSegment = $this->translateURIDashes(array_shift($nonDirSegments));
// Prefix HTTP verb
$this->method = $this->httpVerb . ucfirst($methodSegment);
$this->method = $httpVerb . ucfirst($methodSegment);
// Prevent access to default method path
if (strtolower($this->method) === strtolower($this->defaultMethod)) {
if (strtolower($this->method) === strtolower($defaultMethod)) {
throw new PageNotFoundException(
'Cannot access the default method "' . $this->method . '" with the method name URI path.'
);

View File

@ -21,5 +21,5 @@ interface AutoRouterInterface
*
* @return array [directory_name, controller_name, controller_method, params]
*/
public function getRoute(string $uri): array;
public function getRoute(string $uri, string $httpVerb): array;
}

View File

@ -504,7 +504,7 @@ class Router implements RouterInterface
public function autoRoute(string $uri)
{
[$this->directory, $this->controller, $this->method, $this->params]
= $this->autoRouter->getRoute($uri);
= $this->autoRouter->getRoute($uri, $this->collection->getHTTPVerb());
}
/**