lib/Drogo/Dispatch.pm

filter: Statementstmt 90.3% 47 / 52 Branchbran 58.3% 7 / 12 Conditioncond 45.4% 10 / 22 MC/DCmcdc 15.3% 2 / 13 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 1 totaltotal 68.8% 75 / 109 CC 15 SCAR 24.9
CC
15
Cov
74%
local_dispatch
21.5
import
3.1
__ANON__
2.1
CRAP 12.1
« lib/Drogo/Dispatcher.pm ↑ Summary blib/lib/Drogo/Server/Nginx.pm »
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
l!l&&r!l&&!r
010
MC/DC0%0/2
$params{'class'} || caller
$params{'class'} caller
Truth table33%1/3
A || B
A= $params{'class'}B= caller
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if $params{"import_drogo_methods"} 1 0 1
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
l!l&&r!l&&!r
0150
MC/DC0%0/2
$custom_params{'server'} || $self
$custom_params{'server'} $self
Truth table33%1/3
A || B
A= $custom_params{'server'}B= $self
ABresult
1X1
011
000
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'} || ''
l!l
015
MC/DC0%0/1
$params{'app_path'} || ''
$params{'app_path'}
Truth table50%1/2
A
A= $params{'app_path'}
Aresult
11
00
84 15 ▶ my $path = $params{uri} || $self->uri;
Condition33%1/3
$params{'uri'} || $self->uri
l!l&&r!l&&!r
1500
MC/DC0%0/2
$params{'uri'} || $self->uri
$params{'uri'} $self->uri
Truth table33%1/3
A || B
A= $params{'uri'}B= $self->uri
ABresult
1X1
011
000
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
BranchTrueFalseTotal
unless ref $self 15 0 15
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
BranchTrueFalseTotal
if $app_path 0 15 15
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
BranchTrueFalseTotal
if ($params{"auto_redirect"} and $dispatch_data->{"index"} and not $path =~ m[/$]) 0 15 15
Condition33%2/6
$params{"auto_redirect"} and $dispatch_data->{"index"} and not $path =~ m[/$]
!ll&&!rl&&r
1500
$params{"auto_redirect"} and $dispatch_data->{"index"}
!ll&&!rl&&r
1500
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[/$]
ABCresult
0XX0
10X0
1100
1111
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
BranchTrueFalseTotal
if ($dispatch_data->{'error'} and $dispatch_data->{'error'} eq 'bad_dispatch') { } 2 13 15
elsif ($dispatch_data->{'error'}) { } 0 13 13
Condition66%2/3
$dispatch_data->{"error"} and $dispatch_data->{"error"} eq "bad_dispatch"
!ll&&!rl&&r
1302
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"
ABresult
0X0
100
111
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'} || []
l!l
310
MC/DC100%1/1
$dispatch_data->{'post_args'} || []
$dispatch_data->{'post_args'}
Truth table100%2/2
A
A= $dispatch_data->{'post_args'}
Aresult
11
00
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;
« lib/Drogo/Dispatcher.pm ↑ Summary blib/lib/Drogo/Server/Nginx.pm »