Skip to content

Commit 9381e48

Browse files
authored
Merge pull request #7174 from kenjis/fix-RouteCollection-getRegisteredControllers
fix: RouteCollection::getRegisteredControllers() may not return all controllers
2 parents b302f15 + 785c10f commit 9381e48

2 files changed

Lines changed: 37 additions & 25 deletions

File tree

system/Router/RouteCollection.php

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,41 +1577,52 @@ public function setPrioritize(bool $enabled = true)
15771577
* Get all controllers in Route Handlers
15781578
*
15791579
* @param string|null $verb HTTP verb. `'*'` returns all controllers in any verb.
1580+
*
1581+
* @return array<int, string> controller name list
1582+
* @phpstan-return list<string>
15801583
*/
15811584
public function getRegisteredControllers(?string $verb = '*'): array
15821585
{
1583-
$routes = [];
1586+
$controllers = [];
15841587

15851588
if ($verb === '*') {
1586-
$rawRoutes = [];
1587-
15881589
foreach ($this->defaultHTTPMethods as $tmpVerb) {
1589-
$rawRoutes = array_merge($rawRoutes, $this->routes[$tmpVerb]);
1590-
}
1591-
1592-
foreach ($rawRoutes as $route) {
1593-
$key = key($route['route']);
1594-
$handler = $route['route'][$key];
1595-
1596-
$routes[$key] = $handler;
1590+
foreach ($this->routes[$tmpVerb] as $route) {
1591+
$routeKey = key($route['route']);
1592+
$controller = $this->getControllerName($route['route'][$routeKey]);
1593+
if ($controller !== null) {
1594+
$controllers[] = $controller;
1595+
}
1596+
}
15971597
}
15981598
} else {
15991599
$routes = $this->getRoutes($verb);
1600-
}
1601-
1602-
$controllers = [];
16031600

1604-
foreach ($routes as $handler) {
1605-
if (! is_string($handler)) {
1606-
continue;
1601+
foreach ($routes as $handler) {
1602+
$controller = $this->getControllerName($handler);
1603+
if ($controller !== null) {
1604+
$controllers[] = $controller;
1605+
}
16071606
}
1607+
}
16081608

1609-
[$controller] = explode('::', $handler, 2);
1609+
return array_unique($controllers);
1610+
}
16101611

1611-
$controllers[] = $controller;
1612+
/**
1613+
* @param Closure|string $handler Handler
1614+
*
1615+
* @return string|null Controller classname
1616+
*/
1617+
private function getControllerName($handler)
1618+
{
1619+
if (! is_string($handler)) {
1620+
return null;
16121621
}
16131622

1614-
return array_unique($controllers);
1623+
[$controller] = explode('::', $handler, 2);
1624+
1625+
return $controller;
16151626
}
16161627

16171628
/**

tests/system/Router/RouteCollectionTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,15 +1782,16 @@ public function testGetRegisteredControllersReturnsOneControllerWhenTwoRoutsWith
17821782
public function testGetRegisteredControllersReturnsAllControllers()
17831783
{
17841784
$collection = $this->getCollector();
1785-
$collection->get('test', '\App\Controllers\Hello::get');
1786-
$collection->post('test', '\App\Controllers\Hello::post');
1787-
$collection->post('hello', '\App\Controllers\Test::hello');
1785+
$collection->get('test', '\App\Controllers\HelloGet::get');
1786+
$collection->post('test', '\App\Controllers\HelloPost::post');
1787+
$collection->post('hello', '\App\Controllers\TestPost::hello');
17881788

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

17911791
$expects = [
1792-
'\App\Controllers\Hello',
1793-
'\App\Controllers\Test',
1792+
'\App\Controllers\HelloGet',
1793+
'\App\Controllers\HelloPost',
1794+
'\App\Controllers\TestPost',
17941795
];
17951796
$this->assertSame($expects, $routes);
17961797
}

0 commit comments

Comments
 (0)