1414/** 
1515 * Import classes 
1616 */ 
17+ use  RuntimeException ;
1718use  Sunrise \Http \Router \Router ;
1819use  Symfony \Component \Console \Command \Command ;
1920use  Symfony \Component \Console \Helper \Table ;
2324/** 
2425 * Import functions 
2526 */ 
26- use  function  Sunrise \Http \Router \path_plain ;
2727use  function  join ;
28+ use  function  sprintf ;
29+ use  function  Sunrise \Http \Router \path_plain ;
2830
2931/** 
30-  * RouteListCommand 
32+  * This command will list all routes in your application 
33+  * 
34+  * If you cannot pass the router to the constructor 
35+  * or your architecture has problems with autowiring, 
36+  * then just inherit this class and override the getRouter method. 
3137 * 
3238 * @since 2.9.0 
3339 */ 
34- final   class  RouteListCommand extends  Command
40+ class  RouteListCommand extends  Command
3541{
3642
3743    /** 
38-      * @var Router  
44+      * {@inheritdoc}  
3945     */ 
40-     private   $ router ;
46+     protected   static   $ defaultName  =  ' router:route-list  '  ;
4147
4248    /** 
4349     * {@inheritdoc} 
50+      */ 
51+     protected  static  $ defaultDescription  = 'Lists all routes in your application ' ;
52+ 
53+     /** 
54+      * The router instance populated with routes 
4455     * 
45-      * @param Router $router 
46-      * @param string|null $name 
56+      * @var Router|null 
4757     */ 
48-     public  function  __construct (Router   $ router , ?string  $ name  = null )
58+     private  $ router ;
59+ 
60+     /** 
61+      * Constructor of the class 
62+      * 
63+      * @param Router|null $router 
64+      */ 
65+     public  function  __construct (?Router   $ router  = null )
4966    {
67+         parent ::__construct ();
68+ 
5069        $ this  ->router  = $ router ;
70+     }
71+ 
72+     /** 
73+      * Gets the router instance populated with routes 
74+      * 
75+      * @return Router 
76+      * 
77+      * @throws RuntimeException 
78+      *         If the command doesn't contain the router instance. 
79+      * 
80+      * @since 2.11.0 
81+      */ 
82+     protected  function  getRouter () : Router 
83+     {
84+         if  (null  === $ this  ->router ) {
85+             throw  new  RuntimeException (sprintf (
86+                 'The %2$s() method MUST return the %1$s class instance.  '  .
87+                 'Pass the %1$s class instance to the constructor, or override the %2$s() method. ' ,
88+                 Router::class,
89+                 __METHOD__ 
90+             ));
91+         }
5192
52-         parent :: __construct ( $ name  ??  ' router:route-list ' ) ;
93+         return   $ this -> router ;
5394    }
5495
5596    /** 
5697     * {@inheritdoc} 
5798     */ 
58-     public  function  execute (InputInterface   $ input , OutputInterface   $ output ) : int 
99+     final   protected  function  execute (InputInterface   $ input , OutputInterface   $ output ) : int 
59100    {
60101        $ table  = new  Table ($ output );
61102
@@ -66,7 +107,7 @@ public function execute(InputInterface $input, OutputInterface $output) : int
66107            'Verb ' ,
67108        ]);
68109
69-         foreach  ($ this  ->router ->getRoutes () as  $ route ) {
110+         foreach  ($ this  ->getRouter () ->getRoutes () as  $ route ) {
70111            $ table ->addRow ([
71112                $ route ->getName (),
72113                $ route ->getHost () ?? 'ANY ' ,
0 commit comments