From 4dc421b64e021508410fd51884182c46ac5584e6 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 24 Dec 2022 10:55:58 +0900 Subject: [PATCH 1/4] feat: add option to `spark routes` to sort by handler --- system/Commands/Utilities/Routes.php | 13 +++++++++++-- tests/system/Commands/RoutesTest.php | 26 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/system/Commands/Utilities/Routes.php b/system/Commands/Utilities/Routes.php index bf91729cb273..22b2498138df 100644 --- a/system/Commands/Utilities/Routes.php +++ b/system/Commands/Utilities/Routes.php @@ -68,13 +68,17 @@ class Routes extends BaseCommand * * @var array */ - protected $options = []; + protected $options = [ + '-h' => 'Sort by Handler.', + ]; /** * Displays the help for the spark cli script itself. */ public function run(array $params) { + $sortByHandler = array_key_exists('h', $params) ? true : false; + $collection = Services::routes()->loadRoutes(); $methods = [ 'get', @@ -157,11 +161,16 @@ public function run(array $params) 'Method', 'Route', 'Name', - 'Handler', + $sortByHandler ? 'Handler ↓' : 'Handler', 'Before Filters', 'After Filters', ]; + // Sort by Handler. + if ($sortByHandler) { + usort($tbody, static fn ($handler1, $handler2) => strcmp($handler1[3], $handler2[3])); + } + CLI::table($tbody, $thead); } } diff --git a/tests/system/Commands/RoutesTest.php b/tests/system/Commands/RoutesTest.php index 84e09785af7d..6d506274145d 100644 --- a/tests/system/Commands/RoutesTest.php +++ b/tests/system/Commands/RoutesTest.php @@ -77,6 +77,32 @@ public function testRoutesCommand() $this->assertStringContainsString($expected, $this->getBuffer()); } + public function testRoutesCommandSortByHandler() + { + $this->getCleanRoutes(); + + command('routes -h'); + + $expected = <<<'EOL' + +---------+---------+---------------+----------------------------------------+----------------+---------------+ + | Method | Route | Name | Handler ↓ | Before Filters | After Filters | + +---------+---------+---------------+----------------------------------------+----------------+---------------+ + | GET | closure | » | (Closure) | | toolbar | + | GET | / | » | \App\Controllers\Home::index | | toolbar | + | GET | testing | testing-index | \App\Controllers\TestController::index | | toolbar | + | HEAD | testing | testing-index | \App\Controllers\TestController::index | | toolbar | + | POST | testing | testing-index | \App\Controllers\TestController::index | | toolbar | + | PUT | testing | testing-index | \App\Controllers\TestController::index | | toolbar | + | DELETE | testing | testing-index | \App\Controllers\TestController::index | | toolbar | + | OPTIONS | testing | testing-index | \App\Controllers\TestController::index | | toolbar | + | TRACE | testing | testing-index | \App\Controllers\TestController::index | | toolbar | + | CONNECT | testing | testing-index | \App\Controllers\TestController::index | | toolbar | + | CLI | testing | testing-index | \App\Controllers\TestController::index | | | + +---------+---------+---------------+----------------------------------------+----------------+---------------+ + EOL; + $this->assertStringContainsString($expected, $this->getBuffer()); + } + public function testRoutesCommandAutoRouteImproved() { $routes = $this->getCleanRoutes(); From 42f1afce71a1927c1ca1099023776e2ccf621e92 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 24 Dec 2022 11:06:22 +0900 Subject: [PATCH 2/4] docs: add docs --- user_guide_src/source/changelogs/v4.3.0.rst | 3 +++ user_guide_src/source/incoming/routing.rst | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/user_guide_src/source/changelogs/v4.3.0.rst b/user_guide_src/source/changelogs/v4.3.0.rst index 97a9294d1415..ec998bd112b3 100644 --- a/user_guide_src/source/changelogs/v4.3.0.rst +++ b/user_guide_src/source/changelogs/v4.3.0.rst @@ -208,6 +208,9 @@ Commands - Added ``spark filter:check`` command to check the filters for a route. See :ref:`Controller Filters ` for the details. - Added ``spark make:cell`` command to create a new Cell file and its view. See :ref:`generating-cell-via-command` for the details. - Now ``spark routes`` command shows route names. See :ref:`URI Routing `. +- Now ``spark routes`` command can sort the output by Handler. + See :ref:`routing-spark-routes-sort-by-handler`. + - Help information for a spark command can now be accessed using the ``--help`` option (e.g. ``php spark serve --help``) - Added methods ``CLI::promptByMultipleKeys()`` to support multiple value in input, unlike ``promptByKey()``. See :ref:`prompt-by-multiple-keys` for details. - HTTP/3 is now considered a valid protocol. diff --git a/user_guide_src/source/incoming/routing.rst b/user_guide_src/source/incoming/routing.rst index 1ca7f13b14a6..be01d92d3d51 100644 --- a/user_guide_src/source/incoming/routing.rst +++ b/user_guide_src/source/incoming/routing.rst @@ -804,6 +804,9 @@ Since v4.3.0, the *Name* column shows the route name. ``»`` indicates the name .. 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 ` command. +Auto Routing (Improved) +----------------------- + When you use Auto Routing (Improved), the output is like the following: .. code-block:: none @@ -818,6 +821,9 @@ The *Method* will be like ``GET(auto)``. ``/..`` in the *Route* column indicates one segment. ``[/..]`` indicates it is optional. +Auto Routing (Legacy) +--------------------- + When you use Auto Routing (Legacy), the output is like the following: .. code-block:: none @@ -833,3 +839,14 @@ The *Method* will be ``auto``. ``[/...]`` in the *Route* column indicates any number of segments. .. 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``. + +.. _routing-spark-routes-sort-by-handler: + +Sort by Handler +--------------- + +.. versionadded:: 4.3.0 + +You can sort the routes by *Handler*:: + + > php spark routes -h From c2d4d3cadd7d93c633e033e6e030cd5b8c4a1b0b Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 24 Dec 2022 11:16:40 +0900 Subject: [PATCH 3/4] refactor: by rector --- system/Commands/Utilities/Routes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Commands/Utilities/Routes.php b/system/Commands/Utilities/Routes.php index 22b2498138df..4aeb32f4ff7a 100644 --- a/system/Commands/Utilities/Routes.php +++ b/system/Commands/Utilities/Routes.php @@ -77,7 +77,7 @@ class Routes extends BaseCommand */ public function run(array $params) { - $sortByHandler = array_key_exists('h', $params) ? true : false; + $sortByHandler = array_key_exists('h', $params); $collection = Services::routes()->loadRoutes(); $methods = [ From ee3884f69d3818cf1176e64b5c0b82b5ba600a8b Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 24 Dec 2022 19:51:07 +0900 Subject: [PATCH 4/4] docs: make @var more specific Co-authored-by: Pooya Parsa Dadashi --- system/Commands/Utilities/Routes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Commands/Utilities/Routes.php b/system/Commands/Utilities/Routes.php index 4aeb32f4ff7a..bcd5825c649e 100644 --- a/system/Commands/Utilities/Routes.php +++ b/system/Commands/Utilities/Routes.php @@ -66,7 +66,7 @@ class Routes extends BaseCommand /** * the Command's Options * - * @var array + * @var array */ protected $options = [ '-h' => 'Sort by Handler.',