| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Web::Dispatch; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 15 |  |  | 15 |  | 49793 | use Sub::Quote; | 
|  | 15 |  |  |  |  | 15638 |  | 
|  | 15 |  |  |  |  | 1942 |  | 
| 4 | 15 |  |  | 15 |  | 80 | use Scalar::Util qw(blessed); | 
|  | 15 |  |  |  |  | 27 |  | 
|  | 15 |  |  |  |  | 2161 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 109 |  |  | 109 | 0 | 701 | sub MAGIC_MIDDLEWARE_KEY { __PACKAGE__.'.middleware' } | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 15 |  |  | 15 |  | 935 | use Moo; | 
|  | 15 |  |  |  |  | 2812 |  | 
|  | 15 |  |  |  |  | 391 |  | 
| 9 | 15 |  |  | 15 |  | 26169 | use Web::Dispatch::Parser; | 
|  | 15 |  |  |  |  | 47 |  | 
|  | 15 |  |  |  |  | 475 |  | 
| 10 | 15 |  |  | 15 |  | 9097 | use Web::Dispatch::Node; | 
|  | 15 |  |  |  |  | 50 |  | 
|  | 15 |  |  |  |  | 14039 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | with 'Web::Dispatch::ToApp'; | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | has dispatch_app => ( | 
| 15 | 1 |  |  | 1 |  | 18 | is => 'lazy', builder => sub { shift->dispatch_object->to_app } | 
| 16 |  |  |  |  |  |  | ); | 
| 17 |  |  |  |  |  |  | has dispatch_object => (is => 'ro', required => 0, weak_ref => 1); | 
| 18 |  |  |  |  |  |  | has parser_class => ( | 
| 19 |  |  |  |  |  |  | is => 'ro', default => quote_sub q{ 'Web::Dispatch::Parser' } | 
| 20 |  |  |  |  |  |  | ); | 
| 21 |  |  |  |  |  |  | has node_class => ( | 
| 22 |  |  |  |  |  |  | is => 'ro', default => quote_sub q{ 'Web::Dispatch::Node' } | 
| 23 |  |  |  |  |  |  | ); | 
| 24 |  |  |  |  |  |  | has _parser => (is => 'lazy'); | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | after BUILDARGS => sub { | 
| 27 |  |  |  |  |  |  | my ( $self, %args ) = @_; | 
| 28 |  |  |  |  |  |  | die "Either dispatch_app or dispatch_object need to be supplied." | 
| 29 |  |  |  |  |  |  | if !$args{dispatch_app} and !$args{dispatch_object} | 
| 30 |  |  |  |  |  |  | }; | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | sub _build__parser { | 
| 33 | 13 |  |  | 13 |  | 4794 | my ($self) = @_; | 
| 34 | 13 |  |  |  |  | 201 | $self->parser_class->new; | 
| 35 |  |  |  |  |  |  | } | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | sub call { | 
| 38 | 68 |  |  | 68 | 0 | 299 | my ($self, $env) = @_; | 
| 39 | 68 |  |  |  |  | 1353 | my $res = $self->_dispatch($env, $self->dispatch_app); | 
| 40 | 62 | 100 | 100 |  |  | 581 | return $res->[0] if ref($res) eq 'ARRAY' and @{$res} == 1 and ref($res->[0]) eq 'CODE'; | 
|  | 61 |  | 100 |  |  | 296 |  | 
| 41 | 59 |  |  |  |  | 395 | return $res; | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | sub _dispatch { | 
| 45 | 267 |  |  | 267 |  | 6736 | my ($self, $env, @match) = @_; | 
| 46 | 267 |  |  |  |  | 694 | while (defined(my $try = shift @match)) { | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 645 | 100 |  |  |  | 1409 | return $try if ref($try) eq 'ARRAY'; | 
| 49 | 641 | 100 |  |  |  | 1285 | if (ref($try) eq 'HASH') { | 
| 50 | 94 |  |  |  |  | 1340 | $env = { 'Web::Dispatch.original_env' => $env, %$env, %$try }; | 
| 51 | 94 |  |  |  |  | 398 | next; | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 547 |  |  |  |  | 1575 | my @result = $self->_to_try($try, \@match)->($env, @match); | 
| 55 | 538 | 100 | 100 |  |  | 28088 | next unless @result and defined($result[0]); | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 448 |  |  |  |  | 617 | my $first = $result[0]; | 
| 58 |  |  |  |  |  |  |  | 
| 59 | 448 | 100 |  |  |  | 1251 | if (my $res = $self->_have_result($first, \@result, \@match, $env)) { | 
| 60 |  |  |  |  |  |  |  | 
| 61 | 249 |  |  |  |  | 1015 | return $res; | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | # make a copy so we don't screw with it assigning further up | 
| 65 | 197 |  |  |  |  | 275 | my $env = $env; | 
| 66 | 197 |  |  | 197 |  | 1087 | unshift @match, sub { $self->_dispatch($env, @result) }; | 
|  | 197 |  |  |  |  | 792 |  | 
| 67 |  |  |  |  |  |  | } | 
| 68 |  |  |  |  |  |  |  | 
| 69 | 3 |  |  |  |  | 10 | return; | 
| 70 |  |  |  |  |  |  | } | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | sub _have_result { | 
| 73 | 448 |  |  | 448 |  | 710 | my ($self, $first, $result, $match, $env) = @_; | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 448 | 100 | 100 |  |  | 2847 | if (ref($first) eq 'ARRAY') { | 
|  |  | 100 | 100 |  |  |  |  | 
|  |  | 100 | 100 |  |  |  |  | 
|  |  | 100 | 100 |  |  |  |  | 
| 76 | 234 |  |  |  |  | 673 | return $first; | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  | elsif (blessed($first) && $first->isa('Plack::Middleware')) { | 
| 79 | 7 |  |  |  |  | 20 | return $self->_uplevel_middleware($first, $result); | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  | elsif (ref($first) eq 'HASH' and $first->{+MAGIC_MIDDLEWARE_KEY}) { | 
| 82 | 5 |  |  |  |  | 13 | return $self->_redispatch_with_middleware($first, $match, $env); | 
| 83 |  |  |  |  |  |  | } | 
| 84 |  |  |  |  |  |  | elsif ( | 
| 85 |  |  |  |  |  |  | blessed($first) && | 
| 86 |  |  |  |  |  |  | not($first->can('to_app')) && | 
| 87 |  |  |  |  |  |  | not($first->isa('Web::Dispatch::Matcher')) | 
| 88 |  |  |  |  |  |  | ) { | 
| 89 | 5 |  |  |  |  | 16 | return $first; | 
| 90 |  |  |  |  |  |  | } | 
| 91 | 197 |  |  |  |  | 581 | return; | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | sub _uplevel_middleware { | 
| 95 | 7 |  |  | 7 |  | 12 | my ($self, $match, $results) = @_; | 
| 96 |  |  |  |  |  |  | die "Multiple results but first one is a middleware ($match)" | 
| 97 | 7 | 100 |  |  |  | 10 | if @{$results} > 1; | 
|  | 7 |  |  |  |  | 80 |  | 
| 98 |  |  |  |  |  |  | # middleware needs to uplevel exactly once to wrap the rest of the | 
| 99 |  |  |  |  |  |  | # level it was created for - next elsif unwraps it | 
| 100 | 5 |  |  |  |  | 9 | return { MAGIC_MIDDLEWARE_KEY, $match }; | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | sub _redispatch_with_middleware { | 
| 104 | 5 |  |  | 5 |  | 9 | my ($self, $first, $match, $env) = @_; | 
| 105 |  |  |  |  |  |  |  | 
| 106 | 5 |  |  |  |  | 10 | my $mw = $first->{+MAGIC_MIDDLEWARE_KEY}; | 
| 107 |  |  |  |  |  |  |  | 
| 108 | 5 |  |  | 2 |  | 33 | $mw->app(sub { $self->_dispatch($_[0], @{$match}) }); | 
|  | 2 |  |  |  |  | 44 |  | 
|  | 2 |  |  |  |  | 7 |  | 
| 109 |  |  |  |  |  |  |  | 
| 110 | 5 |  |  |  |  | 43 | return $mw->to_app->($env); | 
| 111 |  |  |  |  |  |  | } | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | sub _to_try { | 
| 114 | 547 |  |  | 547 |  | 780 | my ($self, $try, $more) = @_; | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | # sub () {}      becomes a dispatcher | 
| 117 |  |  |  |  |  |  | # sub {}               is a PSGI app and can be returned as is | 
| 118 |  |  |  |  |  |  | # '' => sub {}   becomes a dispatcher | 
| 119 |  |  |  |  |  |  | # $obj isa WD:Predicates::Matcher => sub { ... } -  become a dispatcher | 
| 120 |  |  |  |  |  |  | # $obj w/to_app method is a Plack::App-like thing - call it to get a PSGI app | 
| 121 |  |  |  |  |  |  | # | 
| 122 |  |  |  |  |  |  |  | 
| 123 | 547 | 100 | 66 |  |  | 1812 | if (ref($try) eq 'CODE') { | 
|  |  | 100 | 66 |  |  |  |  | 
|  |  | 100 | 100 |  |  |  |  | 
|  |  | 100 | 100 |  |  |  |  | 
|  |  |  | 100 |  |  |  |  | 
| 124 | 490 | 100 |  |  |  | 1378 | if (defined(my $proto = prototype($try))) { | 
| 125 | 134 |  |  |  |  | 312 | $self->_construct_node(match => $proto, run => $try); | 
| 126 |  |  |  |  |  |  | } else { | 
| 127 | 356 |  |  |  |  | 1099 | $try | 
| 128 |  |  |  |  |  |  | } | 
| 129 |  |  |  |  |  |  | } elsif (!ref($try) | 
| 130 |  |  |  |  |  |  | and (ref($more->[0]) eq 'CODE' | 
| 131 |  |  |  |  |  |  | or ($more->[0] and !ref($more->[0]) and $self->dispatch_object | 
| 132 |  |  |  |  |  |  | and $self->dispatch_object->can($more->[0]))) | 
| 133 |  |  |  |  |  |  | ) { | 
| 134 | 4 |  |  |  |  | 41 | $self->_construct_node(match => $try, run => shift(@$more)); | 
| 135 |  |  |  |  |  |  | } elsif ( | 
| 136 |  |  |  |  |  |  | (blessed($try) && $try->isa('Web::Dispatch::Matcher')) | 
| 137 |  |  |  |  |  |  | and (ref($more->[0]) eq 'CODE') | 
| 138 |  |  |  |  |  |  | ) { | 
| 139 | 42 |  |  |  |  | 97 | $self->_construct_node(match => $try, run => shift(@$more)); | 
| 140 |  |  |  |  |  |  | } elsif (blessed($try) && $try->can('to_app')) { | 
| 141 | 7 |  |  |  |  | 22 | $try->to_app; | 
| 142 |  |  |  |  |  |  | } else { | 
| 143 | 4 |  |  |  |  | 42 | die "No idea how we got here with $try"; | 
| 144 |  |  |  |  |  |  | } | 
| 145 |  |  |  |  |  |  | } | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | sub _construct_node { | 
| 148 | 180 |  |  | 180 |  | 823 | my ($self, %args) = @_; | 
| 149 | 180 | 100 |  |  |  | 4057 | $args{match} = $self->_parser->parse($args{match}) if !ref $args{match}; | 
| 150 | 180 | 100 |  |  |  | 2780 | if ( my $obj = $self->dispatch_object) { | 
| 151 |  |  |  |  |  |  | # if possible, call dispatchers as methods of the app object | 
| 152 | 179 |  |  |  |  | 296 | my $dispatch_sub = $args{run}; | 
| 153 | 179 |  |  | 93 |  | 655 | $args{run} = sub { $obj->$dispatch_sub(@_) }; | 
|  | 93 |  |  |  |  | 259 |  | 
| 154 |  |  |  |  |  |  | } | 
| 155 | 180 |  |  |  |  | 5374 | $self->node_class->new(\%args)->to_app; | 
| 156 |  |  |  |  |  |  | } | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | 1; |