« blib/script/amon2-setup.pl
↑ Summary
blib/lib/auto/share/dist/Amon2/flavor/Minimum/builder/MyBuilder.pm »
| 1 | package Amon2::Web::Dispatcher::RouterSimple; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Router::Simple 0.03; | ||
| 5 | |||
| 6 | sub import { | ||
| 7 | my $class = shift; | ||
| 8 | my %args = @_; | ||
| 9 | my $caller = caller(0); | ||
| 10 | |||
| 11 | my $router = Router::Simple->new(); | ||
| 12 | |||
| 13 | no strict 'refs'; | ||
| 14 | # functions | ||
| 15 | *{"${caller}::connect"} = sub { | ||
| 16 | if (@_ == 2 && !ref $_[1]) { | ||
| 17 | my ($path, $dest_str) = @_; | ||
| 18 | my ($controller, $action) = split('#', $dest_str); | ||
| 19 | my %dest; | ||
| 20 | $dest{controller} = $controller; | ||
| 21 | $dest{action} = $action if defined $action; | ||
| 22 | $router->connect($path, \%dest); | ||
| 23 | } else { | ||
| 24 | $router->connect(@_); | ||
| 25 | } | ||
| 26 | }; | ||
| 27 | *{"${caller}::submapper"} = sub { | ||
| 28 | $router->submapper(@_); | ||
| 29 | }; | ||
| 30 | # class methods | ||
| 31 | *{"${caller}::router"} = sub { $router }; | ||
| 32 | for my $meth (qw/match as_string/) { | ||
| 33 | *{"$caller\::${meth}"} = sub { | ||
| 34 | my $self = shift; | ||
| 35 | $router->$meth(@_) | ||
| 36 | }; | ||
| 37 | } | ||
| 38 | *{"$caller\::dispatch"} = \&_dispatch; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub _dispatch { | ||
| 42 | my ($class, $c) = @_; | ||
| 43 | my $req = $c->request; | ||
| 44 | if (my $p = $class->match($req->env)) { | ||
| 45 | my $action = $p->{action}; | ||
| 46 | $c->{args} = $p; | ||
| 47 | "@{[ ref $c ]}::C::$p->{controller}"->$action($c, $p); | ||
| 48 | } else { | ||
| 49 | $c->res_404(); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | 1; | ||
| 54 | __END__ |