blib/lib/Catalyst/Seal/Dispatch.pm

filter: Statementstmt 90.0% 171 / 190 Branchbran 58.4% 62 / 106 Conditioncond 41.4% 17 / 41 MC/DCmcdc 15.0% 3 / 20 Subroutinesub 92.0% 23 / 25 Podpod 0.0% 0 / 2 totaltotal 71.8% 276 / 384 CC 72 SCAR 26.6
CC
72
Cov
74%
_component
40.4
_invoke_as_path
13.3
_controller_dispatch
12.4
CRAP 14.3
« blib/lib/Catalyst/Seal/Prepare.pm ↑ Summary blib/lib/Catalyst/Seal/Construct.pm »
1 package Catalyst::Seal::Dispatch;
2  
3 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 19 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 19 ▶use Catalyst::Seal ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 19 ▶use Catalyst::Seal::Guard ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 19 our $VERSION = '0.04';
13  
14 19 my %PATH;
15 my %ACTIONS;
16  
17 19 our $MAX_KEYS = 2048;
18 19 my $CAPPED = 0;
19  
20 1 ▶sub capped { $CAPPED }
Subroutine capped: called CC=1 SCAR=0.0
Pod undocumented
21  
22 4 ▶sub _clear {
Subroutine _clear: called CC=1 SCAR=0.0
23 4 %PATH = ();
24 4 %ACTIONS = ();
25 4 $CAPPED = 0;
26 4 return;
27 }
28  
29 6 ▶sub memo_sizes { return (scalar keys %PATH, scalar keys %ACTIONS) }
Subroutine memo_sizes: called CC=1 SCAR=0.0
Pod undocumented
30  
31 210 ▶sub _invoke_as_path {
Subroutine _invoke_as_path: called CC=12 SCAR=25.9
32 210 my ( $self, $c, $rel_path, $args ) = @_;
33  
34 210 my $base = '';
35 210 ▶ if ($rel_path !~ m{\A/}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rel_path =~ m[\A/]) 0 210 210
36 0 my $top = $c->stack->[-1];
37 0 ▶ $base = $top ? $top->namespace : '';
Branch: 1 branch
BranchTrueFalseTotal
$top ? : 0 0 0
38 0 ▶ $base = '' unless defined $base;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $base 0 0 0
39 }
40  
41 210 my $key = "$base\0$rel_path";
42 210 my $hit = $PATH{$key};
43  
44 210 ▶ unless ($hit) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hit) 203 7 210
45 203 my (@extra_args, $found);
46 203 my $path = $self->_action_rel2abs( $c, $rel_path );
47  
48 203 my $tail;
49 203 ▶ while ( ( $path, $tail ) = ( $path =~ m#^(?:(.*)/)?(\w+)?$# ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($path, $tail) = $path =~ m[^(?:(.*)/)?(\w+)?$]) 603 0 603
50 603 ▶ if ( my $action = $c->get_action( $tail, $path ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $action = $c->get_action($tail, $path)) { } 3 600 603
51 3 $found = $action;
52 3 last;
53 }
54 else {
55 600 ▶ last unless $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 200 400 600
56 }
57 400 unshift @extra_args, $tail;
58 }
59  
60 203 $hit = [ $found, \@extra_args ];
61 203 ▶ if (keys %PATH < $MAX_KEYS) {
Branch: 2 branches
BranchTrueFalseTotal
if (keys %PATH < $Catalyst::Seal::Dispatch::MAX_KEYS) { } 11 192 203
elsif (not $CAPPED++) { } 1 191 192
62 11 $PATH{$key} = $hit;
63 }
64 elsif (!$CAPPED++) {
65 1 Catalyst::Seal::note(
66 "dispatch path memo reached $MAX_KEYS keys, no longer caching");
67 }
68 }
69  
70 210 my ($action, $extra) = @$hit;
71 210 ▶ return unless $action;
Branch: 1 branch
BranchTrueFalseTotal
unless $action 200 10 210
72 10 ▶ push @$args, @$extra if @$extra;
Branch: 1 branch
BranchTrueFalseTotal
if @$extra 0 10 10
73 10 return $action;
74 }
75  
76 1149 ▶sub _get_actions {
Subroutine _get_actions: called CC=6 SCAR=18.1
77 1149 my ( $self, $c, $action, $namespace ) = @_;
78 1149 ▶ return [] unless $action;
Branch: 1 branch
BranchTrueFalseTotal
unless $action 0 1149 1149
79  
80 1149 ▶ my $key = "$action\0" . (defined $namespace ? $namespace : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $namespace ? : 1149 0 1149
81 1149 my $hit = $ACTIONS{$key};
82  
83 1149 ▶ unless ($hit) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hit) 57 1092 1149
84 57 ▶ my $ns = join( "/", grep { length } split '/', $namespace || "" );
Condition100%2/2
$namespace || ''
l!l
2136
MC/DC100%1/1
$namespace || ''
$namespace
Truth table100%2/2
A
A= $namespace
Aresult
11
00
85 57 my @match = $self->get_containers($ns);
86 57 $hit = [ map { $_->get_action($action) } @match ];
87 57 ▶ $ACTIONS{$key} = $hit if keys %ACTIONS < $MAX_KEYS;
Branch: 1 branch
BranchTrueFalseTotal
if keys %ACTIONS < $Catalyst::Seal::Dispatch::MAX_KEYS 57 0 57
88 }
89  
90 1149 return @$hit;
91 }
92  
93 19 my %PLAN;
94 my %DISPATCH;
95  
96 1 ▶sub _clear_chain { %PLAN = (); %DISPATCH = (); return }
Subroutine _clear_chain: called CC=1 SCAR=0.0
97  
98 1834 ▶sub _run {
Subroutine _run: called CC=2 SCAR=6.9
99 1834 my ($c, $action) = @_;
100  
101 1834 my @args = @{ $c->request->arguments };
102 1834 local $c->request->{arguments} = \@args;
103 19 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
104 1834 $action->dispatch($c);
105  
106 1834 ▶ $c->state(0) if @{ $c->error };
Branch: 1 branch
BranchTrueFalseTotal
if @{$c->error;} 602 1232 1834
107 1834 return $c->state;
108 }
109  
110 367 ▶sub _dispatch {
Subroutine _dispatch: called CC=8 SCAR=22.9
111 367 my ($self, $c) = @_;
112  
113 367 my $action = $c->action;
114 367 my $stock = $Catalyst::Seal::Guard::ORIGINAL{'Catalyst::Dispatcher::dispatch'};
115  
116 367 ▶ return $stock ? $stock->($self, $c) : () unless $action;
Branch: 2 branches
BranchTrueFalseTotal
unless $action 0 367 367
$stock ? : 0 0 0
117  
118 367 my $ns = $action->namespace;
119 367 ▶ $ns = '' unless defined $ns;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ns 0 367 367
120  
121 367 my $disp = $DISPATCH{$ns};
122 367 ▶ unless (defined $disp) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $disp) 16 351 367
123 16 ▶ $disp = $DISPATCH{$ns} = $c->get_action('_DISPATCH', $ns) || 0;
Condition50%1/2
$c->get_action('_DISPATCH', $ns) || 0
l!l
160
MC/DC0%0/1
$c->get_action('_DISPATCH', $ns) || 0
$c->get_action('_DISPATCH', $ns)
Truth table50%1/2
A
A= $c->get_action('_DISPATCH', $ns)
Aresult
11
00
124 }
125 367 ▶ return $stock ? $stock->($self, $c) : () unless $disp;
Branch: 2 branches
BranchTrueFalseTotal
unless $disp 0 367 367
$stock ? : 0 0 0
126  
127 367 _run($c, $disp);
128 367 return;
129 }
130  
131 0 ▶sub _state_one { $_[0]->state(1); return 1 }
Subroutine _state_one: not called CC=1 SCAR=6.9
132 0 ▶sub _state_or_one { my $c = shift; $c->state($c->state || 1); return 1 }
Subroutine _state_or_one: not called CC=2 SCAR=17.9
Condition0%0/2
$c->state || 1
l!l
00
MC/DC0%0/1
$c->state || 1
$c->state
Truth table0%0/2
A
A= $c->state
Aresult
11
00
133  
134 16 ▶sub _build_plan {
Subroutine _build_plan: called CC=8 SCAR=22.8
135 16 my ($c, $self, $ns) = @_;
136  
137 16 my $begin = ( $c->get_actions('begin', $ns) )[-1];
138 16 my @autos = $c->get_actions('auto', $ns);
139 16 my $end = ( $c->get_actions('end', $ns) )[-1];
140  
141 16 ▶ my %noop = (
Branch: 2 branches
BranchTrueFalseTotal
$begin ? : 16 0 16
@autos ? : 16 0 16
142 _BEGIN => ($begin ? undef : \&_state_one),
143 _AUTO => (@autos ? undef : \&_state_or_one),
144 );
145  
146 16 my @steps;
147 16 for my $name (@{ $self->_dispatch_steps }) {
148 48 ▶ if ($noop{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($noop{$name}) 0 48 48
149 0 push @steps, $noop{$name};
150 0 next;
151 }
152 48 ▶ my $a = $c->get_action($name, $ns) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $a = $c->get_action($name, $ns) or return 0 48 0 48
153 48 push @steps, $a;
154 }
155  
156 16 my $end_step;
157 16 ▶ if ($end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end) { } 16 0 16
158 16 ▶ $end_step = $c->get_action('_END', $ns) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$end_step = $c->get_action("_END", $ns) or return 0 16 0 16
159 }
160 else {
161 0 $end_step = \&_state_one;
162 }
163  
164 16 return { steps => \@steps, end => $end_step };
165 }
166  
167 367 ▶sub _controller_dispatch {
Subroutine _controller_dispatch: called CC=10 SCAR=25.2
168 367 my ($self, $c) = @_;
169  
170 367 my $top = $c->stack->[-1];
171 367 ▶ my $ns = $top ? $top->namespace : '';
Branch: 1 branch
BranchTrueFalseTotal
$top ? : 367 0 367
172 367 ▶ $ns = '' unless defined $ns;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ns 0 367 367
173  
174 367 my $plan = $PLAN{$ns};
175 367 ▶ $plan = $PLAN{$ns} = _build_plan($c, $self, $ns) unless defined $plan;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $plan 16 351 367
176  
177 367 ▶ unless ($plan) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($plan) 0 367 367
178 0 my $stock = $Catalyst::Seal::Guard::ORIGINAL{'Catalyst::Controller::_DISPATCH'};
179 0 ▶ return $stock ? $stock->($self, $c) : ();
Branch: 1 branch
BranchTrueFalseTotal
$stock ? : 0 0 0
180 }
181  
182 367 for my $step (@{ $plan->{steps} }) {
183 1100 ▶ last unless ref $step eq 'CODE' ? $step->($c) : _run($c, $step);
Branch: 2 branches
BranchTrueFalseTotal
unless ref $step eq "CODE" ? &$step($c) : _run($c, $step) 204 896 1100
ref $step eq 'CODE' ? : 0 1100 1100
184 }
185 367 my $end = $plan->{end};
186 367 ▶ ref $end eq 'CODE' ? $end->($c) : _run($c, $end);
Branch: 1 branch
BranchTrueFalseTotal
ref $end eq 'CODE' ? : 0 367 367
187 367 return;
188 }
189  
190 19 my %COMPONENT;
191  
192 1 ▶sub _clear_components { %COMPONENT = (); return }
Subroutine _clear_components: called CC=1 SCAR=0.0
193  
194 3728 ▶sub _component {
Subroutine _component: called CC=18 SCAR=37.0
195 3728 my ($c, $name, @args) = @_;
196  
197 3728 ▶ if (defined $name && !ref $name && !@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name and not ref $name and not @args) 3728 0 3728
Condition33%2/6
defined $name and not ref $name and not @args
!ll&&!rl&&r
003728
defined $name and not ref $name
!ll&&!rl&&r
003728
MC/DC0%0/3
defined $name and not ref $name and not @args
defined $name not ref $name not @args
Truth table25%1/4
A and not(B) and not(C)
A= defined $nameB= not ref $nameC= not @args
ABCresult
0XX0
10X0
1100
1111
198 3728 my $hit = $COMPONENT{$name};
199 3728 ▶ return $hit->[0] if $hit;
Branch: 1 branch
BranchTrueFalseTotal
if $hit 3511 217 3728
200 }
201  
202 217 my $stock = $Catalyst::Seal::Guard::ORIGINAL{'Catalyst::component'};
203 217 ▶ my @out = $stock ? $stock->($c, $name, @args) : ();
Branch: 1 branch
BranchTrueFalseTotal
$stock ? : 217 0 217
204  
205 217 ▶ if (defined $name && !ref $name && !@args && @out == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name and not ref $name and not @args and @out == 1) 17 200 217
Condition44%4/9
defined $name and not ref $name and not @args and @out == 1
!ll&&!rl&&r
020017
defined $name and not ref $name and not @args
!ll&&!rl&&r
00217
defined $name and not ref $name
!ll&&!rl&&r
00217
MC/DC25%1/4
defined $name and not ref $name and not @args and @out == 1
defined $name not ref $name not @args @out == 1
Truth table40%2/5
A and not(B) and not(C) and D
A= defined $nameB= not ref $nameC= not @argsD= @out == 1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
206 17 ▶ my $comps = eval { $c->components } || {};
Condition50%1/2
eval { do { $c->components } } || {}
l!l
170
MC/DC0%0/1
eval { do { $c->components } } || {}
eval { do { $c->components } }
Truth table50%1/2
A
A= eval { do { $c->components } }
Aresult
11
00
207 17 my $raw = $comps->{$name};
208 17 ▶ if (defined $raw && Scalar::Util::blessed($raw)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $raw and builtin::blessed($raw) and not $raw->can("ACCEPT_CONTEXT") and builtin::blessed($out[0]) and builtin::refaddr($out[0]) == builtin::refaddr($raw)) 17 0 17
Condition33%4/12
defined $raw and builtin::blessed($raw) and not $raw->can("ACCEPT_CONTEXT") and builtin::blessed($out[0]) and builtin::refaddr($out[0]) == builtin::refaddr($raw)
!ll&&!rl&&r
0017
defined $raw and builtin::blessed($raw) and not $raw->can("ACCEPT_CONTEXT") and builtin::blessed($out[0])
!ll&&!rl&&r
0017
defined $raw and builtin::blessed($raw) and not $raw->can("ACCEPT_CONTEXT")
!ll&&!rl&&r
0017
defined $raw and builtin::blessed($raw)
!ll&&!rl&&r
0017
MC/DC0%0/5
defined $raw and builtin::blessed($raw) and not $raw->can("ACCEPT_CONTEXT") and builtin::blessed($out[0]) and builtin::refaddr($out[0]) == builtin::refaddr($raw)
defined $raw builtin::blessed($raw) not $raw->can("ACCEPT_CONTEXT") builtin::blessed($out[0]) builtin::refaddr($out[0]) == builtin::refaddr($raw)
Truth table16%1/6
A and B and not(C) and D and E
A= defined $rawB= builtin::blessed($raw)C= not $raw->can("ACCEPT_CONTEXT")D= builtin::blessed($out[0])E= builtin::refaddr($out[0]) == builtin::refaddr($raw)
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
209 && !$raw->can('ACCEPT_CONTEXT')
210 && Scalar::Util::blessed($out[0])
211 && Scalar::Util::refaddr($out[0]) == Scalar::Util::refaddr($raw)) {
212 17 ▶ $COMPONENT{$name} = [ $out[0] ] if keys %COMPONENT < $MAX_KEYS;
Branch: 1 branch
BranchTrueFalseTotal
if keys %COMPONENT < $Catalyst::Seal::Dispatch::MAX_KEYS 17 0 17
213 }
214 }
215  
216 217 ▶ return wantarray ? @out : $out[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 217 217
217 }
218  
219 12 ▶sub _rewire_dispatch_actions {
Subroutine _rewire_dispatch_actions: called CC=9 SCAR=22.8
220 12 my ($app) = @_;
221  
222 12 ▶ my $stock = $Catalyst::Seal::Guard::ORIGINAL{'Catalyst::Controller::_DISPATCH'}
Branch: 1 branch
BranchTrueFalseTotal
my $stock = $Catalyst::Seal::Guard::ORIGINAL{"Catalyst::Controller::_DISPATCH"} or return 0 12 0 12
223 or return 0;
224  
225 12 ▶ my $hash = eval { $app->dispatcher->_action_hash } or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $hash = eval { do { $app->dispatcher->_action_hash } } or return 0 12 0 12
226 12 my $done = 0;
227  
228 12 for my $action (values %$hash) {
229 576 ▶ next unless $action && $action->name eq '_DISPATCH';
Branch: 1 branch
BranchTrueFalseTotal
unless $action and $action->name eq "_DISPATCH" 540 36 576
Condition66%2/3
$action and $action->name eq "_DISPATCH"
!ll&&!rl&&r
054036
MC/DC50%1/2
$action and $action->name eq "_DISPATCH"
$action $action->name eq "_DISPATCH"
Truth table66%2/3
A and B
A= $actionB= $action->name eq "_DISPATCH"
ABresult
0X0
100
111
230 36 ▶ my $code = $action->{code} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $code = $action->{"code"} or next 36 0 36
231 36 ▶ next unless Scalar::Util::refaddr($code) == Scalar::Util::refaddr($stock);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::refaddr($code) == builtin::refaddr($stock) 0 36 36
232 36 $action->{code} = \&_controller_dispatch;
233 36 $done++;
234 }
235  
236 12 ▶ Catalyst::Seal::note("rewired $done _DISPATCH action(s)")
Branch: 1 branch
BranchTrueFalseTotal
if $Catalyst::Seal::DEBUG 0 12 12
237 if $Catalyst::Seal::DEBUG;
238 12 return $done;
239 }
240  
241 19 my $PATCHED = 0;
242  
243 12 ▶Catalyst::Seal::register_step('dispatch' => sub {
Subroutine __ANON__: called CC=6 SCAR=19.6
244 12 my ($app) = @_;
245  
246 12 ▶ return if $PATCHED++;
Branch: 1 branch
BranchTrueFalseTotal
if $PATCHED++ 0 12 12
247  
248 12 my $path = Catalyst::Seal::Guard::replace(
249 'Catalyst::Dispatcher::_invoke_as_path' => \&_invoke_as_path);
250 12 my $actions = Catalyst::Seal::Guard::replace(
251 'Catalyst::Dispatcher::get_actions' => \&_get_actions);
252  
253 12 ▶ unless ($path || $actions) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path or $actions) 0 12 12
Condition33%1/3
$path or $actions
l!l&&r!l&&!r
1200
MC/DC0%0/2
$path or $actions
$path $actions
Truth table33%1/3
A or B
A= $pathB= $actions
ABresult
1X1
011
000
254 0 Catalyst::Seal::note('neither dispatch lookup was memoised');
255 0 return;
256 }
257  
258 Catalyst::Seal::Guard::replace(
259 12 'Catalyst::Dispatcher::dispatch' => \&_dispatch);
260 12 Catalyst::Seal::Guard::replace(
261 'Catalyst::Controller::_DISPATCH' => \&_controller_dispatch);
262 12 _rewire_dispatch_actions($app);
263 12 Catalyst::Seal::Guard::replace(
264 'Catalyst::component' => \&_component);
265  
266 12 my $register = Catalyst::Dispatcher->can('register');
267 12 ▶ if ($register) {
Branch: 1 branch
BranchTrueFalseTotal
if ($register) { } 12 0 12
268 19 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
269 1 ▶ *Catalyst::Dispatcher::register = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
270 1 _clear();
271 1 _clear_chain();
272 1 _clear_components();
273 1 return $register->(@_);
274 12 };
275 }
276 else {
277 0 Catalyst::Seal::note(
278 'no Catalyst::Dispatcher::register to hook, memos cannot be invalidated');
279 0 Catalyst::Seal::Guard::restore('Catalyst::Dispatcher::_invoke_as_path');
280 0 Catalyst::Seal::Guard::restore('Catalyst::Dispatcher::get_actions');
281 0 return;
282 }
283  
284 12 ▶ Catalyst::Seal::note("memoised dispatch lookups (path=$path actions=$actions)")
Branch: 1 branch
BranchTrueFalseTotal
if $Catalyst::Seal::DEBUG 0 12 12
285 if $Catalyst::Seal::DEBUG;
286 12 return;
287 19 });
288  
289 19 1;
290  
291 __END__
« blib/lib/Catalyst/Seal/Prepare.pm ↑ Summary blib/lib/Catalyst/Seal/Construct.pm »