From 680ea61a8e18395ee354955278d817e5c0cc9c5a Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 25 Jan 2023 13:48:37 +0900 Subject: [PATCH 1/3] test: improve test case --- tests/system/Router/RouteCollectionTest.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/system/Router/RouteCollectionTest.php b/tests/system/Router/RouteCollectionTest.php index 70d378467414..6458449a02c9 100644 --- a/tests/system/Router/RouteCollectionTest.php +++ b/tests/system/Router/RouteCollectionTest.php @@ -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); } From 74bbf2bcf86712a15683d68e52ddab0b9027ffb0 Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 25 Jan 2023 14:27:04 +0900 Subject: [PATCH 2/3] fix: getRegisteredControllers() may not return all controllers --- system/Router/RouteCollection.php | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/system/Router/RouteCollection.php b/system/Router/RouteCollection.php index 7dd87701a432..26cedbfc1a65 100644 --- a/system/Router/RouteCollection.php +++ b/system/Router/RouteCollection.php @@ -1577,31 +1577,32 @@ 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 controller name list + * @phpstan-return list */ public function getRegisteredControllers(?string $verb = '*'): array { - $routes = []; + $handlers = []; 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) { + $key = key($route['route']); + $handlers[] = $route['route'][$key]; + } } } else { $routes = $this->getRoutes($verb); + + foreach ($routes as $handler) { + $handlers[] = $handler; + } } $controllers = []; - foreach ($routes as $handler) { + foreach ($handlers as $handler) { if (! is_string($handler)) { continue; } From 785c10fc1e0ab860ea9afeea7347c2a4bd55ffd6 Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 25 Jan 2023 14:42:06 +0900 Subject: [PATCH 3/3] refactor: reduce foreach --- system/Router/RouteCollection.php | 38 +++++++++++++++++++------------ 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/system/Router/RouteCollection.php b/system/Router/RouteCollection.php index 26cedbfc1a65..c688ed88a04f 100644 --- a/system/Router/RouteCollection.php +++ b/system/Router/RouteCollection.php @@ -1583,36 +1583,46 @@ public function setPrioritize(bool $enabled = true) */ public function getRegisteredControllers(?string $verb = '*'): array { - $handlers = []; + $controllers = []; if ($verb === '*') { foreach ($this->defaultHTTPMethods as $tmpVerb) { foreach ($this->routes[$tmpVerb] as $route) { - $key = key($route['route']); - $handlers[] = $route['route'][$key]; + $routeKey = key($route['route']); + $controller = $this->getControllerName($route['route'][$routeKey]); + if ($controller !== null) { + $controllers[] = $controller; + } } } } else { $routes = $this->getRoutes($verb); foreach ($routes as $handler) { - $handlers[] = $handler; + $controller = $this->getControllerName($handler); + if ($controller !== null) { + $controllers[] = $controller; + } } } - $controllers = []; - - foreach ($handlers as $handler) { - if (! is_string($handler)) { - continue; - } - - [$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; } /**