1 | package Mojolicious::Command::routes;
|
---|
2 | use Mojo::Base 'Mojolicious::Command';
|
---|
3 |
|
---|
4 | use re 'regexp_pattern';
|
---|
5 | use Mojo::Util qw(encode getopt tablify);
|
---|
6 |
|
---|
7 | has description => 'Show available routes';
|
---|
8 | has usage => sub { shift->extract_usage };
|
---|
9 |
|
---|
10 | sub run {
|
---|
11 | my ($self, @args) = @_;
|
---|
12 |
|
---|
13 | getopt \@args, 'v|verbose' => \my $verbose;
|
---|
14 |
|
---|
15 | my $rows = [];
|
---|
16 | _walk($_, 0, $rows, $verbose) for @{$self->app->routes->children};
|
---|
17 | print encode('UTF-8', tablify($rows));
|
---|
18 | }
|
---|
19 |
|
---|
20 | sub _walk {
|
---|
21 | my ($route, $depth, $rows, $verbose) = @_;
|
---|
22 |
|
---|
23 | # Pattern
|
---|
24 | my $prefix = '';
|
---|
25 | if (my $i = $depth * 2) { $prefix .= ' ' x $i . '+' }
|
---|
26 | push @$rows, my $row = [$prefix . ($route->pattern->unparsed || '/')];
|
---|
27 |
|
---|
28 | # Flags
|
---|
29 | my @flags;
|
---|
30 | push @flags, @{$route->over || []} ? 'C' : '.';
|
---|
31 | push @flags, (my $partial = $route->partial) ? 'D' : '.';
|
---|
32 | push @flags, $route->inline ? 'U' : '.';
|
---|
33 | push @flags, $route->is_websocket ? 'W' : '.';
|
---|
34 | push @$row, join('', @flags) if $verbose;
|
---|
35 |
|
---|
36 | # Methods
|
---|
37 | my $via = $route->via;
|
---|
38 | push @$row, !$via ? '*' : uc join ',', @$via;
|
---|
39 |
|
---|
40 | # Name
|
---|
41 | my $name = $route->name;
|
---|
42 | push @$row, $route->has_custom_name ? qq{"$name"} : $name;
|
---|
43 |
|
---|
44 | # Regex (verbose)
|
---|
45 | my $pattern = $route->pattern;
|
---|
46 | $pattern->match('/', $route->is_endpoint && !$partial);
|
---|
47 | push @$row, (regexp_pattern $pattern->regex)[0] if $verbose;
|
---|
48 |
|
---|
49 | $depth++;
|
---|
50 | _walk($_, $depth, $rows, $verbose) for @{$route->children};
|
---|
51 | $depth--;
|
---|
52 | }
|
---|
53 |
|
---|
54 | 1;
|
---|
55 |
|
---|
56 | =encoding utf8
|
---|
57 |
|
---|
58 | =head1 NAME
|
---|
59 |
|
---|
60 | Mojolicious::Command::routes - Routes command
|
---|
61 |
|
---|
62 | =head1 SYNOPSIS
|
---|
63 |
|
---|
64 | Usage: APPLICATION routes [OPTIONS]
|
---|
65 |
|
---|
66 | ./myapp.pl routes
|
---|
67 | ./myapp.pl routes -v
|
---|
68 |
|
---|
69 | Options:
|
---|
70 | -h, --help Show this summary of available options
|
---|
71 | --home <path> Path to home directory of your application, defaults to
|
---|
72 | the value of MOJO_HOME or auto-detection
|
---|
73 | -m, --mode <name> Operating mode for your application, defaults to the
|
---|
74 | value of MOJO_MODE/PLACK_ENV or "development"
|
---|
75 | -v, --verbose Print additional details about routes, flags indicate
|
---|
76 | C=Conditions, D=Detour, U=Under and W=WebSocket
|
---|
77 |
|
---|
78 | =head1 DESCRIPTION
|
---|
79 |
|
---|
80 | L<Mojolicious::Command::routes> lists all your application routes.
|
---|
81 |
|
---|
82 | This is a core command, that means it is always enabled and its code a good
|
---|
83 | example for learning to build new commands, you're welcome to fork it.
|
---|
84 |
|
---|
85 | See L<Mojolicious::Commands/"COMMANDS"> for a list of commands that are
|
---|
86 | available by default.
|
---|
87 |
|
---|
88 | =head1 ATTRIBUTES
|
---|
89 |
|
---|
90 | L<Mojolicious::Command::routes> inherits all attributes from
|
---|
91 | L<Mojolicious::Command> and implements the following new ones.
|
---|
92 |
|
---|
93 | =head2 description
|
---|
94 |
|
---|
95 | my $description = $routes->description;
|
---|
96 | $routes = $routes->description('Foo');
|
---|
97 |
|
---|
98 | Short description of this command, used for the command list.
|
---|
99 |
|
---|
100 | =head2 usage
|
---|
101 |
|
---|
102 | my $usage = $routes->usage;
|
---|
103 | $routes = $routes->usage('Foo');
|
---|
104 |
|
---|
105 | Usage information for this command, used for the help screen.
|
---|
106 |
|
---|
107 | =head1 METHODS
|
---|
108 |
|
---|
109 | L<Mojolicious::Command::routes> inherits all methods from
|
---|
110 | L<Mojolicious::Command> and implements the following new ones.
|
---|
111 |
|
---|
112 | =head2 run
|
---|
113 |
|
---|
114 | $routes->run(@ARGV);
|
---|
115 |
|
---|
116 | Run this command.
|
---|
117 |
|
---|
118 | =head1 SEE ALSO
|
---|
119 |
|
---|
120 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
|
---|
121 |
|
---|
122 | =cut
|
---|