Skip to content

Commit c95912e

Browse files
committed
fix: auto-routing legacy with $routes->add() behavior
If you $routes->add(), the controller was inaccessible with auto-routing legacy.
1 parent 6cedbe9 commit c95912e

3 files changed

Lines changed: 29 additions & 26 deletions

File tree

phpstan-baseline.neon.dist

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,16 +197,11 @@ parameters:
197197

198198
-
199199
message: "#^Call to an undefined method CodeIgniter\\\\Router\\\\RouteCollectionInterface\\:\\:getRegisteredControllers\\(.*\\)\\.$#"
200-
count: 2
201-
path: system/Router/Router.php
202-
203-
-
204-
message: "#^Expression on left side of \\?\\? is not nullable\\.$#"
205200
count: 1
206201
path: system/Router/Router.php
207202

208203
-
209-
message: "#^Method CodeIgniter\\\\Router\\\\RouteCollectionInterface\\:\\:getRoutes\\(\\) invoked with 1 parameter, 0 required\\.$#"
204+
message: "#^Expression on left side of \\?\\? is not nullable\\.$#"
210205
count: 1
211206
path: system/Router/Router.php
212207

system/Router/AutoRouter.php

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace CodeIgniter\Router;
1313

14+
use Closure;
1415
use CodeIgniter\Exceptions\PageNotFoundException;
1516

1617
/**
@@ -19,11 +20,11 @@
1920
final class AutoRouter implements AutoRouterInterface
2021
{
2122
/**
22-
* List of controllers registered for the CLI verb that should not be accessed in the web.
23+
* List of CLI routes.
2324
*
24-
* @var class-string[]
25+
* @var array<string, Closure|string> [routeKey => handler]
2526
*/
26-
private array $protectedControllers;
27+
private array $cliRoutes;
2728

2829
/**
2930
* Sub-directory that contains the requested controller class.
@@ -58,17 +59,17 @@ final class AutoRouter implements AutoRouterInterface
5859
private string $defaultNamespace;
5960

6061
public function __construct(
61-
array $protectedControllers,
62+
array $cliRoutes,
6263
string $defaultNamespace,
6364
string $defaultController,
6465
string $defaultMethod,
6566
bool $translateURIDashes,
6667
string $httpVerb
6768
) {
68-
$this->protectedControllers = $protectedControllers;
69-
$this->defaultNamespace = $defaultNamespace;
70-
$this->translateURIDashes = $translateURIDashes;
71-
$this->httpVerb = $httpVerb;
69+
$this->cliRoutes = $cliRoutes;
70+
$this->defaultNamespace = $defaultNamespace;
71+
$this->translateURIDashes = $translateURIDashes;
72+
$this->httpVerb = $httpVerb;
7273

7374
$this->controller = $defaultController;
7475
$this->method = $defaultMethod;
@@ -126,18 +127,24 @@ public function getRoute(string $uri, string $httpVerb): array
126127
$controller .= $controllerName;
127128

128129
$controller = strtolower($controller);
129-
130-
foreach ($this->protectedControllers as $controllerInRoute) {
131-
if (! is_string($controllerInRoute)) {
132-
continue;
133-
}
134-
if (strtolower($controllerInRoute) !== $controller) {
135-
continue;
130+
$methodName = strtolower($this->methodName());
131+
132+
foreach ($this->cliRoutes as $handler) {
133+
if (is_string($handler)) {
134+
$handler = strtolower($handler);
135+
136+
if (strpos($handler, $controller . '::' . $methodName) === 0) {
137+
throw new PageNotFoundException(
138+
'Cannot access CLI Route: ' . $uri
139+
);
140+
}
141+
142+
if ($handler === $controller) {
143+
throw new PageNotFoundException(
144+
'Cannot access CLI Route: ' . $uri
145+
);
146+
}
136147
}
137-
138-
throw new PageNotFoundException(
139-
'Cannot access the controller in a CLI Route. Controller: ' . $controllerInRoute
140-
);
141148
}
142149
}
143150

system/Router/Router.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public function __construct(RouteCollectionInterface $routes, ?Request $request
145145
);
146146
} else {
147147
$this->autoRouter = new AutoRouter(
148-
$this->collection->getRegisteredControllers('cli'),
148+
$this->collection->getRoutes('cli'), // @phpstan-ignore-line
149149
$this->collection->getDefaultNamespace(),
150150
$this->collection->getDefaultController(),
151151
$this->collection->getDefaultMethod(),
@@ -393,6 +393,7 @@ public function getLocale()
393393
*/
394394
protected function checkRoutes(string $uri): bool
395395
{
396+
// @phpstan-ignore-next-line
396397
$routes = $this->collection->getRoutes($this->collection->getHTTPVerb());
397398

398399
// Don't waste any time

0 commit comments

Comments
 (0)