Skip to content

Commit 785c10f

Browse files
committed
refactor: reduce foreach
1 parent 74bbf2b commit 785c10f

1 file changed

Lines changed: 24 additions & 14 deletions

File tree

system/Router/RouteCollection.php

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,36 +1583,46 @@ public function setPrioritize(bool $enabled = true)
15831583
*/
15841584
public function getRegisteredControllers(?string $verb = '*'): array
15851585
{
1586-
$handlers = [];
1586+
$controllers = [];
15871587

15881588
if ($verb === '*') {
15891589
foreach ($this->defaultHTTPMethods as $tmpVerb) {
15901590
foreach ($this->routes[$tmpVerb] as $route) {
1591-
$key = key($route['route']);
1592-
$handlers[] = $route['route'][$key];
1591+
$routeKey = key($route['route']);
1592+
$controller = $this->getControllerName($route['route'][$routeKey]);
1593+
if ($controller !== null) {
1594+
$controllers[] = $controller;
1595+
}
15931596
}
15941597
}
15951598
} else {
15961599
$routes = $this->getRoutes($verb);
15971600

15981601
foreach ($routes as $handler) {
1599-
$handlers[] = $handler;
1602+
$controller = $this->getControllerName($handler);
1603+
if ($controller !== null) {
1604+
$controllers[] = $controller;
1605+
}
16001606
}
16011607
}
16021608

1603-
$controllers = [];
1604-
1605-
foreach ($handlers as $handler) {
1606-
if (! is_string($handler)) {
1607-
continue;
1608-
}
1609-
1610-
[$controller] = explode('::', $handler, 2);
1609+
return array_unique($controllers);
1610+
}
16111611

1612-
$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;
16131621
}
16141622

1615-
return array_unique($controllers);
1623+
[$controller] = explode('::', $handler, 2);
1624+
1625+
return $controller;
16161626
}
16171627

16181628
/**

0 commit comments

Comments
 (0)