line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::AutoRoute; |
2
|
1
|
|
|
1
|
|
1423
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
181
|
use File::Find 'find'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
464
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.22'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub register { |
9
|
12
|
|
|
12
|
1
|
89944
|
my ($self, $app, $conf) = @_; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# Parent route |
12
|
12
|
|
66
|
|
|
70
|
my $r = $conf->{route} || $app->routes; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# Template Base |
15
|
12
|
|
|
|
|
84
|
my $template_base_dirs = $app->renderer->paths; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# Top directory |
18
|
12
|
|
100
|
|
|
106
|
my $top_dir = $conf->{top_dir} || 'auto'; |
19
|
12
|
|
|
|
|
21
|
$top_dir =~ s#^/##; |
20
|
12
|
|
|
|
|
18
|
$top_dir =~ s#/$##; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# Search templates |
23
|
12
|
|
|
|
|
12
|
my @templates; |
24
|
12
|
|
|
|
|
18
|
for my $template_base_dir (@$template_base_dirs) { |
25
|
15
|
|
|
|
|
25
|
$template_base_dir =~ s#/$##; |
26
|
15
|
|
|
|
|
33
|
my $template_dir = "$template_base_dir/$top_dir"; |
27
|
|
|
|
|
|
|
|
28
|
15
|
50
|
|
|
|
388
|
if (-d $template_dir) { |
29
|
|
|
|
|
|
|
# Find templates |
30
|
|
|
|
|
|
|
find(sub { |
31
|
73
|
|
|
73
|
|
72
|
my $template_abs = $File::Find::name; |
32
|
73
|
|
|
|
|
49
|
my $template = $template_abs; |
33
|
73
|
|
|
|
|
337
|
$template =~ s/\Q$template_dir\///; |
34
|
|
|
|
|
|
|
|
35
|
73
|
100
|
|
|
|
1541
|
if ($template =~ s/\.html\.ep$//) { |
36
|
43
|
|
|
|
|
609
|
push @templates, $template; |
37
|
|
|
|
|
|
|
} |
38
|
15
|
|
|
|
|
879
|
}, $template_dir); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# Register routes |
43
|
12
|
|
|
|
|
21
|
for my $template (@templates) { |
44
|
43
|
100
|
|
|
|
3647
|
my $route_path = $template eq 'index' ? '/' : $template; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# Route |
47
|
|
|
|
|
|
|
$r->route("/$route_path") |
48
|
43
|
|
|
40
|
|
146
|
->to(cb => sub { shift->render("/$top_dir/$template", 'mojo.maybe' => 1) }); |
|
40
|
|
|
|
|
380420
|
|
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
1; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=head1 NAME |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
Mojolicious::Plugin::AutoRoute - Mojolicious plugin to create routes automatically |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head1 SYNOPSIS |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# Mojolicious |
61
|
|
|
|
|
|
|
$self->plugin('AutoRoute'); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# Mojolicious::Lite |
64
|
|
|
|
|
|
|
plugin 'AutoRoute'; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# With option |
67
|
|
|
|
|
|
|
plugin 'AutoRoute', route => $r; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head1 DESCRIPTION |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
L is a L plugin |
72
|
|
|
|
|
|
|
to create routes automatically. |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
If you put templates into C directory, |
75
|
|
|
|
|
|
|
the corresponding routes is created automatically. |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
For example: |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
TEMPLATES ROUTES |
80
|
|
|
|
|
|
|
templates/auto/index.html.ep # / |
81
|
|
|
|
|
|
|
/foo.html.ep # /foo |
82
|
|
|
|
|
|
|
/foo/bar.html.ep # /foo/bar |
83
|
|
|
|
|
|
|
/foo/bar/baz.html.ep # /foo/bar/baz |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
I like PHP simplicity. All thing needed is that you put PHP files into some directory, |
86
|
|
|
|
|
|
|
and write program. You don't need to create routes manually. |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
This plugin gives PHP simplicity to L. |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head1 EXAMPLE |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
use Mojolicious::Lite; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# AutoRoute |
95
|
|
|
|
|
|
|
plugin 'AutoRoute'; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# Custom routes |
98
|
|
|
|
|
|
|
get '/create/:id' => sub { shift->render_maybe('/create') }; |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
app->start; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
__DATA__ |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
@@ auto/index.html.ep |
105
|
|
|
|
|
|
|
/ |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
@@ auto/foo.html.ep |
108
|
|
|
|
|
|
|
/foo |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
@@ auto/bar.html.ep |
111
|
|
|
|
|
|
|
/bar |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
@@ auto/foo/bar/baz.html.ep |
114
|
|
|
|
|
|
|
/foo/bar/baz |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
@@ auto/json.html.ep |
117
|
|
|
|
|
|
|
<% |
118
|
|
|
|
|
|
|
$self->render(json => {foo => 1}); |
119
|
|
|
|
|
|
|
return; |
120
|
|
|
|
|
|
|
%> |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
@@ create.html.ep |
123
|
|
|
|
|
|
|
/create/<%= $id %> |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head1 OPTIONS |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head2 route |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
route => $route; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
You can set parent route if you need. |
132
|
|
|
|
|
|
|
This is L object. |
133
|
|
|
|
|
|
|
Default is C<$app->routes>. |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=head2 top_dir |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
top_dir => 'myauto' |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
Top directory. default is C. |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head1 TIPS |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
If you want to create custom route, use C method. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
# Mojolicious Lite |
146
|
|
|
|
|
|
|
any '/foo' => sub { shift->render_maybe('/foo') }; |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
# Mojolicious |
149
|
|
|
|
|
|
|
$r->any('/foo' => sub { shift->render_maybe('/foo') }; |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
For backwrod comaptible, you can use C function. |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
use Mojolicious::Plugin::AutoRoute::Util 'template'; |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
# Mojolicious Lite |
156
|
|
|
|
|
|
|
any '/foo' => template '/foo'; |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
# Mojolicious |
159
|
|
|
|
|
|
|
$r->any('/foo' => template '/foo'); |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head1 METHOD |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head2 register |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
$plugin->register($app); |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
Register plugin in L application. |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head1 SEE ALSO |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
L, L, L. |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=cut |