| 1 | package PAD; | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | our $VERSION = '0.04'; | ||
| 5 | 0 | ▶ | use Plack::Request; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use Plack::App::Directory; | ||
| 7 | |||
| 8 | sub new { | ||
| 9 | my ($class, %args) = @_; | ||
| 10 | my $plugin = $args{plugin}; | ||
| 11 | |||
| 12 | __PACKAGE__->require($plugin); | ||
| 13 | |||
| 14 | bless { | ||
| 15 | plugin => $plugin, | ||
| 16 | args => \%args, | ||
| 17 | }, $class; | ||
| 18 | } | ||
| 19 | |||
| 20 | sub plugin { shift->{plugin} } | ||
| 21 | |||
| 22 | sub psgi_app { | ||
| 23 | my $self = shift; | ||
| 24 | |||
| 25 | return sub { | ||
| 26 | my $req = Plack::Request->new(shift); | ||
| 27 | |||
| 28 | my $path = $req->path_info; | ||
| 29 | $path =~ s/[\/\\\0]//g; | ||
| 30 | if ($path eq '/' || $path eq 'favicon.ico' || not $path) { | ||
| 31 | return Plack::App::Directory->new->to_app->($req->env); | ||
| 32 | } | ||
| 33 | |||
| 34 | my $plugin = $self->plugin->new( | ||
| 35 | %{ $self->{args} }, | ||
| 36 | request => $req, | ||
| 37 | ); | ||
| 38 | |||
| 39 | return $req->path_info =~ $plugin->suffix | ||
| 40 | ? $plugin->execute | ||
| 41 | : Plack::App::Directory->new->to_app->($req->env); | ||
| 42 | }; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub require { | ||
| 46 | my (undef, $class) = @_; | ||
| 47 | unless ($class->can("new")) { | ||
| 48 | my $path = $class; | ||
| 49 | $path =~ s|::|/|g; | ||
| 50 | require "$path.pm"; ## no critic | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | 1; | ||
| 55 | __END__ | ||