Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/quality-control.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ jobs:
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: Fixes styling.

phpstan:
name: Perform Static Analysis
runs-on: ubuntu-latest
Expand Down
27 changes: 19 additions & 8 deletions src/Console/Commands/RoutesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Tempest\Console\Console;
use Tempest\Console\ConsoleCommand;
use Tempest\Console\ConsoleStyle;
use Tempest\Http\Route;
use Tempest\Http\Router;

final readonly class RoutesCommand
Expand All @@ -23,18 +24,28 @@ public function __construct(
)]
public function list(): void
{
/** @var \Tempest\Http\Route[] $sortedRoutes */
$sortedRoutes = [];
/**
* Here we flatten the multidimensional array and then run
* this array through a custom sort function that sorts
* first by URI and then by method name.
*
* @var \Tempest\Http\Route[] $routes
*/
$routes = array_merge([], ...array_values($this->router->getRoutes()));

usort($routes, function (Route $a, Route $b) {
if ($a->uri !== $b->uri) {
return $a->uri < $b->uri ? -1 : 1;
}

foreach($this->router->getRoutes() as $routesForMethod) {
foreach ($routesForMethod as $uri => $route) {
$sortedRoutes[$uri] = $route;
if ($a->method === $b->method) {
return 0;
}
}

ksort($sortedRoutes);
return $a->method->name < $b->method->name ? -1 : 1;
});

foreach ($sortedRoutes as $route) {
foreach ($routes as $route) {
$this->console->writeln(implode(' ', [
ConsoleStyle::FG_BLUE(str_pad($route->method->value, 4)),
ConsoleStyle::FG_DARK_BLUE($route->uri),
Expand Down