88use Tempest \Console \ConsoleArgument ;
99use Tempest \Console \ConsoleCommand ;
1010use Tempest \Http \Method ;
11+ use Tempest \Reflection \MethodReflector ;
1112use Tempest \Router \RouteConfig ;
13+ use Tempest \Router \Routing \Construction \DiscoveredRoute ;
1214
1315use function Tempest \Support \str ;
16+ use function Tempest \Support \Str \after_last ;
17+ use function Tempest \Support \Str \before_last ;
1418
1519final readonly class RoutesCommand
1620{
@@ -19,7 +23,7 @@ public function __construct(
1923 private RouteConfig $ routeConfig ,
2024 ) {}
2125
22- #[ConsoleCommand(name: 'routes ' , description: 'Lists all registered routes ' , aliases: ['routes:list ' , 'list:routes ' ])]
26+ #[ConsoleCommand(name: 'routes ' , description: 'Lists all registered routes ' , aliases: ['routes:list ' , 'list:routes ' , ' route:list ' ])]
2327 public function list (
2428 #[ConsoleArgument(description: 'Outputs registered routes as JSON ' )]
2529 bool $ json = false ,
@@ -49,6 +53,7 @@ public function list(
4953 $ this ->console ->header ('Registered routes ' , subheader: 'These routes are registered in your application. ' );
5054 $ this ->console ->writeln ();
5155
56+ /** @var DiscoveredRoute $route */
5257 foreach ($ sortedRoutes as $ route ) {
5358 $ color = match ($ route ->method ) {
5459 Method::GET => 'magenta ' ,
@@ -61,14 +66,26 @@ public function list(
6166
6267 $ this ->console ->keyValue (
6368 key: str ($ route ->method ->value )
64- ->alignLeft (width: 5 )
69+ ->alignRight (width: 8 )
6570 ->wrap ("<style='fg- {$ color }'> " , '</style> ' )
66- ->append ($ route ->uri )
67- ->toString (),
68- value: str ()
69- ->append ("<style='dim'> {$ route ->handler ->getDeclaringClass ()->getName ()}:: {$ route ->handler ->getName ()}</style> " )
71+ ->append (' ' , $ route ->uri )
7072 ->toString (),
73+ value: $ this ->formatRouteHandler ($ route ->handler ),
7174 );
7275 }
7376 }
77+
78+ private function formatRouteHandler (MethodReflector $ handler ): string
79+ {
80+ $ namespace = before_last ($ handler ->getDeclaringClass ()->getName (), '\\' );
81+ $ name = after_last ($ handler ->getDeclaringClass ()->getName (), '\\' );
82+ $ method = $ handler ->getName ();
83+
84+ return sprintf (
85+ "<style='fg-white dim'>%s \\</style><style='fg-white'>%s</style><style='dim'>::</style><style='fg-white'>%s</style><style='dim'>()</style> " ,
86+ $ namespace ,
87+ $ name ,
88+ $ method ,
89+ );
90+ }
7491}
0 commit comments