| 1 | package Plack::App::Path::Router::Custom; | ||||||||||
| 2 | 14 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 14 | $Plack::App::Path::Router::Custom::AUTHORITY = 'cpan:STEVAN'; | |||||||||
| 4 | } | ||||||||||
| 5 | 14 | $Plack::App::Path::Router::Custom::VERSION = '0.08'; | |||||||||
| 6 | 14 | ▶ | use Moose 0.90; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 14 | ▶ | use MooseX::NonMoose 0.07; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 14 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | # ABSTRACT: A Plack component for dispatching with Path::Router | ||||||||||
| 10 | |||||||||||
| 11 | 14 | extends 'Plack::Component'; | |||||||||
| 12 | |||||||||||
| 13 | |||||||||||
| 14 | |||||||||||
| 15 | 14 | has 'router' => ( | |||||||||
| 16 | is => 'ro', | ||||||||||
| 17 | isa => 'Path::Router', | ||||||||||
| 18 | required => 1, | ||||||||||
| 19 | ); | ||||||||||
| 20 | |||||||||||
| 21 | |||||||||||
| 22 | has new_request => ( | ||||||||||
| 23 | traits => ['Code'], | ||||||||||
| 24 | isa => 'CodeRef', | ||||||||||
| 25 | 3 | ▶ | default => sub { sub { $_[0] } }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 14 | handles => { | |||||||||
| 27 | new_request => 'execute', | ||||||||||
| 28 | }, | ||||||||||
| 29 | ); | ||||||||||
| 30 | |||||||||||
| 31 | |||||||||||
| 32 | has target_to_app => ( | ||||||||||
| 33 | traits => ['Code'], | ||||||||||
| 34 | isa => 'CodeRef', | ||||||||||
| 35 | 1 | ▶ | default => sub { sub { $_[0] } }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 36 | 14 | handles => { | |||||||||
| 37 | target_to_app => 'execute', | ||||||||||
| 38 | }, | ||||||||||
| 39 | ); | ||||||||||
| 40 | |||||||||||
| 41 | |||||||||||
| 42 | has handle_response => ( | ||||||||||
| 43 | traits => ['Code'], | ||||||||||
| 44 | isa => 'CodeRef', | ||||||||||
| 45 | 2 | ▶ | default => sub { sub { $_[0] } }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 46 | 14 | handles => { | |||||||||
| 47 | handle_response => 'execute', | ||||||||||
| 48 | }, | ||||||||||
| 49 | ); | ||||||||||
| 50 | |||||||||||
| 51 | |||||||||||
| 52 | has handle_exception => ( | ||||||||||
| 53 | traits => ['Code'], | ||||||||||
| 54 | isa => 'CodeRef', | ||||||||||
| 55 | 0 | ▶ | default => sub { sub { die $_[0] } }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=1.2
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 56 | 14 | handles => { | |||||||||
| 57 | handle_exception => 'execute' , | ||||||||||
| 58 | }, | ||||||||||
| 59 | ); | ||||||||||
| 60 | |||||||||||
| 61 | |||||||||||
| 62 | 56 | ▶ | sub call { | ||||||||
|
Subroutine
call: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 63 | 56 | my ($self, $env) = @_; | |||||||||
| 64 | |||||||||||
| 65 | 56 | $env->{'plack.router'} = $self->router; | |||||||||
| 66 | |||||||||||
| 67 | 56 | my $req = $self->new_request( $env ); | |||||||||
| 68 | |||||||||||
| 69 | 56 | my $match = $self->router->match( $env->{PATH_INFO} ); | |||||||||
| 70 | |||||||||||
| 71 | 56 | ▶ | if ( $match ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 49 | $env->{'plack.router.match'} = $match; | |||||||||
| 73 | |||||||||||
| 74 | 49 | my $route = $match->route; | |||||||||
| 75 | 49 | my $mapping = $match->mapping; | |||||||||
| 76 | |||||||||||
| 77 | 49 | my @args; | |||||||||
| 78 | 49 | foreach my $component ( @{ $route->components } ) { | |||||||||
| 79 | 75 | my $name = $route->get_component_name( $component ); | |||||||||
| 80 | 75 | ▶ | next unless $name; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 40 | my $value = $mapping->{ $name }; | |||||||||
| 82 | 40 | ▶ | if (defined $value) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 34 | push @args => $value; | |||||||||
| 84 | 34 | $env->{ ('plack.router.match.args.' . $name) } = $value; | |||||||||
| 85 | } | ||||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 49 | $env->{ 'plack.router.match.args' } = \@args; | |||||||||
| 89 | |||||||||||
| 90 | 49 | my $target = $match->target; | |||||||||
| 91 | |||||||||||
| 92 | 49 | my $handled_response; | |||||||||
| 93 | |||||||||||
| 94 | 49 | ▶ | try { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 95 | 49 | my $app = $self->target_to_app( $target ); | |||||||||
| 96 | 49 | my $res = $app->( $req, @args ); | |||||||||
| 97 | |||||||||||
| 98 | 47 | $handled_response = $self->handle_response( $res, $req ); | |||||||||
| 99 | } | ||||||||||
| 100 | 2 | ▶ | catch { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 101 | 2 | $handled_response = $self->handle_exception( $_ ); | |||||||||
| 102 | 49 | }; | |||||||||
| 103 | |||||||||||
| 104 | 49 | return $handled_response; | |||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | 7 | return $self->handle_response( | |||||||||
| 108 | [ 404, [ 'Content-Type' => 'text/html' ], [ 'Not Found' ] ] , | ||||||||||
| 109 | $req , | ||||||||||
| 110 | ); | ||||||||||
| 111 | } | ||||||||||
| 112 | |||||||||||
| 113 | 14 | __PACKAGE__->meta->make_immutable; | |||||||||
| 114 | 14 | ▶ | no Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 115 | |||||||||||
| 116 | |||||||||||
| 117 | 14 | 1; | |||||||||
| 118 | |||||||||||
| 119 | __END__ | ||||||||||