Skip to content

Commit

Permalink
fix: [BC] feature testing may use incorrect HTTP verb for auto routin…
Browse files Browse the repository at this point in the history
…g improved

Fixes AutoRouterInterface::getRoute().
  • Loading branch information
kenjis committed Jun 6, 2023
1 parent b9747ea commit 7ffb6fd
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
2 changes: 1 addition & 1 deletion system/Router/AutoRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function __construct(
*
* @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);

Expand Down
23 changes: 14 additions & 9 deletions system/Router/AutoRouterImproved.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand All @@ -93,22 +97,23 @@ public function __construct(
$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;
}

/**
* Finds controller, method and params from the URI.
*
* @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.
Expand Down Expand Up @@ -144,10 +149,10 @@ public function getRoute(string $uri): array
$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.'
);
Expand Down
2 changes: 1 addition & 1 deletion system/Router/AutoRouterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion system/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ protected function checkRoutes(string $uri): bool
public function autoRoute(string $uri)
{
[$this->directory, $this->controller, $this->method, $this->params]
= $this->autoRouter->getRoute($uri);
= $this->autoRouter->getRoute($uri, $this->collection->getHTTPVerb());
}

/**
Expand Down

0 comments on commit 7ffb6fd

Please sign in to comment.