|
11 | 11 |
|
12 | 12 | namespace CodeIgniter\Router; |
13 | 13 |
|
| 14 | +use Closure; |
14 | 15 | use CodeIgniter\Exceptions\PageNotFoundException; |
15 | 16 |
|
16 | 17 | /** |
|
19 | 20 | final class AutoRouter implements AutoRouterInterface |
20 | 21 | { |
21 | 22 | /** |
22 | | - * List of controllers registered for the CLI verb that should not be accessed in the web. |
| 23 | + * List of CLI routes that do not contain '*' routes. |
23 | 24 | * |
24 | | - * @var class-string[] |
| 25 | + * @var array<string, Closure|string> [routeKey => handler] |
25 | 26 | */ |
26 | | - private array $protectedControllers; |
| 27 | + private array $cliRoutes; |
27 | 28 |
|
28 | 29 | /** |
29 | 30 | * Sub-directory that contains the requested controller class. |
@@ -58,17 +59,17 @@ final class AutoRouter implements AutoRouterInterface |
58 | 59 | private string $defaultNamespace; |
59 | 60 |
|
60 | 61 | public function __construct( |
61 | | - array $protectedControllers, |
| 62 | + array $cliRoutes, |
62 | 63 | string $defaultNamespace, |
63 | 64 | string $defaultController, |
64 | 65 | string $defaultMethod, |
65 | 66 | bool $translateURIDashes, |
66 | 67 | string $httpVerb |
67 | 68 | ) { |
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; |
72 | 73 |
|
73 | 74 | $this->controller = $defaultController; |
74 | 75 | $this->method = $defaultMethod; |
@@ -126,18 +127,31 @@ public function getRoute(string $uri, string $httpVerb): array |
126 | 127 | $controller .= $controllerName; |
127 | 128 |
|
128 | 129 | $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 | + // Like $routes->cli('hello/(:segment)', 'Home::$1') |
| 137 | + if (strpos($handler, '::$') !== false) { |
| 138 | + throw new PageNotFoundException( |
| 139 | + 'Cannot access CLI Route: ' . $uri |
| 140 | + ); |
| 141 | + } |
| 142 | + |
| 143 | + if (strpos($handler, $controller . '::' . $methodName) === 0) { |
| 144 | + throw new PageNotFoundException( |
| 145 | + 'Cannot access CLI Route: ' . $uri |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + if ($handler === $controller) { |
| 150 | + throw new PageNotFoundException( |
| 151 | + 'Cannot access CLI Route: ' . $uri |
| 152 | + ); |
| 153 | + } |
136 | 154 | } |
137 | | - |
138 | | - throw new PageNotFoundException( |
139 | | - 'Cannot access the controller in a CLI Route. Controller: ' . $controllerInRoute |
140 | | - ); |
141 | 155 | } |
142 | 156 | } |
143 | 157 |
|
|
0 commit comments