| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Mojolicious::Plugin::AutoRoute; | 
| 2 | 1 |  |  | 1 |  | 1256 | use Mojo::Base 'Mojolicious::Plugin'; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 6 |  | 
| 3 |  |  |  |  |  |  |  | 
| 4 | 1 |  |  | 1 |  | 165 | use File::Find 'find'; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 393 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | our $VERSION = '0.21'; | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | sub register { | 
| 9 | 12 |  |  | 12 | 1 | 84069 | my ($self, $app, $conf) = @_; | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | # Parent route | 
| 12 | 12 |  | 66 |  |  | 68 | my $r = $conf->{route} || $app->routes; | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | # Template Base | 
| 15 | 12 |  |  |  |  | 73 | my $template_base_dirs = $app->renderer->paths; | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | # Top directory | 
| 18 | 12 |  | 100 |  |  | 95 | my $top_dir = $conf->{top_dir} || 'auto'; | 
| 19 | 12 |  |  |  |  | 26 | $top_dir =~ s#^/##; | 
| 20 | 12 |  |  |  |  | 20 | $top_dir =~ s#/$##; | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | # Search templates | 
| 23 | 12 |  |  |  |  | 16 | my @templates; | 
| 24 | 12 |  |  |  |  | 18 | for my $template_base_dir (@$template_base_dirs) { | 
| 25 | 15 |  |  |  |  | 23 | $template_base_dir =~ s#/$##; | 
| 26 | 15 |  |  |  |  | 26 | my $template_dir = "$template_base_dir/$top_dir"; | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 15 | 50 |  |  |  | 305 | if (-d $template_dir) { | 
| 29 |  |  |  |  |  |  | # Find templates | 
| 30 |  |  |  |  |  |  | find(sub { | 
| 31 | 73 |  |  | 73 |  | 72 | my $template_abs = $File::Find::name; | 
| 32 | 73 |  |  |  |  | 58 | my $template = $template_abs; | 
| 33 | 73 |  |  |  |  | 340 | $template =~ s/\Q$template_dir\///; | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 73 | 100 |  |  |  | 1752 | if ($template =~ s/\.html\.ep$//) { | 
| 36 | 43 |  |  |  |  | 579 | push @templates, $template; | 
| 37 |  |  |  |  |  |  | } | 
| 38 | 15 |  |  |  |  | 894 | }, $template_dir); | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  | } | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | # Register routes | 
| 43 | 12 |  |  |  |  | 20 | for my $template (@templates) { | 
| 44 | 43 | 100 |  |  |  | 3633 | my $route_path = $template eq 'index' ? '/' : $template; | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | # Route | 
| 47 |  |  |  |  |  |  | $r->route("/$route_path") | 
| 48 | 43 |  |  | 40 |  | 135 | ->to(cb => sub { shift->render("/$top_dir/$template", 'mojo.maybe' => 1) }); | 
|  | 40 |  |  |  |  | 424138 |  | 
| 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 |  |  |  |  |  |  | Routes corresponding to URL is created . | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | TEMPLATES                           ROUTES | 
| 77 |  |  |  |  |  |  | templates/auto/index.html.ep        # / | 
| 78 |  |  |  |  |  |  | /foo.html.ep          # /foo | 
| 79 |  |  |  |  |  |  | /foo/bar.html.ep      # /foo/bar | 
| 80 |  |  |  |  |  |  | /foo/bar/baz.html.ep  # /foo/bar/baz | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | If you like C, this plugin is very good. | 
| 83 |  |  |  |  |  |  | You only put file into C directory. | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | =head1 EXAMPLE | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | use Mojolicious::Lite; | 
| 88 |  |  |  |  |  |  | use Mojolicious::Plugin::AutoRoute::Util 'template'; | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | # AutoRoute | 
| 91 |  |  |  |  |  |  | plugin 'AutoRoute'; | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | # Custom routes | 
| 94 |  |  |  |  |  |  | get '/create/:id' => template '/create'; | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | @@ auto/index.html.ep | 
| 97 |  |  |  |  |  |  | / | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | @@ auto/foo.html.ep | 
| 100 |  |  |  |  |  |  | /foo | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | @@ auto/bar.html.ep | 
| 103 |  |  |  |  |  |  | /bar | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | @@ auto/foo/bar/baz.html.ep | 
| 106 |  |  |  |  |  |  | /foo/bar/baz | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | @@ auto/json.html.ep | 
| 109 |  |  |  |  |  |  | <% | 
| 110 |  |  |  |  |  |  | $self->render(json => {foo => 1}); | 
| 111 |  |  |  |  |  |  | return; | 
| 112 |  |  |  |  |  |  | %> | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | @@ create.html.ep | 
| 115 |  |  |  |  |  |  | /create/<%= $id %> | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | =head1 OPTIONS | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | =head2 route | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | route => $route; | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | You can set parent route if you need. | 
| 124 |  |  |  |  |  |  | This is L object. | 
| 125 |  |  |  |  |  |  | Default is C<$app->routes>. | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | =head2 top_dir | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | top_dir => 'myauto' | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | Top directory. default is C. | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | =head1 TIPS | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | If you want to create custom route, use C method. | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | # Mojolicious Lite | 
| 138 |  |  |  |  |  |  | any '/foo' => sub { shift->render_maybe('/foo') }; | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | # Mojolicious | 
| 141 |  |  |  |  |  |  | $r->any('/foo' => sub { shift->render_maybe('/foo') }; | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | For backwrod comaptible, you can use C function. | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | use Mojolicious::Plugin::AutoRoute::Util 'template'; | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | # Mojolicious Lite | 
| 148 |  |  |  |  |  |  | any '/foo' => template '/foo';s | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | # Mojolicious | 
| 151 |  |  |  |  |  |  | $r->any('/foo' => template '/foo'); | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | =head1 METHOD | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | =head2 register | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | $plugin->register($app); | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | Register plugin in L application. | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | =head1 CAUTION | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | This plugin depend on Mojolicious internal structure. | 
| 164 |  |  |  |  |  |  | I try to keep this module work well and  backword compatible, | 
| 165 |  |  |  |  |  |  | but I don't guarantee it. | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | L, L, L. | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  | =cut |