Skip to content

Commit 4dc421b

Browse files
committed
feat: add option to spark routes to sort by handler
1 parent cfd0208 commit 4dc421b

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

system/Commands/Utilities/Routes.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,17 @@ class Routes extends BaseCommand
6868
*
6969
* @var array
7070
*/
71-
protected $options = [];
71+
protected $options = [
72+
'-h' => 'Sort by Handler.',
73+
];
7274

7375
/**
7476
* Displays the help for the spark cli script itself.
7577
*/
7678
public function run(array $params)
7779
{
80+
$sortByHandler = array_key_exists('h', $params) ? true : false;
81+
7882
$collection = Services::routes()->loadRoutes();
7983
$methods = [
8084
'get',
@@ -157,11 +161,16 @@ public function run(array $params)
157161
'Method',
158162
'Route',
159163
'Name',
160-
'Handler',
164+
$sortByHandler ? 'Handler ↓' : 'Handler',
161165
'Before Filters',
162166
'After Filters',
163167
];
164168

169+
// Sort by Handler.
170+
if ($sortByHandler) {
171+
usort($tbody, static fn ($handler1, $handler2) => strcmp($handler1[3], $handler2[3]));
172+
}
173+
165174
CLI::table($tbody, $thead);
166175
}
167176
}

tests/system/Commands/RoutesTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,32 @@ public function testRoutesCommand()
7777
$this->assertStringContainsString($expected, $this->getBuffer());
7878
}
7979

80+
public function testRoutesCommandSortByHandler()
81+
{
82+
$this->getCleanRoutes();
83+
84+
command('routes -h');
85+
86+
$expected = <<<'EOL'
87+
+---------+---------+---------------+----------------------------------------+----------------+---------------+
88+
| Method | Route | Name | Handler ↓ | Before Filters | After Filters |
89+
+---------+---------+---------------+----------------------------------------+----------------+---------------+
90+
| GET | closure | » | (Closure) | | toolbar |
91+
| GET | / | » | \App\Controllers\Home::index | | toolbar |
92+
| GET | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
93+
| HEAD | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
94+
| POST | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
95+
| PUT | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
96+
| DELETE | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
97+
| OPTIONS | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
98+
| TRACE | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
99+
| CONNECT | testing | testing-index | \App\Controllers\TestController::index | | toolbar |
100+
| CLI | testing | testing-index | \App\Controllers\TestController::index | | |
101+
+---------+---------+---------------+----------------------------------------+----------------+---------------+
102+
EOL;
103+
$this->assertStringContainsString($expected, $this->getBuffer());
104+
}
105+
80106
public function testRoutesCommandAutoRouteImproved()
81107
{
82108
$routes = $this->getCleanRoutes();

0 commit comments

Comments
 (0)