Skip to content

Commit 4a6fa4c

Browse files
authored
Merge pull request #7015 from kenjis/feat-spark-route-sort-by-handler
feat: `spark routes` option to sort by handler
2 parents 86e142d + ee3884f commit 4a6fa4c

4 files changed

Lines changed: 58 additions & 3 deletions

File tree

system/Commands/Utilities/Routes.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,19 @@ class Routes extends BaseCommand
6666
/**
6767
* the Command's Options
6868
*
69-
* @var array
69+
* @var array<string, string>
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);
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();

user_guide_src/source/changelogs/v4.3.0.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ Commands
208208
- Added ``spark filter:check`` command to check the filters for a route. See :ref:`Controller Filters <spark-filter-check>` for the details.
209209
- Added ``spark make:cell`` command to create a new Cell file and its view. See :ref:`generating-cell-via-command` for the details.
210210
- Now ``spark routes`` command shows route names. See :ref:`URI Routing <routing-spark-routes>`.
211+
- Now ``spark routes`` command can sort the output by Handler.
212+
See :ref:`routing-spark-routes-sort-by-handler`.
213+
211214
- Help information for a spark command can now be accessed using the ``--help`` option (e.g. ``php spark serve --help``)
212215
- Added methods ``CLI::promptByMultipleKeys()`` to support multiple value in input, unlike ``promptByKey()``. See :ref:`prompt-by-multiple-keys` for details.
213216
- HTTP/3 is now considered a valid protocol.

user_guide_src/source/incoming/routing.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,9 @@ Since v4.3.0, the *Name* column shows the route name. ``»`` indicates the name
804804

805805
.. important:: The system is not perfect. If you use Custom Placeholders, *Filters* might not be correct. If you want to check filters for a route, you can use :ref:`spark filter:check <spark-filter-check>` command.
806806

807+
Auto Routing (Improved)
808+
-----------------------
809+
807810
When you use Auto Routing (Improved), the output is like the following:
808811

809812
.. code-block:: none
@@ -818,6 +821,9 @@ The *Method* will be like ``GET(auto)``.
818821

819822
``/..`` in the *Route* column indicates one segment. ``[/..]`` indicates it is optional.
820823

824+
Auto Routing (Legacy)
825+
---------------------
826+
821827
When you use Auto Routing (Legacy), the output is like the following:
822828

823829
.. code-block:: none
@@ -833,3 +839,14 @@ The *Method* will be ``auto``.
833839
``[/...]`` in the *Route* column indicates any number of segments.
834840

835841
.. note:: When auto-routing is enabled, if you have the route ``home``, it can be also accessd by ``Home``, or maybe by ``hOme``, ``hoMe``, ``HOME``, etc. But the command shows only ``home``.
842+
843+
.. _routing-spark-routes-sort-by-handler:
844+
845+
Sort by Handler
846+
---------------
847+
848+
.. versionadded:: 4.3.0
849+
850+
You can sort the routes by *Handler*::
851+
852+
> php spark routes -h

0 commit comments

Comments
 (0)