« lib/Dancer2/Core/Role/HasEnvironment.pm
↑ Summary
blib/lib/auto/share/dist/Dancer2/skel/default/lib/AppFile.pm »
| 1 | package Dancer2::Core::Dispatcher; | ||||||||||
| 2 | # ABSTRACT: Class for dispatching request to the appropriate route handler | ||||||||||
| 3 | 178 | $Dancer2::Core::Dispatcher::VERSION = '2.3.0'; | |||||||||
| 4 | 178 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 178 | ▶ | use Dancer2::Core::Types; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 178 | ▶ | use Dancer2::Core::Request; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 178 | ▶ | use Dancer2::Core::Response; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | has apps => ( | ||||||||||
| 11 | is => 'rw', | ||||||||||
| 12 | isa => ArrayRef, | ||||||||||
| 13 | 3 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 178 | ); | |||||||||
| 15 | |||||||||||
| 16 | 178 | has apps_psgi => ( | |||||||||
| 17 | is => 'ro', | ||||||||||
| 18 | isa => ArrayRef, | ||||||||||
| 19 | lazy => 1, | ||||||||||
| 20 | builder => '_build_apps_psgi', | ||||||||||
| 21 | ); | ||||||||||
| 22 | |||||||||||
| 23 | 45 | ▶ | sub _build_apps_psgi { | ||||||||
|
Subroutine
_build_apps_psgi: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 45 | my $self = shift; | |||||||||
| 25 | 45 | return [ map +( $_->name, $_->to_app ), @{ $self->apps } ]; | |||||||||
| 26 | } | ||||||||||
| 27 | |||||||||||
| 28 | 149 | ▶ | sub dispatch { | ||||||||
|
Subroutine
dispatch: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 29 | 149 | my ( $self, $env ) = @_; | |||||||||
| 30 | 149 | my @apps = @{ $self->apps_psgi }; | |||||||||
| 31 | |||||||||||
| 32 | 149 | DISPATCH: while (1) { | |||||||||
| 33 | 168 | ▶ | for ( my $i = 0; $i < @apps; $i += 2 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 220 | my ( $app_name, $app ) = @apps[ $i, $i + 1 ]; | |||||||||
| 35 | |||||||||||
| 36 | 220 | my $response = $app->($env); | |||||||||
| 37 | |||||||||||
| 38 | # check for an internal request | ||||||||||
| 39 | 220 | ▶ | delete Dancer2->runner->{'internal_forward'} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | and next DISPATCH; | ||||||||||
| 41 | |||||||||||
| 42 | # the app raised a flag saying it couldn't match anything | ||||||||||
| 43 | # which is different than "I matched and it's a 404" | ||||||||||
| 44 | delete Dancer2->runner->{'internal_404'} | ||||||||||
| 45 | 201 | ▶ | or do { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 139 | delete Dancer2->runner->{'internal_request'}; | |||||||||
| 47 | 139 | return $response; | |||||||||
| 48 | }; | ||||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | # don't run anymore | ||||||||||
| 52 | 10 | delete Dancer2->runner->{'internal_request'}; | |||||||||
| 53 | 10 | last; | |||||||||
| 54 | } # while | ||||||||||
| 55 | |||||||||||
| 56 | # a 404 on all apps, using the first app | ||||||||||
| 57 | 10 | my $default_app = $self->apps->[0]; | |||||||||
| 58 | 10 | my $request = $default_app->build_request($env); | |||||||||
| 59 | 10 | return $default_app->response_not_found($request)->to_psgi; | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | 178 | 1; | |||||||||
| 63 | |||||||||||
| 64 | __END__ | ||||||||||