Skip to content

Commit

Permalink
feat: add auto routes listing to spark routes command
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Jan 19, 2022
1 parent 9ea5492 commit 591e05e
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions system/Commands/Utilities/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -101,6 +104,10 @@ public function run(array $params)
}
}

if ($collection->shouldAutoRoute()) {
$tbody = array_merge($tbody, $this->getAutoRoutes($collection));
}

$thead = [
'Method',
'Route',
Expand All @@ -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;
}
}

0 comments on commit 591e05e

Please sign in to comment.