Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: auto routes listing #5590

Merged
merged 15 commits into from
Jan 27, 2022
Merged

Conversation

kenjis
Copy link
Member

@kenjis kenjis commented Jan 19, 2022

Description

  • add listing of auto routes in spark routes
    • auto routes are not listed and users may expose unexpected methods

Note:
If you have Controllers\Home::index, on macOS you can access it by:

But this command shows only home/index[/...].

Checklist:

  • This PR needs Fix Autoloader::initialize() #5592
  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@kenjis kenjis marked this pull request as draft January 19, 2022 02:27
@kenjis kenjis added the enhancement PRs that improve existing functionalities label Jan 19, 2022
@kenjis kenjis force-pushed the add-auto-routes-listing branch 2 times, most recently from 591e05e to 622d611 Compare January 20, 2022 01:52
Copy link
Member

@MGatner MGatner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is some great work @kenjis! I do wonder if the functional change could be confusing or break someone's CLI output. What about making this a separate command, and appending a warning note at the end of routes like: "Auto-routing is enabled; run _____ to see additional active routes."

I would like to hear others' opinions too, I don't feel strongly either way.

@kenjis
Copy link
Member Author

kenjis commented Jan 24, 2022

@MGatner I think one command is better.
Because if you have auto routes, the routes are already there.
It is better you can see all routes at a sight.

+--------+-------------------------------------+-------------------------------------------------+
| Method | Route                               | Handler                                         |
+--------+-------------------------------------+-------------------------------------------------+
| GET    | /                                   | \App\Controllers\Home::index                    |
| GET    | news/create                         | \App\Controllers\News::create                   |
| GET    | news/([^/]+)                        | \App\Controllers\News::view/$1                  |
| GET    | news                                | \App\Controllers\News::index                    |
| GET    | (.*)                                | \App\Controllers\Pages::view/$1                 |
| POST   | news/create                         | \App\Controllers\News::create                   |
| CLI    | migrations/([^/]+)/([^/]+)          | \CodeIgniter\Commands\MigrationsCommand::$1/$2  |
| CLI    | migrations/([^/]+)                  | \CodeIgniter\Commands\MigrationsCommand::$1     |
| CLI    | migrations                          | \CodeIgniter\Commands\MigrationsCommand::index  |
| CLI    | ci(.*)                              | \CodeIgniter\CLI\CommandRunner::index/$1        |
| auto   | /                                   | \App\Controllers\Home::index                    |
| auto   | home                                | \App\Controllers\Home::index                    |
| auto   | home/index[/...]                    | \App\Controllers\Home::index                    |
| auto   | news                                | \App\Controllers\News::index                    |
| auto   | news/index[/...]                    | \App\Controllers\News::index                    |
| auto   | news/view[/...]                     | \App\Controllers\News::view                     |
| auto   | news/create[/...]                   | \App\Controllers\News::create                   |
| auto   | pages                               | \App\Controllers\Pages::index                   |
| auto   | pages/index[/...]                   | \App\Controllers\Pages::index                   |
| auto   | pages/view[/...]                    | \App\Controllers\Pages::view                    |
+--------+-------------------------------------+-------------------------------------------------+

You can use grep for one controller.

$ php spark routes | grep '\\App\\Controllers\\News::'
| GET    | news/create                         | \App\Controllers\News::create                   |
| GET    | news/([^/]+)                        | \App\Controllers\News::view/$1                  |
| GET    | news                                | \App\Controllers\News::index                    |
| POST   | news/create                         | \App\Controllers\News::create                   |
| auto   | news                                | \App\Controllers\News::index                    |
| auto   | news/index[/...]                    | \App\Controllers\News::index                    |
| auto   | news/view[/...]                     | \App\Controllers\News::view                     |
| auto   | news/create[/...]                   | \App\Controllers\News::create                   |

@kenjis
Copy link
Member Author

kenjis commented Jan 25, 2022

Added note in #5590 (comment)

@kenjis kenjis marked this pull request as ready for review January 25, 2022 01:59
@kenjis
Copy link
Member Author

