lib/Drogo/Dispatcher.pm

filter: Statementstmt 76.9% 87 / 113 Branchbran 60.4% 29 / 48 Conditioncond 55.5% 10 / 18 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 58.3% 7 / 12 Podpod 80.0% 4 / 5 totaltotal 67.6% 140 / 207 CC 35 SCAR 34.5
CC
35
Cov
70%
dig_for_dispatch
41.5
_class_is_imported
30.0
server
2.0
CRAP 31.6
« lib/Drogo/Guts.pm ↑ Summary lib/Drogo/Dispatch.pm »
1 package Drogo::Dispatcher;
2  
3 1 ▶use base qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
4 Exporter
5 Drogo::Dispatcher::Attributes
6 1 );
7  
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Drogo::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Drogo::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Drogo::RequestResponse;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our @EXPORT = qw(dig_for_dispatch);
15  
16 # keep a list of dispatched paths
17 1 my %path_cache;
18  
19 =head1 NAME
20  
21 Drogo::Dispatcher - Internals for Drogo dispatching
22  
23 =head1 Synopsis
24  
25 Automatic dispatcher built on code attributes.
26  
27 =head1 Methods
28  
29 =cut
30  
31 sub dig_for_dispatch
32 16 ▶{
Subroutine dig_for_dispatch: called CC=31 SCAR=37.3
Pod undocumented
33 16 my ($self, %params) = @_;
34 16 my $class = $params{class};
35 16 my $path = $params{path};
36 16 ▶ my $mapping = $params{mapping} || {};
Condition50%1/2
$params{'mapping'} || {}
l!l
016
MC/DC0%0/1
$params{'mapping'} || {}
$params{'mapping'}
Truth table50%1/2
A
A= $params{'mapping'}
Aresult
11
00
37 16 ▶ my $called_path = $path || $params{called_path}; # the complete path
Condition66%2/3
$path || $params{'called_path'}
l!l&&r!l&&!r
1510
MC/DC0%0/2
$path || $params{'called_path'}
$path $params{'called_path'}
Truth table66%2/3
A || B
A= $pathB= $params{'called_path'}
ABresult
1X1
011
000
38 16 ▶ my $dispatch_url = $params{dispatch_url} || '';
Condition100%2/2
$params{'dispatch_url'} || ''
l!l
115
MC/DC100%1/1
$params{'dispatch_url'} || ''
$params{'dispatch_url'}
Truth table100%2/2
A
A= $params{'dispatch_url'}
Aresult
11
00
39 16 ▶ my $trailing = $params{trailing} || []; # unmatched trailing arguments
Condition50%1/2
$params{'trailing'} || []
l!l
016
MC/DC0%0/1
$params{'trailing'} || []
$params{'trailing'}
Truth table50%1/2
A
A= $params{'trailing'}
Aresult
11
00
40  
41 # reset self
42 16 ▶ $self = $params{self} if $params{self};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"self"} 1 15 16
43  
44 # dereference class
45 16 ▶ $class = ref $class ? ref $class : $class;
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 0 16 16
46  
47 # check cache (for fast dispatches)
48 return $path_cache{"${class}::${called_path}"}
49 16 ▶ if $path_cache{"${class}::${called_path}"};
Branch: 1 branch
BranchTrueFalseTotal
if $path_cache{"${class}::$called_path"} 3 13 16
50  
51 # change the class, if applicable to the mapping table
52 13 for my $new_class (keys %$mapping)
53 {
54 0 ▶ if ($new_class eq $path or
Branch: 1 branch
BranchTrueFalseTotal
if ($new_class eq $path or $path =~ m[^$new_class/]) 0 0 0
Condition0%0/3
$new_class eq $path or $path =~ m[^$new_class/]
l!l&&r!l&&!r
000
MC/DC0%0/2
$new_class eq $path or $path =~ m[^$new_class/]
$new_class eq $path $path =~ m[^$new_class/]
Truth table0%0/3
A or B
A= $new_class eq $pathB= $path =~ m[^$new_class/]
ABresult
1X1
011
000
55 $path =~ /^$new_class\//)
56 {
57 0 $class = $mapping->{$new_class};
58 0 $path =~ s/^$new_class//;
59 }
60 }
61  
62 # sanitize path
63 {
64 # remove starting slash
65 13 $path =~ s/^\///;
66  
67 # remove trailing slash
68 13 $path =~ s/\/+$//;
69  
70 # remove index trailing (you can't call index directly)
71 13 $path =~ s/(\/|^)index$//;
72  
73 # append 'index' if no path given
74 13 ▶ $path .= 'index' unless $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 2 11 13
75 }
76  
77 # build list of paths
78 13 my @paths = split('/', $path);
79  
80 # build method call
81 13 my $call_class = $class;
82 13 my $method = pop @paths;
83 13 my $remote_class = $class;
84 13 ▶ $remote_class = join('::', $class, join('::', @paths))
Branch: 1 branch
BranchTrueFalseTotal
if @paths 5 8 13
85 if @paths;
86  
87 13 ▶ &_class_is_imported($remote_class) if $params{auto_import};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"auto_import"} 0 13 13
88  
89 13 ▶ if (UNIVERSAL::can($remote_class, 'get_dispatch_flags'))
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($remote_class, 'get_dispatch_flags')) { } 10 3 13
90 {
91 10 my $methods = $remote_class->get_dispatch_flags;
92 10 my $used_index = 0;
93  
94 # if this is a page index, find the index sub name
95 10 ▶ if ($method eq 'index')
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "index") 2 8 10
96 {
97 2 ($method) = grep { $methods->{$_} eq 'index' } keys %$methods;
98 2 $used_index = 1;
99 }
100  
101 10 ▶ if ($methods->{$method})
Branch: 1 branch
BranchTrueFalseTotal
if ($methods->{$method}) { } 7 3 10
102 {
103 # perform dispatch
104 {
105 1 ▶ no strict 'refs'; # evil
Subroutine BEGIN: called CC=1 SCAR=0.0
106 7 my $subptr = join('::', $remote_class, $method);
107  
108 # store path in cache
109 7 $path_cache{"${class}::${called_path}"} = {
110 class => $remote_class,
111 method => $method,
112 sub => \&$subptr,
113 index => $used_index,
114 dispatch_url => join('/', $dispatch_url, $path),
115 };
116  
117 7 return $path_cache{"${class}::${called_path}"};
118 }
119 }
120 else
121 {
122 # attempt to jump forward
123 {
124 3 my $jump_class = join('::', $remote_class, $method);
125  
126 3 ▶ &_class_is_imported($jump_class) if $params{auto_import};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"auto_import"} 0 3 3
127 3 ▶ if (UNIVERSAL::can($jump_class, 'get_dispatch_flags'))
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($jump_class, "get_dispatch_flags")) 1 2 3
128 {
129 1 return $jump_class->dig_for_dispatch(
130 self => $self,
131 class => $jump_class,
132 path => '',
133 called_path => $called_path,
134 dispatch_url => $path,
135 );
136 }
137 }
138  
139 2 return { error => 'bad_dispatch' };
140 }
141 }
142 else # get_dispatch_flags is not assessable
143 {
144 # attempt to jump backward
145 {
146 3 my @jump_paths = @paths;
147 3 my @post_args = ($method);
148  
149 3 ▶ while (@jump_paths)
Branch: 1 branch
BranchTrueFalseTotal
if (@jump_paths) 5 0 5
150 {
151 5 my $method = pop @jump_paths;
152 5 my $jump_class = join('::', $class, @jump_paths);
153  
154 5 ▶ &_class_is_imported($jump_class) if $params{auto_import};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"auto_import"} 0 5 5
155  
156 5 ▶ if (UNIVERSAL::can($jump_class, 'get_dispatch_flags'))
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($jump_class, "get_dispatch_flags")) 3 2 5
157 {
158 3 my $dispatch_flags = $jump_class->get_dispatch_flags;
159  
160 3 ▶ if ($dispatch_flags->{$method} and
Branch: 1 branch
BranchTrueFalseTotal
if ($dispatch_flags->{$method} and $dispatch_flags->{$method} eq "action_match") 2 1 3
Condition66%2/3
$dispatch_flags->{$method} and $dispatch_flags->{$method} eq "action_match"
!ll&&!rl&&r
102
MC/DC50%1/2
$dispatch_flags->{$method} and $dispatch_flags->{$method} eq "action_match"
$dispatch_flags->{$method} $dispatch_flags->{$method} eq "action_match"
Truth table66%2/3
A and B
A= $dispatch_flags->{$method}B= $dispatch_flags->{$method} eq "action_match"
ABresult
0X0
100
111
161 $dispatch_flags->{$method} eq 'action_match')
162 {
163 2 my $subptr = join('::', $jump_class, $method);
164  
165 return {
166 2 class => $jump_class,
167 method => $method,
168 sub => \&$subptr,
169 index => 0,
170 dispatch_url => $called_path,
171 post_args => \@post_args,
172 };
173 }
174  
175 # check every action matching regex
176 1 for my $m (keys %$dispatch_flags)
177 {
178 3 my $a = $dispatch_flags->{$m};
179 3 my ($act, $attr) = split('-', $a);
180  
181 3 ▶ next if $act ne 'action_regex' and $act ne 'path';
Branch: 1 branch
BranchTrueFalseTotal
if $act ne "action_regex" and $act ne "path" 2 1 3
Condition66%2/3
$act ne "action_regex" and $act ne "path"
!ll&&!rl&&r
102
MC/DC50%1/2
$act ne "action_regex" and $act ne "path"
$act ne "action_regex" $act ne "path"
Truth table66%2/3
A and B
A= $act ne "action_regex"B= $act ne "path"
ABresult
0X0
100
111
182  
183 1 ▶ if ($act eq 'action_regex')
Branch: 2 branches
BranchTrueFalseTotal
if ($act eq 'action_regex') { } 1 0 1
elsif ($act eq 'path') { } 0 0 0
184 {
185 1 my $post_args = join('/', $method, @post_args);
186 1 my @results = ( $post_args =~ /$attr/ );
187  
188 1 ▶ if (@results)
Branch: 1 branch
BranchTrueFalseTotal
if (@results) 1 0 1
189 {
190 1 my $subptr = join('::', $jump_class, $m);
191  
192 return {
193 1 class => $jump_class,
194 method => $m,
195 sub => \&$subptr,
196 index => 0,
197 dispatch_url => $called_path,
198 post_args => \@results,
199 };
200 }
201 }
202 elsif ($act eq 'path')
203 {
204 0 my $post_args = join('/', $method, @post_args);
205  
206 0 my @results = ( $post_args =~ /^$attr$/ );
207  
208 0 ▶ if (@results)
Branch: 1 branch
BranchTrueFalseTotal
if (@results) 0 0 0
209 {
210 0 my $subptr = join('::', $jump_class, $m);
211  
212 return {
213 0 class => $jump_class,
214 method => $m,
215 sub => \&$subptr,
216 index => 0,
217 dispatch_url => $called_path,
218 };
219 }
220 }
221  
222 }
223 }
224  
225 2 unshift @post_args, $method;
226 }
227 }
228  
229 0 return { error => 'bad_dispatch' };
230 }
231 }
232  
233 # _class_is_imported(Some::Class)
234 #
235 # If a class is not imported, import it.
236 #
237  
238 sub _class_is_imported
239 0 ▶{
Subroutine _class_is_imported: not called CC=5 SCAR=34.0
240 0 my $class = shift;
241  
242 0 (my $class_file = $class) =~ s{::}{/}g;
243 0 $class_file .= '.pm'; # Let's assume all class files end in .pm
244  
245 0 ▶ if (not exists $INC{$class_file})
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $INC{$class_file}) 0 0 0
246 {
247 0 for my $base_path (@INC)
248 {
249 0 my $full_path = join('/', $base_path, $class_file);
250
251 0 ▶ if (-e $full_path)
Branch: 1 branch
BranchTrueFalseTotal
if (-e $full_path) 0 0 0
252 {
253 0 eval qq{use $class;};
254 0 ▶ warn "-->$@<--" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
255 0 return;
256 }
257 }
258 }
259 }
260  
261 =head2 server
262  
263 Returns server object.
264  
265 =cut
266  
267 0 ▶sub server { shift->r->server }
Subroutine server: not called CC=1 SCAR=6.9
Pod documented
268  
269 =head2 r
270  
271 Returns RequestResponse object.
272  
273 =cut
274  
275 sub r
276 0 ▶{
Subroutine r: not called CC=1 SCAR=6.9
Pod documented
277 0 my $self = shift;
278  
279 0 return Drogo::RequestResponse->new($self);
280 }
281  
282 1 *dispatcher = *r;
283  
284 =head2 request
285  
286 Returns Request object.
287  
288 =cut
289  
290 sub request
291 0 ▶{
Subroutine request: not called CC=1 SCAR=6.9
Pod documented
292 0 my $self = shift;
293  
294 0 return Drogo::Request->new($self);
295 }
296  
297 1 *req = *request;
298  
299 =head2 response
300  
301 Returns Response object.
302  
303 =cut
304  
305 sub response
306 0 ▶{
Subroutine response: not called CC=1 SCAR=6.9
Pod documented
307 0 my $self = shift;
308  
309 0 return Drogo::Response->new($self);
310 }
311  
312 1 *res = *response;
313  
314 =head1 AUTHORS
315  
316 Bizowie <http://bizowie.com>
317  
318 =head1 COPYRIGHT AND LICENSE
319  
320 Copyright (C) 2013 Bizowie
321  
322 This library is free software. You can redistribute it and/or modify it under the same terms as Perl itself.
323  
324 =cut
325  
326 1 1;
327  
« lib/Drogo/Guts.pm ↑ Summary lib/Drogo/Dispatch.pm »