Skip to content

Commit 622d611

Browse files
committed
feat: add auto routes listing to spark routes command
1 parent d817ed2 commit 622d611

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

system/Commands/Utilities/Routes.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
use CodeIgniter\CLI\BaseCommand;
1515
use CodeIgniter\CLI\CLI;
16+
use CodeIgniter\Commands\Utilities\Routes\ControllerFinder;
17+
use CodeIgniter\Commands\Utilities\Routes\ControllerMethodReader;
18+
use CodeIgniter\Router\RouteCollection;
1619
use Config\Services;
1720

1821
/**
@@ -101,6 +104,10 @@ public function run(array $params)
101104
}
102105
}
103106

107+
if ($collection->shouldAutoRoute()) {
108+
$tbody = array_merge($tbody, $this->getAutoRoutes($collection));
109+
}
110+
104111
$thead = [
105112
'Method',
106113
'Route',
@@ -109,4 +116,31 @@ public function run(array $params)
109116

110117
CLI::table($tbody, $thead);
111118
}
119+
120+
private function getAutoRoutes(RouteCollection $collection): array
121+
{
122+
$defaultNamespace = $collection->getDefaultNamespace();
123+
$finder = new ControllerFinder($defaultNamespace);
124+
$reader = new ControllerMethodReader($defaultNamespace);
125+
126+
$tbody = [];
127+
128+
foreach ($finder->find() as $class) {
129+
$output = $reader->read(
130+
$class,
131+
$collection->getDefaultController(),
132+
$collection->getDefaultMethod()
133+
);
134+
135+
foreach ($output as $item) {
136+
$tbody[] = [
137+
'auto',
138+
$item['route'],
139+
$item['handler'],
140+
];
141+
}
142+
}
143+
144+
return $tbody;
145+
}
112146
}

0 commit comments

Comments
 (0)