kenjis commented Jan 25, 2022

Rector reports an error. But on my local, [OK] Rector is done!.
I rebased and ran composer update, but the same. Why?

1) system/Commands/Utilities/Routes.php:107

    ---------- begin diff ----------
@@ @@
                 $collection->getDefaultController(),
                 $collection->getDefaultMethod()
             );
-            $tbody = array_merge($tbody, $autoRouteCollector->get());
+            $tbody = [...$tbody, ...$autoRouteCollector->get()];
         }

         $thead = [
    ----------- end diff -----------

Applied rules:
 * ArraySpreadInsteadOfArrayMergeRector (https://wiki.php.net/rfc/spread_operator_for_array)

https://github.com/codeigniter4/CodeIgniter4/runs/4932368477?check_suite_focus=true

@kenjis
Copy link
Member Author

kenjis commented Jan 25, 2022

Fixed the file.

$ vendor/bin/rector --clear-cache
 648/708 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░]  91%
1 file with changes
===================

1) system/Commands/Utilities/Routes.php:107

    ---------- begin diff ----------
@@ @@
                 $collection->getDefaultController(),
                 $collection->getDefaultMethod()
             );
-            $tbody = array_merge($tbody, $autoRouteCollector->get());
+            $tbody = [...$tbody, ...$autoRouteCollector->get()];
         }

         $thead = [
    ----------- end diff -----------

Applied rules:
 * ArraySpreadInsteadOfArrayMergeRector (https://wiki.php.net/rfc/spread_operator_for_array)


                                                                                                    
 [ERROR] Could not process                                                                          
         "/Users/kenji/work/codeigniter/CodeIgniter4/vendor/rector/rector/vendor/symplify/easy-paral
         lel/src/ValueObject/ParallelProcess.php" file, due to:                                     
         "Child process timed out after 120 seconds". On line: 100                                  
                                                                                                    

                                                                                                    
 [ERROR] Could not process some files, due to:                                                      
         "Reached system errors count limit of 20, exiting...".                                     
                                                                                                    

@kenjis kenjis marked this pull request as draft January 26, 2022 02:51
@kenjis
Copy link
Member Author

kenjis commented Jan 26, 2022

@kenjis
Copy link
Member Author

kenjis commented Jan 26, 2022

<?php

namespace App\Controllers;

class Home extends BaseController
{
    public function index()
    {
        return view('welcome_message');
    }

    public function _remap($method)
    {
        if ($method === 'abc') {
            return $this->index();
        }

        return $this->default();
    }

    public function default()
    {
        return 'default';
    }
}
$ php spark routes

CodeIgniter v4.1.8 Command Line Tool - Server Time: 2022-01-25 23:24:33 UTC-06:00

+--------+----------------------------+------------------------------------------------+
| Method | Route                      | Handler                                        |
+--------+----------------------------+------------------------------------------------+
| GET    | /                          | \App\Controllers\Home::index                   |
| CLI    | migrations/([^/]+)/([^/]+) | \CodeIgniter\Commands\MigrationsCommand::$1/$2 |
| CLI    | migrations/([^/]+)         | \CodeIgniter\Commands\MigrationsCommand::$1    |
| CLI    | migrations                 | \CodeIgniter\Commands\MigrationsCommand::index |
| CLI    | ci(.*)                     | \CodeIgniter\CLI\CommandRunner::index/$1       |
| auto   | /                          | \App\Controllers\Home::_remap                  |
| auto   | home[/...]                 | \App\Controllers\Home::_remap                  |
+--------+----------------------------+------------------------------------------------+

@kenjis kenjis marked this pull request as ready for review January 26, 2022 05:30
@kenjis kenjis merged commit 367b805 into codeigniter4:develop Jan 27, 2022
@kenjis kenjis deleted the add-auto-routes-listing branch January 27, 2022 07:39
@kenjis kenjis mentioned this pull request Jan 29, 2022
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement PRs that improve existing functionalities
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants