From 591e05e68d68cf039672b49096a6c3a57a988c75 Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 19 Jan 2022 11:18:15 +0900 Subject: [PATCH] feat: add auto routes listing to `spark routes` command --- system/Commands/Utilities/Routes.php | 34 ++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/system/Commands/Utilities/Routes.php b/system/Commands/Utilities/Routes.php index 210d1e11c5f7..78c7f1c8411f 100644 --- a/system/Commands/Utilities/Routes.php +++ b/system/Commands/Utilities/Routes.php @@ -13,6 +13,9 @@ use CodeIgniter\CLI\BaseCommand; use CodeIgniter\CLI\CLI; +use CodeIgniter\Commands\Utilities\Routes\ControllerFinder; +use CodeIgniter\Commands\Utilities\Routes\ControllerMethodReader; +use CodeIgniter\Router\RouteCollection; use Config\Services; /** @@ -101,6 +104,10 @@ public function run(array $params) } } + if ($collection->shouldAutoRoute()) { + $tbody = array_merge($tbody, $this->getAutoRoutes($collection)); + } + $thead = [ 'Method', 'Route', @@ -109,4 +116,31 @@ public function run(array $params) CLI::table($tbody, $thead); } + + private function getAutoRoutes(RouteCollection $collection): array + { + $defaultNamespace = $collection->getDefaultNamespace(); + $finder = new ControllerFinder($defaultNamespace); + $reader = new ControllerMethodReader($defaultNamespace); + + $tbody = []; + + foreach ($finder->find() as $class) { + $output = $reader->read( + $class, + $collection->getDefaultController(), + $collection->getDefaultMethod() + ); + + foreach ($output as $item) { + $tbody[] = [ + 'auto', + $item['route'], + $item['handler'], + ]; + } + } + + return $tbody; + } }