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