File Coverage

blib/lib/Mojolicious/Command/routes.pm
Criterion Covered Total %
statement 37 38 97.3
branch 13 20 65.0
condition 3 7 42.8
subroutine 5 5 100.0
pod 1 1 100.0
total 59 71 83.1


line stmt bran cond sub pod time code
1             package Mojolicious::Command::routes;
2 1     1   7 use Mojo::Base 'Mojolicious::Command';
  1         2  
  1         9  
3              
4 1     1   7 use re qw(regexp_pattern);
  1         4  
  1         158  
5 1     1   7 use Mojo::Util qw(encode getopt tablify);
  1         3  
  1         658  
6              
7             has description => 'Show available routes';
8             has usage => sub { shift->extract_usage };
9              
10             sub run {
11 3     3 1 2595 my ($self, @args) = @_;
12              
13 3 100       13 die $self->usage unless getopt \@args, 'v|verbose' => \my $verbose;
14              
15 2         6 my $rows = [];
16 2         7 _walk($_, 0, $rows, $verbose) for @{$self->app->routes->children};
  2         14  
17 2         12 print encode('UTF-8', tablify($rows));
18             }
19              
20             sub _walk {
21 2     2   8 my ($route, $depth, $rows, $verbose) = @_;
22              
23             # Pattern
24 2         4 my $prefix = '';
25 2 50       8 if (my $i = $depth * 2) { $prefix .= ' ' x $i . '+' }
  0         0  
26 2   50     9 push @$rows, my $row = [$prefix . ($route->pattern->unparsed || '/')];
27              
28             # Flags
29 2         4 my @flags;
30 2 50 50     3 push @flags, @{$route->requires // []} ? 'C' : '.';
  2         10  
31 2 50       8 push @flags, (my $partial = $route->partial) ? 'P' : '.';
32 2 50       10 push @flags, $route->inline ? 'U' : '.';
33 2 50       11 push @flags, $route->is_websocket ? 'W' : '.';
34 2 100       9 push @$row, join('', @flags) if $verbose;
35              
36             # Methods
37 2         7 my $methods = $route->methods;
38 2 50       7 push @$row, !$methods ? '*' : uc join ',', @$methods;
39              
40             # Name
41 2         9 my $name = $route->name;
42 2 50       11 push @$row, $route->has_custom_name ? qq{"$name"} : $name;
43              
44             # Regex (verbose)
45 2         7 my $pattern = $route->pattern;
46 2   33     8 $pattern->match('/', $route->is_endpoint && !$partial);
47 2 100       11 push @$row, (regexp_pattern $pattern->regex)[0] if $verbose;
48              
49 2         5 $depth++;
50 2         3 _walk($_, $depth, $rows, $verbose) for @{$route->children};
  2         7  
51 2         8 $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 to home directory of your application, defaults to
72             the value of MOJO_HOME or auto-detection
73             -m, --mode 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, P=Partial, U=Under and W=WebSocket
77              
78             =head1 DESCRIPTION
79              
80             L lists all your application routes.
81              
82             This is a core command, that means it is always enabled and its code a good example for learning to build new commands,
83             you're welcome to fork it.
84              
85             See L for a list of commands that are available by default.
86              
87             =head1 ATTRIBUTES
88              
89             L inherits all attributes from L and implements the following new
90             ones.
91              
92             =head2 description
93              
94             my $description = $routes->description;
95             $routes = $routes->description('Foo');
96              
97             Short description of this command, used for the command list.
98              
99             =head2 usage
100              
101             my $usage = $routes->usage;
102             $routes = $routes->usage('Foo');
103              
104             Usage information for this command, used for the help screen.
105              
106             =head1 METHODS
107              
108             L inherits all methods from L and implements the following new
109             ones.
110              
111             =head2 run
112              
113             $routes->run(@ARGV);
114              
115             Run this command.
116              
117             =head1 SEE ALSO
118              
119             L, L, L.
120              
121             =cut