blib/lib/Kelp/Routes.pm

filter: Statementstmt 96.9% 159 / 164 Branchbran 86.3% 57 / 66 Conditioncond 78.7% 37 / 47 MC/DCmcdc 64.2% 18 / 28 Subroutinesub 100% 28 / 28 Podpod 100% 9 / 9 totaltotal 90.0% 308 / 342 CC 53 SCAR 23.8
CC
53
Cov
91%
_parse_route
25.5
load_destination
10.3
format_to
6.0
CRAP 10.8
↑ Summary blib/lib/Kelp/Util.pm »
1 package Kelp::Routes;
2  
3 43 ▶use Kelp::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 43 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 43 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 43 ▶use Kelp::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 43 ▶use Kelp::Routes::Location;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 43 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 43 our @CARP_NOT = qw(Kelp::Module::Routes);
12  
13 43 attr base => ''; # the default is set by config module
14 43 attr rebless => 0; # do not rebless app by default
15 43 attr pattern_obj => 'Kelp::Routes::Pattern';
16 43 attr fatal => 0;
17 41 ▶attr routes => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
18 5 ▶attr names => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
19  
20 # Cache
21 36 ▶attr cache => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
22 36 my $self = shift;
23 36 my %cache;
24  
25 Plack::Util::inline_object(
26 315 ▶ get => sub { $cache{$_[0]} },
Subroutine __ANON__: called CC=1 SCAR=0.0
27 242 ▶ set => sub { $cache{$_[0]} = $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 27 ▶ clear => sub { %cache = () }
Subroutine __ANON__: called CC=1 SCAR=0.0
29 36 );
30 43 };
31  
32 sub add
33 258 ▶{
Subroutine add: called CC=3 SCAR=11.0
Pod documented
34 258 my ($self, $pattern, $descr, $parent) = @_;
35 258 ▶ $parent = {} if !$parent || ref $parent ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if not $parent or ref $parent ne "HASH" 252 6 258
Condition66%2/3
not $parent or ref $parent ne "HASH"
l!l&&r!l&&!r
25206
MC/DC50%1/2
not $parent or ref $parent ne "HASH"
not $parent ref $parent ne "HASH"
Truth table66%2/3
not(A) or B
A= not $parentB= ref $parent ne "HASH"
ABresult
1X1
011
000
36  
37 258 my $route = $self->_parse_route($parent, $pattern, $descr);
38  
39 249 Kelp::Util::_DEBUG(routes => 'Added route: ', $route);
40  
41 249 return $self->_build_location($route);
42 }
43  
44 sub clear
45 27 ▶{
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
46 27 my ($self) = @_;
47  
48 27 $self->routes([]);
49 27 $self->cache->clear;
50 27 $self->names({});
51 }
52  
53 sub url
54 15 ▶{
Subroutine url: called CC=4 SCAR=14.3
Pod documented
55 15 my $self = shift;
56 15 ▶ my $name = shift // croak "Route name is missing";
Condition50%1/2
shift() // croak("Route name is missing")
l!l
150
MC/DC0%0/1
shift() // croak("Route name is missing")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
57 0 ▶ my %args = @_ == 1 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 15 15
58  
59 15 ▶ return $name unless exists $self->names->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->names->{$name} 5 10 15
60 10 my $route = $self->routes->[$self->names->{$name}];
61 10 return $route->build(%args);
62 }
63  
64 sub _build_location
65 249 ▶{
Subroutine _build_location: called CC=1 SCAR=0.0
66 # build a specific location object on which ->add can be called again
67 249 my ($self, $route) = @_;
68  
69 249 return Kelp::Routes::Location->new(
70 router => $self,
71 parent => $route,
72 );
73 }
74  
75 sub _message
76 23 ▶{
Subroutine _message: called CC=3 SCAR=11.0
77 23 my ($self, $type_str, @parts) = @_;
78 23 my $message = "[ROUTES] $type_str: ";
79  
80 23 for my $part (@parts) {
81 41 ▶ $part //= '';
Condition50%1/2
$part //= ""
l!l
410
MC/DC0%0/1
$part //= ""
$part
Truth table50%1/2
A
A= $part
Aresult
11
00
82 41 $part =~ s/ at .+? line \d+.\n//g; # way prettier errors
83 }
84  
85 23 return $message . join ' - ', @parts;
86 }
87  
88 sub _error
89 22 ▶{
Subroutine _error: called CC=2 SCAR=6.9
90 22 my ($self, @parts) = @_;
91  
92 22 ▶ croak $self->_message('ERROR', @parts) if $self->fatal;
Branch: 1 branch
BranchTrueFalseTotal
if $self->fatal 9 13 22
93 13 carp $self->_message('WARNING, route is skipped', @parts);
94 13 return;
95 }
96  
97 sub _warning
98 1 ▶{
Subroutine _warning: called CC=1 SCAR=0.0
99 1 my ($self, @parts) = @_;
100  
101 1 carp $self->_message('WARNING', @parts);
102 }
103  
104 sub _parse_route
105 271 ▶{
Subroutine _parse_route: called CC=25 SCAR=32.4
106 271 my ($self, $parent, $key, $val) = @_;
107  
108 # Scalar, e.g. 'bar#foo'
109 # CODE, e.g. sub { ... }
110 271 ▶ if (!ref $val || ref $val eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $val or ref $val eq "CODE") 194 77 271
Condition100%3/3
not ref $val or ref $val eq "CODE"
l!l&&r!l&&!r
7212277
MC/DC100%2/2
not ref $val or ref $val eq "CODE"
not ref $val ref $val eq "CODE"
Truth table100%3/3
not(A) or B
A= not ref $valB= ref $val eq "CODE"
ABresult
1X1
011
000
111 194 $val = {to => $val};
112 }
113  
114 # Sanity check
115 271 ▶ if (ref $val ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val ne "HASH") 1 270 271
116 1 return $self->_error('Route description must be a string, CODE or HASH');
117 }
118  
119 # Handle key in form of [METHOD => 'pattern']
120 270 ▶ if (ref $key eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $key eq "ARRAY") 23 247 270
121 23 ▶ if ((grep { defined } @$key) != 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep({defined $_;} @$key) != 2) 0 23 23
122 0 return $self->_error("Path as an ARRAY is expected to have two parameters");
123 }
124  
125 23 my ($method, $pattern) = @$key;
126 23 ▶ if (!grep { $method eq $_ } qw/GET POST PUT DELETE/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$method eq $_;} "GET", "POST", "PUT", "DELETE") 1 22 23
127 1 $self->_warning("Using an odd method '$method'");
128 }
129  
130 23 $val->{method} = $method;
131 23 $key = $pattern;
132 }
133  
134 # Only SCALAR and Regexp allowed
135 270 ▶ if (ref $key && ref $key ne 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $key and ref $key ne "Regexp") 2 268 270
Condition100%3/3
ref $key and ref $key ne "Regexp"
!ll&&!rl&&r
26712
MC/DC100%2/2
ref $key and ref $key ne "Regexp"
ref $key ref $key ne "Regexp"
Truth table100%3/3
A and B
A= ref $keyB= ref $key ne "Regexp"
ABresult
0X0
100
111
136 2 return $self->_error("Pattern '$key' can not be computed");
137 }
138  
139 268 $val->{pattern} = $key;
140  
141 # Format and load the target of 'to'
142 268 my $error;
143 268 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
144 268 $val->{to} = $self->format_to($val->{to});
145 260 $val->{dest} = $self->load_destination($val->{to});
146 }
147 18 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
148 18 $error = $_;
149 268 };
150  
151 268 ▶ if (!defined $val->{dest} || $error) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $val->{"dest"} or $error) 18 250 268
Condition66%2/3
not defined $val->{"dest"} or $error
l!l&&r!l&&!r
180250
MC/DC50%1/2
not defined $val->{"dest"} or $error
not defined $val->{"dest"} $error
Truth table66%2/3
not(A) or B
A= not defined $val->{"dest"}B= $error
ABresult
1X1
011
000
152 18 return $self->_error("Invalid destination for route '$key'", $error);
153 }
154  
155 # store tree for later and set up bridge based on it
156 250 my $tree = delete $val->{tree};
157 250 ▶ if ($tree && (ref $tree ne 'ARRAY' || @$tree % 2 != 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tree and ref $tree ne "ARRAY" || @$tree % 2 != 0) 0 250 250
Condition50%3/6
$tree and ref $tree ne "ARRAY" || @$tree % 2 != 0
!ll&&!rl&&r
24370
ref $tree ne "ARRAY" or @$tree % 2 != 0
l!l&&r!l&&!r
007
MC/DC0%0/3
$tree and ref $tree ne "ARRAY" || @$tree % 2 != 0
$tree ref $tree ne "ARRAY" @$tree % 2 != 0
Truth table50%2/4
A and B or C
A= $treeB= ref $tree ne "ARRAY"C= @$tree % 2 != 0
ABCresult
0XX0
1000
11X1
1011
158 0 return $self->_error("Tree must be an even-sized ARRAY");
159 }
160 250 ▶ $val->{bridge} ||= defined $tree;
Condition100%3/3
$val->{"bridge"} ||= defined $tree
l!l&&r!l&&!r
147229
MC/DC100%2/2
$val->{"bridge"} ||= defined $tree
$val->{"bridge"} defined $tree
Truth table100%3/3
A ||= B
A= $val->{"bridge"}B= defined $tree
ABresult
1X1
011
000
161  
162 # psgi + bridge is incompatible, as psgi route will only render (not return true values)
163 250 ▶ if ($val->{psgi} && $val->{bridge}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val->{"psgi"} and $val->{"bridge"}) 1 249 250
Condition100%3/3
$val->{"psgi"} and $val->{"bridge"}
!ll&&!rl&&r
24451
MC/DC100%2/2
$val->{"psgi"} and $val->{"bridge"}
$val->{"psgi"} $val->{"bridge"}
Truth table100%3/3
A and B
A= $val->{"psgi"}B= $val->{"bridge"}
ABresult
0X0
100
111
164 1 return $self->_error("Route '$key' cannot have both 'psgi' and 'bridge'");
165 }
166  
167 # Adjust the destination for psgi
168 $val->{dest} = $self->wrap_psgi($val->{to}, $val->{dest})
169 249 ▶ if $val->{psgi};
Branch: 1 branch
BranchTrueFalseTotal
if $val->{"psgi"} 5 244 249
170  
171 # Credit stuff from tree parent, if possible
172 249 ▶ if (defined $parent->{pattern}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parent->{"pattern"}) 19 230 249
173 19 ▶ if ($val->{name} && $parent->{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val->{"name"} and $parent->{"name"}) 13 6 19
Condition100%3/3
$val->{"name"} and $parent->{"name"}
!ll&&!rl&&r
5113
MC/DC100%2/2
$val->{"name"} and $parent->{"name"}
$val->{"name"} $parent->{"name"}
Truth table100%3/3
A and B
A= $val->{"name"}B= $parent->{"name"}
ABresult
0X0
100
111
174 13 $val->{name} = $parent->{name} . '_' . $val->{name};
175 }
176 19 $val->{pattern} = $parent->{pattern} . $val->{pattern};
177 }
178  
179 # Can now add the object to routes
180 249 my $route = $self->build_pattern($val);
181 249 push @{$self->routes}, $route;
182  
183 # Add route index to names
184 249 ▶ if (my $name = $val->{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = $val->{"name"}) 26 223 249
185 26 ▶ if (exists $self->names->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->names->{$name}) 0 26 26
186 0 $self->_warning("Multiple routes named '$name'");
187 }
188 26 $self->names->{$name} = $#{$self->routes};
189 }
190  
191 # handle further tree levels, if any
192 249 ▶ $tree //= [];
Condition100%2/2
$tree //= []
l!l
7242
MC/DC100%1/1
$tree //= []
$tree
Truth table100%2/2
A
A= $tree
Aresult
11
00
193 249 ▶ while (@$tree) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$tree) 13 249 262
194 13 my ($k, $v) = splice(@$tree, 0, 2);
195 13 $self->_parse_route($val, $k, $v);
196 }
197  
198 249 return $route;
199 }
200  
201 # Override to change what 'to' values are valid
202 sub format_to
203 268 ▶{
Subroutine format_to: called CC=6 SCAR=17.9
Pod documented
204 268 my ($self, $to) = @_;
205 268 my $ref = ref $to;
206  
207 268 ▶ if (!defined $to) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $to) { } 4 264 268
elsif (not $to or $ref and $ref ne 'CODE') { } 4 379 383
208 4 croak 'missing';
209 }
210 ▶ elsif (!$to || ($ref && $ref ne 'CODE')) {
Condition83%5/6
not $to or $ref and $ref ne "CODE"
l!l&&r!l&&!r
04260
$ref and $ref ne "CODE"
!ll&&!rl&&r
1191414
MC/DC66%2/3
not $to or $ref and $ref ne "CODE"
not $to $ref $ref ne "CODE"
Truth table75%3/4
not(A) or B and C
A= not $toB= $refC= $ref ne "CODE"
ABCresult
1XX1
0111
00X0
0100
211 4 croak 'neither a string nor a coderef';
212 }
213  
214 260 ▶ $to = Kelp::Util::camelize($to, $self->base)
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 119 141 260
215 unless $ref;
216  
217 260 return $to;
218 }
219  
220 # Override to change the way the application loads the destination from 'to'
221 sub load_destination
222 260 ▶{
Subroutine load_destination: called CC=10 SCAR=23.3
Pod documented
223 260 my ($self, $to) = @_;
224 260 my $ref = ref $to;
225  
226 260 ▶ if (!$ref && $to) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $ref and $to) { } 119 141 260
elsif ($ref) { } 141 0 141
Condition66%2/3
not $ref and $to
!ll&&!rl&&r
1410119
MC/DC50%1/2
not $ref and $to
not $ref $to
Truth table66%2/3
not(A) and B
A= not $refB= $to
ABresult
0X0
100
111
227  
228 # Load the class, if there is one
229 119 ▶ if (my $class = Kelp::Util::extract_class($to)) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $class = Kelp::Util::extract_class($to)) { } 103 16 119
elsif (exists &$to) { } 10 6 16
230 103 my $method = Kelp::Util::extract_function($to);
231  
232 103 Kelp::Util::load_package($class);
233  
234 101 my $method_code = $class->can($method);
235 101 ▶ croak "method '$method' does not exist in class '$class'"
Branch: 1 branch
BranchTrueFalseTotal
unless $method_code 2 99 101
236 unless $method_code;
237  
238 99 ▶ return [$self->rebless && $class->isa($self->base) ? $class : undef, $method_code];
Branch: 1 branch
BranchTrueFalseTotal
$self->rebless && $class->isa($self->base) ? : 31 68 99
Condition66%2/3
$self->rebless and $class->isa($self->base)
!ll&&!rl&&r
68031
MC/DC50%1/2
$self->rebless and $class->isa($self->base)
$self->rebless $class->isa($self->base)
Truth table66%2/3
A and B
A= $self->reblessB= $class->isa($self->base)
ABresult
0X0
100
111
239 }
240 elsif (exists &$to) {
241  
242 # Move to reference
243 10 return [undef, \&{$to}];
244 }
245 else {
246 6 croak "function '$to' does not exist";
247 }
248 }
249 elsif ($ref) {
250 141 ▶ croak "don't know how to load from reftype '$ref'"
Branch: 1 branch
BranchTrueFalseTotal
unless $ref eq "CODE" 0 141 141
251 unless $ref eq 'CODE';
252  
253 141 return [undef, $to];
254 }
255  
256 0 return undef;
257 }
258  
259 # Override to change the way a psgi application is adapted to kelp
260 sub wrap_psgi
261 5 ▶{
Subroutine wrap_psgi: called CC=1 SCAR=0.0
Pod documented
262 5 my ($self, $to, $destination) = @_;
263  
264 # adjust the subroutine to load
265 # don't adjust the controller (index 0) to still call the proper hooks if
266 # it was configured to be a controller action
267  
268 5 $destination->[1] = Kelp::Util::adapt_psgi($destination->[1]);
269 5 return $destination;
270 }
271  
272 # Override to use a custom pattern object
273 sub build_pattern
274 249 ▶{
Subroutine build_pattern: called CC=1 SCAR=0.0
Pod documented
275 return Kelp::Util::load_package($_[0]->pattern_obj)->new(
276 249 %{$_[1]}
277 );
278 }
279  
280 sub match
281 311 ▶{
Subroutine match: called CC=4 SCAR=13.9
Pod documented
282 311 my ($self, $path, $method) = @_;
283 311 ▶ $method //= '';
Condition100%2/2
$method //= ""
l!l
26942
MC/DC100%1/1
$method //= ""
$method
Truth table100%2/2
A
A= $method
Aresult
11
00
284  
285 # Look for this path and method in the cache. If found,
286 # return the array of routes that matched the previous time.
287 # If not found, then return all routes.
288 311 my $key = "$path:$method";
289 311 my $routes = $self->cache->get($key);
290 311 ▶ if (!defined $routes) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $routes) { } 242 69 311
291  
292 # Look through all routes, grep the ones that match and sort them with
293 # the compare method. Perl sort function is stable, meaning it will
294 # preserve the initial order of records it considers equal. This means
295 # that the order of registering routes is crucial when a couple of
296 # routes are registered with the same pattern: routes defined earlier
297 # will be run first and the first one to render will end the execution
298 # chain.
299 @$routes =
300 42 sort { $a->compare($b) }
301 242 grep { $_->match($path, $method) } @{$self->routes};
302  
303 242 $self->cache->set($key, $routes);
304 }
305 else {
306 # matching fills the route parameters
307 69 $_->match($path, $method) for @$routes;
308 }
309  
310 # shallow copy to make sure nothing pollutes the cache
311 311 return [@$routes];
312 }
313  
314 # dispatch does not do many sanity checks on the destination, since those are
315 # done in format_to and load_destination. A single check is present, which
316 # lazy-computes dest if it is not set (since some code might have overrode add).
317 sub dispatch
318 275 ▶{
Subroutine dispatch: called CC=4 SCAR=14.1
Pod documented
319 275 my ($self, $app, $route) = @_;
320 275 ▶ $app || die "Application instance required";
Branch: 1 branch
BranchTrueFalseTotal
$app or die "Application instance required" 275 0 275
321 275 ▶ $route || die "No route pattern instance supplied";
Branch: 1 branch
BranchTrueFalseTotal
$route or die "No route pattern instance supplied" 275 0 275
322  
323 275 my $dest = $route->dest;
324 275 ▶ $route->dest($self->load_destination($route->to))
Branch: 1 branch
BranchTrueFalseTotal
unless $dest 0 275 275
325 unless $dest;
326  
327 275 my ($controller, $action) = @{$dest};
328 275 my $c = $app->context->set_controller($controller);
329  
330 275 $app->context->run_method(before_dispatch => ($route->to));
331 275 return $action->($c, @{$route->param});
332 }
333  
334 43 1;
335  
336 __END__
↑ Summary blib/lib/Kelp/Util.pm »