Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: RouteCollection::getRegisteredControllers() may not return all controllers #7174

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
51 changes: 31 additions & 20 deletions system/Router/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1577,41 +1577,52 @@ public function setPrioritize(bool $enabled = true)
* Get all controllers in Route Handlers
*
* @param string|null $verb HTTP verb. `'*'` returns all controllers in any verb.
*
* @return array<int, string> controller name list
* @phpstan-return list<string>
*/
public function getRegisteredControllers(?string $verb = '*'): array
{
$routes = [];
$controllers = [];

if ($verb === '*') {
$rawRoutes = [];

foreach ($this->defaultHTTPMethods as $tmpVerb) {
$rawRoutes = array_merge($rawRoutes, $this->routes[$tmpVerb]);
}

foreach ($rawRoutes as $route) {
$key = key($route['route']);
$handler = $route['route'][$key];

$routes[$key] = $handler;
foreach ($this->routes[$tmpVerb] as $route) {
$routeKey = key($route['route']);
$controller = $this->getControllerName($route['route'][$routeKey]);
if ($controller !== null) {
$controllers[] = $controller;
}
}
}
} else {
$routes = $this->getRoutes($verb);
}

$controllers = [];

foreach ($routes as $handler) {
if (! is_string($handler)) {
continue;
foreach ($routes as $handler) {
$controller = $this->getControllerName($handler);
if ($controller !== null) {
$controllers[] = $controller;
}
}
}

[$controller] = explode('::', $handler, 2);
return array_unique($controllers);
}

$controllers[] = $controller;
/**
* @param Closure|string $handler Handler
*
* @return string|null Controller classname
*/
private function getControllerName($handler)
{
if (! is_string($handler)) {
return null;
}

return array_unique($controllers);
[$controller] = explode('::', $handler, 2);

return $controller;
}

/**
Expand Down
11 changes: 6 additions & 5 deletions tests/system/Router/RouteCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1782,15 +1782,16 @@ public function testGetRegisteredControllersReturnsOneControllerWhenTwoRoutsWith
public function testGetRegisteredControllersReturnsAllControllers()
{
$collection = $this->getCollector();
$collection->get('test', '\App\Controllers\Hello::get');
$collection->post('test', '\App\Controllers\Hello::post');
$collection->post('hello', '\App\Controllers\Test::hello');
$collection->get('test', '\App\Controllers\HelloGet::get');
$collection->post('test', '\App\Controllers\HelloPost::post');
$collection->post('hello', '\App\Controllers\TestPost::hello');

$routes = $collection->getRegisteredControllers('*');

$expects = [
'\App\Controllers\Hello',
'\App\Controllers\Test',
'\App\Controllers\HelloGet',
'\App\Controllers\HelloPost',
'\App\Controllers\TestPost',
];
$this->assertSame($expects, $routes);
}
Expand Down