Mojolicious/Routes/Match.pm

filter: Statementstmt 93.5% 58 / 62 Branchbran 80.5% 29 / 36 Conditioncond 87.5% 35 / 40 MC/DCmcdc 70.8% 17 / 24 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 86.4% 147 / 170 CC 35 SCAR 30.2
CC
35
Cov
88%
_match
27.8
path_for
11.0
BEGIN
1.0
CRAP 20.5
« Mojo/Template.pm ↑ Summary Mojo/Server/Morbo/Backend/Poll.pm »
1 package Mojolicious::Routes::Match;
2 21 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 21 ▶use Mojo::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 21 has [qw(endpoint root)];
7 21 has position => 0;
8 141 ▶has stack => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
9  
10 289 ▶sub find { $_[0]->_match($_[0]->root, $_[1], $_[2]) }
Subroutine find: called CC=1 SCAR=0.0
Pod documented
11  
12 150 ▶sub path_for {
Subroutine path_for: called CC=11 SCAR=24.0
Pod documented
13 150 my ($self, $name, %values) = (shift, Mojo::Util::_options(@_));
14  
15 # Current route
16 150 ▶ my ($route, $current) = (undef, !$name || $name eq 'current');
Condition100%3/3
!$name || $name eq 'current'
l!l&&r!l&&!r
131118
MC/DC100%2/2
!$name || $name eq 'current'
!$name $name eq 'current'
Truth table100%3/3
not(A) || B
A= !$nameB= $name eq 'current'
ABresult
1X1
011
000
17 150 ▶ if ($current) { return {} unless $route = $self->endpoint }
Branch: 2 branches
BranchTrueFalseTotal
if ($current) { } 132 18 150
unless $route = $self->endpoint 7 125 132
18  
19 # Find endpoint
20 18 ▶ else { return {path => $name} unless $route = $self->root->lookup($name) }
Branch: 1 branch
BranchTrueFalseTotal
unless $route = $self->root->lookup($name) 1 17 18
21  
22 # Merge values (clear format)
23 142 ▶ my $captures = $self->stack->[-1] // {};
Condition100%2/2
$self->stack->[-1] // {}
l!l
1357
MC/DC100%1/1
$self->stack->[-1] // {}
$self->stack->[-1]
Truth table100%2/2
A
A= $self->stack->[-1]
Aresult
11
00
24 142 my %merged = (%$captures, format => undef, %values);
25 142 my $pattern = $route->pattern;
26 142 my $constraints = $pattern->constraints;
27 $merged{format} = ($current ? $captures->{format} : undef) // $pattern->defaults->{format}
28 142 ▶ if !exists $values{format} && $constraints->{format} && $constraints->{format} ne '1';
Branch: 2 branches
BranchTrueFalseTotal
if not exists $values{"format"} and $constraints->{"format"} and $constraints->{"format"} ne 1 33 109 142
$current ? : 31 2 33
Condition88%8/9
not exists $values{"format"} and $constraints->{"format"} and $constraints->{"format"} ne 1
!ll&&!rl&&r
981133
not exists $values{"format"} and $constraints->{"format"}
!ll&&!rl&&r
217744
($current ? $captures->{"format"} : undef) // $pattern->defaults->{"format"}
l!l&&r!l&&!r
2409
MC/DC0%0/3
not exists $values{"format"} and $constraints->{"format"} and $constraints->{"format"} ne 1
not exists $values{"format"} $constraints->{"format"} $constraints->{"format"} ne 1
MC/DC50%1/2
($current ? $captures->{"format"} : undef) // $pattern->defaults->{"format"}
($current ? $captures->{"format"} : undef) $pattern->defaults->{"format"}
Truth table0%0/4
not(A) and B and C
A= not exists $values{"format"}B= $constraints->{"format"}C= $constraints->{"format"} ne 1
ABCresult
0XX0
10X0
1100
1111
Truth table66%2/3
A // B
A= ($current ? $captures->{"format"} : undef)B= $pattern->defaults->{"format"}
ABresult
1X1
011
000
29  
30 142 return {path => $route->render(\%merged), websocket => $route->has_websocket};
31 }
32  
33 1153 ▶sub _match {
Subroutine _match: called CC=25 SCAR=33.3
34 1153 my ($self, $r, $c, $options) = @_;
35  
36 # Pattern
37 1153 my $path = $options->{path};
38 1153 my $partial = $r->partial;
39 1153 ▶ my $detect = (my $endpoint = $r->is_endpoint) && !$partial;
Condition100%3/3
(my $endpoint = $r->is_endpoint) && !$partial
!ll&&!rl&&r
5033647
MC/DC100%2/2
(my $endpoint = $r->is_endpoint) && !$partial
(my $endpoint = $r->is_endpoint) !$partial
Truth table100%3/3
A && not(B)
A= (my $endpoint = $r->is_endpoint)B= !$partial
ABresult
0X0
100
111
40 1153 ▶ return undef unless my $captures = $r->pattern->match_partial(\$path, $detect);
Branch: 1 branch
BranchTrueFalseTotal
unless my $captures = $r->pattern->match_partial(\$path, $detect) 332 821 1153
41 821 local $options->{path} = $path;
42 821 ▶ local @{$self->{captures} //= {}}{keys %$captures} = values %$captures;
Condition100%2/2
$self->{"captures"} //= {}
l!l
533288
MC/DC100%1/1
$self->{"captures"} //= {}
$self->{"captures"}
Truth table100%2/2
A
A= $self->{"captures"}
Aresult
11
00
43 821 $captures = $self->{captures};
44  
45 # Method
46 821 my $methods = $r->methods;
47 821 ▶ return undef if $methods && !grep { $_ eq $options->{method} } @$methods;
Branch: 1 branch
BranchTrueFalseTotal
if $methods and not grep({$_ eq $options->{"method"};} @$methods) 29 792 821
Condition100%3/3
$methods and not grep({$_ eq $options->{"method"};} @$methods)
!ll&&!rl&&r
7246829
MC/DC100%2/2
$methods and not grep({$_ eq $options->{"method"};} @$methods)
$methods not grep({$_ eq $options->{"method"};} @$methods)
Truth table100%3/3
A and not(B)
A= $methodsB= not grep({$_ eq $options->{"method"};} @$methods)
ABresult
0X0
100
111
48  
49 # Conditions
50 792 ▶ if (my $over = $r->requires) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $over = $r->requires) 0 792 792
51 0 ▶ my $conditions = $self->{conditions} ||= $self->root->conditions;
Condition0%0/3
$self->{"conditions"} ||= $self->root->conditions
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"conditions"} ||= $self->root->conditions
$self->{"conditions"} $self->root->conditions
Truth table0%0/3
A ||= B
A= $self->{"conditions"}B= $self->root->conditions
ABresult
1X1
011
000
52 0 ▶ for (my $i = 0; $i < @$over; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$over) 0 0 0
53 0 ▶ return undef unless my $condition = $conditions->{$over->[$i]};
Branch: 1 branch
BranchTrueFalseTotal
unless my $condition = $$conditions{$over->[$i]} 0 0 0
54 0 ▶ return undef if !$condition->($r, $c, $captures, $over->[$i + 1]);
Branch: 1 branch
BranchTrueFalseTotal
unless &$condition($r, $c, $captures, $$over[$i + 1]) 0 0 0
55 }
56 }
57  
58 # WebSocket
59 792 ▶ return undef if $r->is_websocket && !$options->{websocket};
Branch: 1 branch
BranchTrueFalseTotal
if $r->is_websocket and not $options->{"websocket"} 1 791 792
Condition100%3/3
$r->is_websocket and not $options->{"websocket"}
!ll&&!rl&&r
78921
MC/DC100%2/2
$r->is_websocket and not $options->{"websocket"}
$r->is_websocket not $options->{"websocket"}
Truth table100%3/3
A and not(B)
A= $r->is_websocketB= not $options->{"websocket"}
ABresult
0X0
100
111
60  
61 # Partial
62 791 ▶ my $empty = !length $path || $path eq '/';
Condition100%3/3
!length($path) || $path eq '/'
l!l&&r!l&&!r
21218561
MC/DC100%2/2
!length($path) || $path eq '/'
!length($path) $path eq '/'
Truth table100%3/3
not(A) || B
A= !length($path)B= $path eq '/'
ABresult
1X1
011
000
63 791 ▶ if ($partial) {
Branch: 1 branch
BranchTrueFalseTotal
if ($partial) 3 788 791
64 3 $captures->{path} = $path;
65 3 $self->endpoint($r);
66 3 $empty = 1;
67 }
68  
69 # Endpoint (or intermediate destination)
70 791 ▶ if (($endpoint && $empty) || $r->inline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($endpoint and $empty or $r->inline) 237 554 791
Condition100%6/6
$endpoint and $empty or $r->inline
l!l&&r!l&&!r
21423554
$endpoint and $empty
!ll&&!rl&&r
385192214
MC/DC100%3/3
$endpoint and $empty or $r->inline
$endpoint $empty $r->inline
Truth table80%4/5
A and B or C
A= $endpointB= $emptyC= $r->inline
ABCresult
11X1
0X11
1011
0X00
1000
71 237 push @{$self->stack}, {%$captures};
72 237 ▶ if ($endpoint && $empty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($endpoint and $empty) 214 23 237
Condition66%2/3
$endpoint and $empty
!ll&&!rl&&r
230214
MC/DC50%1/2
$endpoint and $empty
$endpoint $empty
Truth table66%2/3
A and B
A= $endpointB= $empty
ABresult
0X0
100
111
73 214 my $format = $captures->{format};
74 214 ▶ if ($format) { $_->{format} = $format for @{$self->stack} }
Branch: 1 branch
BranchTrueFalseTotal
if ($format) 32 182 214
75 214 return !!$self->endpoint($r);
76 }
77 23 delete @$captures{qw(app cb)};
78 }
79  
80 # Match children
81 577 ▶ my $snapshot = $r->parent ? [@{$self->stack}] : [];
Branch: 1 branch
BranchTrueFalseTotal
$r->parent ? : 289 288 577
82 577 for my $child (@{$r->_candidates($path)}) {
83 864 ▶ return 1 if $self->_match($child, $c, $options);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_match($child, $c, $options) 278 586 864
84 586 $self->stack([@$snapshot]);
85 }
86 }
87  
88 21 1;
89  
90 =encoding utf8
91  
92 =head1 NAME
93  
94 Mojolicious::Routes::Match - Find routes
95  
96 =head1 SYNOPSIS
97  
98 use Mojolicious::Controller;
99 use Mojolicious::Routes;
100 use Mojolicious::Routes::Match;
101  
102 # Routes
103 my $r = Mojolicious::Routes->new;
104 $r->get('/user/:id');
105 $r->put('/user/:id');
106  
107 # Match
108 my $c = Mojolicious::Controller->new;
109 my $match = Mojolicious::Routes::Match->new(root => $r);
110 $match->find($c => {method => 'PUT', path => '/user/23'});
111 say $match->stack->[0]{id};
112  
113 # Render
114 say $match->path_for->{path};
115 say $match->path_for(id => 24)->{path};
116  
117 =head1 DESCRIPTION
118  
119 L<Mojolicious::Routes::Match> finds routes in L<Mojolicious::Routes> structures.
120  
121 =head1 ATTRIBUTES
122  
123 L<Mojolicious::Routes::Match> implements the following attributes.
124  
125 =head2 endpoint
126  
127 my $route = $match->endpoint;
128 $match = $match->endpoint(Mojolicious::Routes::Route->new);
129  
130 The route endpoint that matched, usually a L<Mojolicious::Routes::Route> object.
131  
132 =head2 position
133  
134 my $position = $match->position;
135 $match = $match->position(2);
136  
137 Current position on the L</"stack">, defaults to C<0>.
138  
139 =head2 root
140  
141 my $root = $match->root;
142 $match = $match->root(Mojolicious::Routes->new);
143  
144 The root of the route structure, usually a L<Mojolicious::Routes> object.
145  
146 =head2 stack
147  
148 my $stack = $match->stack;
149 $match = $match->stack([{action => 'foo'}, {action => 'bar'}]);
150  
151 Captured parameters with nesting history.
152  
153 =head1 METHODS
154  
155 L<Mojolicious::Routes::Match> inherits all methods from L<Mojo::Base> and implements the following new ones.
156  
157 =head2 find
158  
159 $match->find(Mojolicious::Controller->new, {method => 'GET', path => '/'});
160  
161 Match controller and options against L</"root"> to find an appropriate L</"endpoint">.
162  
163 =head2 path_for
164  
165 my $info = $match->path_for;
166 my $info = $match->path_for(foo => 'bar');
167 my $info = $match->path_for({foo => 'bar'});
168 my $info = $match->path_for('named');
169 my $info = $match->path_for('named', foo => 'bar');
170 my $info = $match->path_for('named', {foo => 'bar'});
171  
172 Render matching route with parameters into path.
173  
174 =head1 SEE ALSO
175  
176 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
177  
178 =cut
« Mojo/Template.pm ↑ Summary Mojo/Server/Morbo/Backend/Poll.pm »