| 1 | package Drogo::Dispatch; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Exporter; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Drogo::Guts; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Drogo::Dispatcher; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Drogo::Server::Test; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | # Configure exporter. | ||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | our @ISA = qw(Exporter Drogo::Dispatcher); | |||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | our @EXPORT = (@Nginx::Simple::HTTP_STATUS_CODES); | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | Drogo::Dispatch - Dispatching framework for Drogo | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | For an example on using the dispatcher, please see L<Drogo>. | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | use Drogo::Dispatch( auto_import => 1 ); | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | Parameters: | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | import_drogo_methods - Inject methods from L<Drogo::Guts> into dispatched class, which is deprecated. | ||||||||||||||||||||||||||||||||||||||||||
| 27 | auto_import - Automatically load modules when they are dispatched, you probably do not want this in a production application. | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | Mapping: Drogo can map to entirely different modules with the mapping hash. | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | use Drogo::Disaptch ( mapping => { | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 'tornado' => 'Tornado::App', | ||||||||||||||||||||||||||||||||||||||||||
| 35 | } ); | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | sub import | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | my ($class, %params) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 1 | ▶ | my $caller = $params{class} || caller; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$params{'class'} || caller
MC/DC0%0/2
$params{'class'} || caller
$params{'class'}
caller
Truth table33%1/3
A || B
A= $params{'class'}B= caller
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | # inject a handler method | ||||||||||||||||||||||||||||||||||||||||||
| 45 | { | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 1 | my $caller_isa = "$caller\::ISA"; | |||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | @{$caller_isa} = qw( | |||||||||||||||||||||||||||||||||||||||||
| 51 | Drogo::Dispatcher | ||||||||||||||||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | # import_drogo_methods is deprecated | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | push @{$caller_isa}, 'Drogo::Guts' | |||||||||||||||||||||||||||||||||||||||||
| 56 | 1 | ▶ | if $params{import_drogo_methods}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 1 | ▶ | *{"$caller\::handler"} = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.4
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 15 | my ($self, %custom_params) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 15 | ▶ | my $server_obj = $custom_params{server} || $self; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$custom_params{'server'} || $self
MC/DC0%0/2
$custom_params{'server'} || $self
$custom_params{'server'}
$self
Truth table33%1/3
A || B
A= $custom_params{'server'}B= $self
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | return local_dispatch( | ||||||||||||||||||||||||||||||||||||||||||
| 63 | $server_obj, | ||||||||||||||||||||||||||||||||||||||||||
| 64 | class => $caller, | ||||||||||||||||||||||||||||||||||||||||||
| 65 | app_path => $params{app_path}, | ||||||||||||||||||||||||||||||||||||||||||
| 66 | auto_import => $params{auto_import}, | ||||||||||||||||||||||||||||||||||||||||||
| 67 | auto_redirect => $params{auto_redirect}, | ||||||||||||||||||||||||||||||||||||||||||
| 68 | mapping => $params{mapping}, | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 15 | %custom_params, | |||||||||||||||||||||||||||||||||||||||||
| 70 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 1 | __PACKAGE__->export_to_level(1, $class); | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | # where do we dispatch to | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | sub local_dispatch | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 15 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
local_dispatch: called CC=12 SCAR=30.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 15 | my ($self, %params) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 15 | my $class = $params{class}; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 15 | ▶ | my $app_path = $params{app_path} || ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$params{'app_path'} || ''
MC/DC0%0/1
$params{'app_path'} || ''
$params{'app_path'}
Truth table50%1/2
A
A= $params{'app_path'}
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 15 | ▶ | my $path = $params{uri} || $self->uri; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$params{'uri'} || $self->uri
MC/DC0%0/2
$params{'uri'} || $self->uri
$params{'uri'}
$self->uri
Truth table33%1/3
A || B
A= $params{'uri'}B= $self->uri
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | # self should be a server object, if it's not create a fake one | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 15 | ▶ | $self = Drogo::Server::Test->new(%params) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | unless ref $self; | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | # trip the app_path off $path, when applicable | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 15 | ▶ | $path =~ s/^$app_path// if $app_path; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | my $dispatch_data = | ||||||||||||||||||||||||||||||||||||||||||
| 94 | __PACKAGE__->dig_for_dispatch( | ||||||||||||||||||||||||||||||||||||||||||
| 95 | class => $class, | ||||||||||||||||||||||||||||||||||||||||||
| 96 | path => $path, | ||||||||||||||||||||||||||||||||||||||||||
| 97 | auto_import => $params{auto_import}, | ||||||||||||||||||||||||||||||||||||||||||
| 98 | mapping => $params{mapping}, | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 15 | ); | |||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | # ensure indexes always end in a slash | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 15 | ▶ | if ($params{auto_redirect} and $dispatch_data->{index} and $path !~ /\/$/) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$params{"auto_redirect"} and $dispatch_data->{"index"} and not $path =~ m[/$]
$params{"auto_redirect"} and $dispatch_data->{"index"}
MC/DC0%0/3
$params{"auto_redirect"} and $dispatch_data->{"index"} and not $path =~ m[/$]
$params{"auto_redirect"}
$dispatch_data->{"index"}
not $path =~ m[/$]
Truth table25%1/4
A and B and not(C)
A= $params{"auto_redirect"}B= $dispatch_data->{"index"}C= not $path =~ m[/$]
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | { | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | $self->status(302); | |||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | $self->header_out(Location => $self->uri . '/'); | |||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | $self->send_http_header; | |||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 15 | ▶ | if ($dispatch_data->{error} and $dispatch_data->{error} eq 'bad_dispatch') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$dispatch_data->{"error"} and $dispatch_data->{"error"} eq "bad_dispatch"
MC/DC50%1/2
$dispatch_data->{"error"} and $dispatch_data->{"error"} eq "bad_dispatch"
$dispatch_data->{"error"}
$dispatch_data->{"error"} eq "bad_dispatch"
Truth table66%2/3
A and B
A= $dispatch_data->{"error"}B= $dispatch_data->{"error"} eq "bad_dispatch"
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | { | ||||||||||||||||||||||||||||||||||||||||||
| 113 | return dispatch( | ||||||||||||||||||||||||||||||||||||||||||
| 114 | $self, | ||||||||||||||||||||||||||||||||||||||||||
| 115 | class => $class, | ||||||||||||||||||||||||||||||||||||||||||
| 116 | method => 'bad_dispatch', | ||||||||||||||||||||||||||||||||||||||||||
| 117 | bless => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 118 | psgi => $params{psgi}, | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 2 | ); | |||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 121 | elsif ($dispatch_data->{error}) | ||||||||||||||||||||||||||||||||||||||||||
| 122 | { | ||||||||||||||||||||||||||||||||||||||||||
| 123 | return dispatch( | ||||||||||||||||||||||||||||||||||||||||||
| 124 | $self, | ||||||||||||||||||||||||||||||||||||||||||
| 125 | class => $class, | ||||||||||||||||||||||||||||||||||||||||||
| 126 | method => 'error', | ||||||||||||||||||||||||||||||||||||||||||
| 127 | error => $dispatch_data->{error}, | ||||||||||||||||||||||||||||||||||||||||||
| 128 | bless => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 129 | psgi => $params{psgi}, | ||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | else # prepare to dispatch for real | ||||||||||||||||||||||||||||||||||||||||||
| 133 | { | ||||||||||||||||||||||||||||||||||||||||||
| 134 | return dispatch( | ||||||||||||||||||||||||||||||||||||||||||
| 135 | $self, | ||||||||||||||||||||||||||||||||||||||||||
| 136 | class => $dispatch_data->{class}, | ||||||||||||||||||||||||||||||||||||||||||
| 137 | method => $dispatch_data->{method}, | ||||||||||||||||||||||||||||||||||||||||||
| 138 | base_class => $class, | ||||||||||||||||||||||||||||||||||||||||||
| 139 | dispatch_url => $dispatch_data->{dispatch_url}, | ||||||||||||||||||||||||||||||||||||||||||
| 140 | bless => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 141 | post_args => ($dispatch_data->{post_args} || [ ]), | ||||||||||||||||||||||||||||||||||||||||||
| 142 | psgi => $params{psgi}, | ||||||||||||||||||||||||||||||||||||||||||
| 143 | 13 | ▶ | ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$dispatch_data->{'post_args'} || []
MC/DC100%1/1
$dispatch_data->{'post_args'} || []
$dispatch_data->{'post_args'}
Truth table100%2/2
A
A= $dispatch_data->{'post_args'}
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | =head1 AUTHORS | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | Bizowie <http://bizowie.com> | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | Copyright (C) 2013 Bizowie | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | This library is free software. You can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||