Cake/Dispatcher.pm

filter: Statementstmt 32.9% 59 / 179 Branchbran 20.3% 13 / 64 Conditioncond 13.7% 8 / 58 MC/DCmcdc 0.0% 0 / 32 Subroutinesub 63.6% 7 / 11 Podpod 0.0% 0 / 5 totaltotal 24.9% 87 / 349 CC 58 SCAR 49.0
CC
58
Cov
27%
_loop_chains
306.0
match
140.3
addAction
72.0
CRAP 134.2
↑ Summary Cake/DBI/Abstract.pm »
1 package Cake::Dispatcher;
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6 #============================================================================
7 # setup
8 #============================================================================
9 3 ▶sub setup {
Subroutine setup: called CC=5 SCAR=20.3
Pod undocumented
10 3 my $self = shift;
11 #match current route
12 3 $self->match();
13 3 ▶ if (defined $self->controller and $self->controller->can('begin')){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->controller and $self->controller->can("begin")) 0 3 3
Condition33%1/3
defined $self->controller and $self->controller->can("begin")
!ll&&!rl&&r
300
MC/DC0%0/2
defined $self->controller and $self->controller->can("begin")
defined $self->controller $self->controller->can("begin")
Truth table33%1/3
A and B
A= defined $self->controllerB= $self->controller->can("begin")
ABresult
0X0
100
111
14 0 $self->controller->begin($self);
15 }
16
17 ##dispatch & excute
18 3 $self->dispatch();
19 3 ▶ if (defined $self->controller and $self->controller->can('end')){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->controller and $self->controller->can("end")) 0 3 3
Condition33%1/3
defined $self->controller and $self->controller->can("end")
!ll&&!rl&&r
300
MC/DC0%0/2
defined $self->controller and $self->controller->can("end")
defined $self->controller $self->controller->can("end")
Truth table33%1/3
A and B
A= defined $self->controllerB= $self->controller->can("end")
ABresult
0X0
100
111
20 0 $self->controller->end($self);
21 }
22 }
23
24 #============================================================================
25 # dispatch: sending to the match route and execute
26 #============================================================================
27 3 ▶sub dispatch {
Subroutine dispatch: called CC=5 SCAR=20.5
Pod undocumented
28 3 my $self = shift;
29 3 my $args = shift;
30
31 3 my $actionclass = $self->ActionClass;
32 3 my $controller = $self->controller;
33 3 my $code = $self->code;
34 3 ▶ $args ||= $self->action->{args};
Condition33%1/3
$args ||= $self->action->{"args"}
l!l&&r!l&&!r
003
MC/DC0%0/2
$args ||= $self->action->{"args"}
$args $self->action->{"args"}
Truth table33%1/3
A ||= B
A= $argsB= $self->action->{"args"}
ABresult
1X1
011
000
35
36 ##running actions
37 3 ▶ if ($actionclass){
Branch: 1 branch
BranchTrueFalseTotal
if ($actionclass) 0 3 3
38 0 $actionclass->execute($controller,$self,$code,$args);
39 }
40
41 ##running auto blocks
42 3 ▶ if (my $auto = $self->auto_chain->{ref $controller}){
Branch: 1 branch
BranchTrueFalseTotal
if (my $auto = $self->auto_chain->{ref $controller}) 0 3 3
43 0 my $line = $self->action->{line};
44 0 ▶ map { __PACKAGE__->execute($controller,$self,$_->{code},$args) if $_->{line} < $line } @{$auto};
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"line"} < $line 0 0 0
45 }
46
47 3 __PACKAGE__->execute($controller,$self,$code,$args);
48 }
49
50
51 #============================================================================
52 # match : match current request path with routes
53 #============================================================================
54 ##match sequence
55 ## 1) direct paths - 2) paths with defined arguments - 3) chained - 4) regex
56 3 ▶sub match {
Subroutine match: called CC=23 SCAR=49.4
Pod undocumented
57 3 my $self = shift;
58 3 ▶ my $path = shift || $self->path;
Condition33%1/3
shift() || $self->path
l!l&&r!l&&!r
030
MC/DC0%0/2
shift() || $self->path
shift() $self->path
Truth table33%1/3
A || B
A= shift()B= $self->path
ABresult
1X1
011
000
59
60 3 $self->log("Start Searching For $path path");
61 3 my $method = $self->method;
62 3 my $dispatch = $self->dispatcher;
63 3 my $match;
64 my @captures;
65
66 3 $self->log("1- Trying Direct Path Match");
67
68 3 ▶ if ($dispatch->{$path} && ($match = $dispatch->{$path}->{$method}) && !$dispatch->{$path}->{$method}->{chain}){
Branch: 1 branch
BranchTrueFalseTotal
if ($dispatch->{$path} and $match = $dispatch->{$path}{$method} and not $dispatch->{$path}{$method}{"chain"}) 0 3 3
Condition33%2/6
$dispatch->{$path} and $match = $dispatch->{$path}{$method} and not $dispatch->{$path}{$method}{"chain"}
!ll&&!rl&&r
300
$dispatch->{$path} and $match = $dispatch->{$path}{$method}
!ll&&!rl&&r
300
MC/DC0%0/3
$dispatch->{$path} and $match = $dispatch->{$path}{$method} and not $dispatch->{$path}{$method}{"chain"}
$dispatch->{$path} $match = $dispatch->{$path}{$method} not $dispatch->{$path}{$method}{"chain"}
Truth table25%1/4
A and B and not(C)
A= $dispatch->{$path}B= $match = $dispatch->{$path}{$method}C= not $dispatch->{$path}{$method}{"chain"}
ABCresult
0XX0
10X0
1100
1111
69 0 $self->addAction($match,"Direct Match");
70 0 return;
71 }
72
73 3 $self->log("2- Trying PAths With Arguments");
74 ## nothing found in direct paths, lets try with args
75 3 (my $tpath = $path) =~ s/^\///;
76 3 my @args = split(/\//,$tpath);
77 3 my $i = $#args+1;
78
79 ####get sequence of path - arg ... /first/second(1) /first(2) /(3)
80 3 my $t;
81 0 my @t = reverse (map { --$i; $t .= '/'.$_; $t.'('.$i.')' } @args);
82 3 push(@t,'/'.'('.($#args+1).')');
83 3 foreach my $this (@t){
84 3 ▶ if ($dispatch->{$this} && ($match = $dispatch->{$this}->{$method}) && !$dispatch->{$this}->{$method}->{chain}){
Branch: 1 branch
BranchTrueFalseTotal
if ($dispatch->{$this} and $match = $dispatch->{$this}{$method} and not $dispatch->{$this}{$method}{"chain"}) 0 3 3
Condition33%2/6
$dispatch->{$this} and $match = $dispatch->{$this}{$method} and not $dispatch->{$this}{$method}{"chain"}
!ll&&!rl&&r
300
$dispatch->{$this} and $match = $dispatch->{$this}{$method}
!ll&&!rl&&r
300
MC/DC0%0/3
$dispatch->{$this} and $match = $dispatch->{$this}{$method} and not $dispatch->{$this}{$method}{"chain"}
$dispatch->{$this} $match = $dispatch->{$this}{$method} not $dispatch->{$this}{$method}{"chain"}
Truth table25%1/4
A and B and not(C)
A= $dispatch->{$this}B= $match = $dispatch->{$this}{$method}C= not $dispatch->{$this}{$method}{"chain"}
ABCresult
0XX0
10X0
1100
1111
85 ##capture
86 0 @captures = splice(@args,-$i,$i);
87 0 $self->addAction($match,"Path with arguments Match", @captures);
88
89 0 return;
90 }
91 3 $i++;
92 }
93
94 3 $self->log("3- Trying Chained Actions");
95 ##lets try chains
96 ####start with chain indexes and search for best match
97 3 ▶ if ( my $indexes = $dispatch->{chains_index} ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $indexes = $dispatch->{"chains_index"}) 0 3 3
98
99 0 local $self->{chain_action};
100 0 local $self->{chain_sequence};
101
102 0 for my $index (@{$indexes}){
103 #die Dumper $chain->{$index_path};
104 0 my $return = $self->_loop_chains($path,$index,1);
105 0 ▶ $return && $return == 1 ? next : last;
Branch: 1 branch
BranchTrueFalseTotal
$return && $return == 1 ? : 0 0 0
Condition0%0/3
$return and $return == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$return and $return == 1
$return $return == 1
Truth table0%0/3
A and B
A= $returnB= $return == 1
ABresult
0X0
100
111
106 }
107
108 0 ▶ if ($self->{chain_action}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"chain_action"}) 0 0 0
109 0 my @chain = @{$self->{chain_action}};
110 0 my $lastaction = pop @chain;
111
112 0 for my $action (@chain){
113 0 $self->addAction($action,"Chained Match");
114 0 $self->dispatch();
115 }
116
117 0 $self->addAction($lastaction,"Last Chained Match");
118 0 return;
119
120 }
121 }
122
123 3 $self->log("4- Trying Regex Match");
124 3 ▶ if ($dispatch->{regex}){
Branch: 1 branch
BranchTrueFalseTotal
if ($dispatch->{"regex"}) 1 2 3
125 1 my $oldstrength = '';
126 1 my $match;
127
128 1 foreach my $this (@{$dispatch->{regex}}){
129 1 ▶ if ($path =~ m/$this->{regex}/){
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /$this->{"regex"}/) 0 1 1
130 0 ▶ next if (!grep(/$method/,@{$this->{methods}}));
Branch: 1 branch
BranchTrueFalseTotal
unless grep /$method/, @{$$this{"methods"};} 0 0 0
131 ###select the strongest
132 0 my $localpath = $path; $localpath =~ s/$this->{regex}//;
133 0 my $strength = length $localpath;
134
135 0 ▶ if ($oldstrength eq '' || $strength < $oldstrength){
Branch: 1 branch
BranchTrueFalseTotal
if ($oldstrength eq "" or $strength < $oldstrength) 0 0 0
Condition0%0/3
$oldstrength eq "" or $strength < $oldstrength
l!l&&r!l&&!r
000
MC/DC0%0/2
$oldstrength eq "" or $strength < $oldstrength
$oldstrength eq "" $strength < $oldstrength
Truth table0%0/3
A or B
A= $oldstrength eq ""B= $strength < $oldstrength
ABresult
1X1
011
000
136 0 $match = $this;
137 }
138
139 0 $oldstrength = $strength;
140 #return;
141 }
142 }
143
144 1 ▶ if ($match){
Branch: 1 branch
BranchTrueFalseTotal
if ($match) 0 1 1
145 ##capture it
146 0 @captures = ($path =~ m/$match->{regex}$/);
147 0 @{$self->{capture}} = map { split('/',$_) } @captures;
148 0 $self->addAction($match->{action},"Regex Match",@{$self->{capture}});
149 0 return;
150 }
151 }
152
153 3 $self->log("There is no matching route for $path");
154
155 ##does the app defined a sub to process not found locations?
156 3 ▶ if ($self->app->can('notfound')){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->app->can('notfound')) { } 0 3 3
157 0 $self->app->notfound($self);
158 } else {
159 3 $self->status_code('404');
160 3 $self->body('Not Found');
161 }
162 }
163
164 0 ▶sub _loop_chains {
Subroutine _loop_chains: not called CC=17 SCAR=57.2
165 0 my $self = shift;
166 0 my $path = shift;
167 0 my $dir = shift;
168 0 my $add_namespace = shift;
169 0 my $regex;
170 0 my $dispatch = $self->dispatcher;
171 0 my $chain = $dispatch->{chains};
172 0 my $thisChain = $chain->{$dir};
173
174 my $route = $add_namespace ?
175 $dir :
176 0 ▶ $thisChain->{dir};
Branch: 1 branch
BranchTrueFalseTotal
$add_namespace ? : 0 0 0
177
178 0 my $args = $thisChain->{args};
179
180 0 ▶ if ($args){
Branch: 1 branch
BranchTrueFalseTotal
if ($args) { } 0 0 0
181 0 $regex = qr{^$route/(.*?)$};
182 } else {
183 0 $regex = qr{^$route(.*?)$};
184 }
185
186 0 ▶ if (my ($newpath) = $path =~ m#$regex#){
Branch: 1 branch
BranchTrueFalseTotal
if (my($newpath) = $path =~ /$regex/) 0 0 0
187 0 ▶ my $match = $dispatch->{$thisChain->{path}}->{$self->method}
Branch: 1 branch
BranchTrueFalseTotal
my $match = $$dispatch{$thisChain->{"path"}}{$self->method} or return 1 0 0 0
188 or return 1;
189
190 0 my @captures;
191 0 ▶ if ($args){
Branch: 1 branch
BranchTrueFalseTotal
if ($args) 0 0 0
192 0 @captures = split '/',$newpath;
193 0 my @args = splice @captures,0,$args;
194 ##re construct path
195 0 $newpath = '/'.join '/',@captures;
196 0 $match->{captures} = \@args;
197 }
198
199 0 my $next_chain = $thisChain->{chained_by};
200 0 ▶ return 1 if !$next_chain && $newpath && (!$args || $args && @captures);
Branch: 1 branch
BranchTrueFalseTotal
if not $next_chain and $newpath and !$args || $args && @captures 0 0 0
Condition0%0/12
not $next_chain and $newpath and !$args || $args && @captures
!ll&&!rl&&r
000
not $next_chain and $newpath
!ll&&!rl&&r
000
not $args or $args and @captures
l!l&&r!l&&!r
000
$args and @captures
!ll&&!rl&&r
000
MC/DC0%0/5
not $next_chain and $newpath and !$args || $args && @captures
not $next_chain $newpath not $args $args @captures
Truth table0%0/6
not(A) and B and not(C) or D and E
A= not $next_chainB= $newpathC= not $argsD= $argsE= @captures
ABCDEresult
0XXXX0
10XXX0
1100X0
110100
111XX1
110111
201 0 push @{$self->{action_sequence}},$match;
202
203 ###last action in the chain
204 $self->{chain_action} = $self->{action_sequence} and return
205 0 ▶ if !$next_chain;
Branch: 1 branch
BranchTrueFalseTotal
unless $next_chain 0 0 0
Condition0%0/5
$next_chain || ($self->{'chain_action'} = $self->{'action_sequence'}) && (return)
l!l&&r!l&&!r
000
$self->{"chain_action"} = $self->{"action_sequence"} and return
!ll
00
MC/DC0%0/2
$next_chain || ($self->{'chain_action'} = $self->{'action_sequence'}) && (return)
$next_chain $self->{"chain_action"} = $self->{"action_sequence"}
Truth table0%0/3
A || B
A= $next_chainB= $self->{"chain_action"} = $self->{"action_sequence"}
ABresult
1X1
011
000
206
207 0 for my $next (@{$next_chain}){
208 0 my $nextnamespace = $chain->{$next}->{namespace};
209 0 ▶ my $return = $self->_loop_chains($newpath,$next,$nextnamespace ne $thisChain->{namespace} ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$nextnamespace ne $thisChain->{'namespace'} ? : 0 0 0
210
211 0 ▶ if ($return && $return == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($return and $return == 1) 0 0 0
Condition0%0/3
$return and $return == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$return and $return == 1
$return $return == 1
Truth table0%0/3
A and B
A= $returnB= $return == 1
ABresult
0X0
100
111
212 0 next;
213 }
214 0 return;
215 }
216
217 0 @{$self->{action_sequence}} = splice @{$self->{action_sequence}},0,-1;
218 }
219
220 0 return 1;
221 }
222
223
224
225 0 ▶sub addAction {
Subroutine addAction: not called CC=8 SCAR=42.8
Pod undocumented
226 0 my $self = shift;
227 0 my $match = shift;
228 0 my $type = shift;
229
230 0 $self->log("PINGO: $type Found in " . $match->{class} . " at line " . $match->{line} );
231
232 0 my @captures;
233
234 0 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 0 0 0
elsif ($match->{'captures'}) { } 0 0 0
235 0 @captures = @_;
236 } elsif ($match->{captures}){
237 0 @captures = @{$match->{captures}};
238 }
239
240 $self->action({
241 ActionClass => $match->{ActionClass},
242 controller => bless($self->config->{$match->{class}} || {},$match->{class}),
243 code => _get_code($match->{class},$match->{code}),
244 line => $match->{line},
245 namespace => $match->{namespace},
246 0 ▶ args => \@captures
Condition0%0/2
$self->config->{$match->{'class'}} || {}
l!l
00
MC/DC0%0/1
$self->config->{$match->{'class'}} || {}
$self->config->{$match->{'class'}}
Truth table0%0/2
A
A= $self->config->{$match->{'class'}}
Aresult
11
00
247 });
248
249 ##log captures
250 0 ▶ if ($self->debug && @captures){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug and @captures) 0 0 0
Condition0%0/3
$self->debug and @captures
!ll&&!rl&&r
000
MC/DC0%0/2
$self->debug and @captures
$self->debug @captures
Truth table0%0/3
A and B
A= $self->debugB= @captures
ABresult
0X0
100
111
251 0 ▶ $self->log(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
252 0 my $msg = '';
253 0 $msg .= "=================\n";
254 0 $msg .= " Captured Args \n";
255 0 $msg .= "=================\n";
256 0 foreach my $arg (@captures){
257 0 $msg .= $arg."\n";
258 }
259 0 return $msg;
260 0 });
261 }
262
263 0 ▶ if (@captures && ref $match->{args} eq 'ARRAY'){
Branch: 1 branch
BranchTrueFalseTotal
if (@captures and ref $match->{"args"} eq "ARRAY") 0 0 0
Condition0%0/3
@captures and ref $match->{"args"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
@captures and ref $match->{"args"} eq "ARRAY"
@captures ref $match->{"args"} eq "ARRAY"
Truth table0%0/3
A and B
A= @capturesB= ref $match->{"args"} eq "ARRAY"
ABresult
0X0
100
111
264 0 my $count = -1;
265 0 my %capture = map { ++$count; $_ => $captures[$count] } @{$match->{args}};
266 0 $self->action->{args} = \%capture;
267 }
268
269 0 return;
270 }
271
272 #============================================================================
273 # excute : execute current action
274 #============================================================================
275 3 ▶sub execute {
Subroutine execute: called CC=3 SCAR=16.7
Pod undocumented
276 3 shift;
277 3 my ($controller,$c,$method,$args) = @_;
278 3 ▶ if (ref $method eq 'CODE'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $method eq "CODE") 0 3 3
279 0 ▶ my @args = ref $args eq 'HASH' ? values %{$args} : @{$args};
Branch: 1 branch
BranchTrueFalseTotal
ref $args eq 'HASH' ? : 0 0 0
280 0 return $method->($controller,$c,@args);
281 }
282 }
283
284
285 #============================================================================
286 # private method
287 #============================================================================
288 0 ▶sub _get_code {
Subroutine _get_code: not called CC=2 SCAR=17.9
289 0 my $controller = shift;
290 0 my $method = shift;
291 ###convert method to CODE ref
292 0 ▶ if (ref $method ne 'CODE'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $method ne "CODE") 0 0 0
293 0 $method = $controller.'::'.$method;
294 0 $method = \&$method;
295 }
296 0 return $method;
297 }
298
299 8 1;
300
301 __END__
↑ Summary Cake/DBI/Abstract.pm »