Mojolicious/Routes/Route.pm

filter: Statementstmt 98.0% 200 / 204 Branchbran 94.9% 93 / 98 Conditioncond 83.3% 40 / 48 MC/DCmcdc 62.0% 18 / 29 Subroutinesub 100% 44 / 44 Podpod 96.4% 27 / 28 totaltotal 93.5% 422 / 451 CC 74 SCAR 17.6
CC
74
Cov
95%
_generate_route
12.0
_prefixes
11.0
_candidates
10.0
CRAP 5.8
« Mojo/UserAgent/Proxy.pm ↑ Summary Mojo/Promise.pm »
1 package Mojolicious::Routes::Route;
2 21 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 21 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 21 ▶use Mojo::DynamicMethods -dispatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 21 ▶use Mojo::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 21 ▶use Mojolicious::Routes::Pattern;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 21 has [qw(inline partial)];
10 458 ▶has 'children' => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
11 21 has parent => undef, weak => 1;
12 1239 ▶has pattern => sub { Mojolicious::Routes::Pattern->new };
Subroutine __ANON__: called CC=1 SCAR=0.0
13  
14 # Reserved stash values
15 21 my %RESERVED = map { $_ => 1 } (
16 qw(action app cb controller data extends format handler inline json layout namespace path status template text),
17 qw(variant)
18 );
19  
20 4 ▶sub BUILD_DYNAMIC {
Subroutine BUILD_DYNAMIC: called CC=1 SCAR=0.5
Pod undocumented
21 4 my ($class, $method, $dyn_methods) = @_;
22  
23 27 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=8.4
Subroutine fun: called CC=2 SCAR=8.4
24 27 my $self = shift;
25 27 my $dynamic = $dyn_methods->{$self->root}{$method};
26 27 ▶ return $self->$dynamic(@_) if $dynamic;
Branch: 2 branches
BranchTrueFalseTotal
if $dynamic 27 0 27
if $dynamic 36 0 36
27 0 my $package = ref($self);
28 0 croak qq{Can't locate object method "$method" via package "$package"};
29 4 };
30 }
31  
32 1181 ▶sub add_child {
Subroutine add_child: called CC=1 SCAR=0.0
Pod documented
33 1181 my ($self, $route) = @_;
34 1181 _invalidate($self);
35 1181 push @{$self->children}, $route->remove->parent($self);
36 1181 $route->pattern->types($self->root->types);
37 1181 return $self;
38 }
39  
40 1098 ▶sub any { shift->_generate_route(ref $_[0] eq 'ARRAY' ? shift : [], @_) }
Subroutine any: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 1 1097 1098
41  
42 1 ▶sub delete { shift->_generate_route(DELETE => @_) }
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
43  
44 19 ▶sub find { shift->_index->{shift()} }
Subroutine find: called CC=1 SCAR=0.0
Pod documented
45  
46 48 ▶sub get { shift->_generate_route(GET => @_) }
Subroutine get: called CC=1 SCAR=0.0
Pod documented
47  
48 1852 ▶sub has_custom_name { !!shift->{custom} }
Subroutine has_custom_name: called CC=1 SCAR=0.0
Pod documented
49  
50 142 ▶sub has_websocket {
Subroutine has_websocket: called CC=2 SCAR=6.9
Pod documented
51 142 my $self = shift;
52 142 ▶ return $self->{has_websocket} if exists $self->{has_websocket};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"has_websocket"} 76 66 142
53 66 return $self->{has_websocket} = grep { $_->is_websocket } @{$self->_chain};
54 }
55  
56 1668 ▶sub is_endpoint { $_[0]->inline ? undef : !@{$_[0]->children} }
Subroutine is_endpoint: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->inline ? : 64 1604 1668
57  
58 166 ▶sub is_reserved { !!$RESERVED{$_[1]} }
Subroutine is_reserved: called CC=1 SCAR=0.0
Pod documented
59  
60 957 ▶sub is_websocket { !!shift->{websocket} }
Subroutine is_websocket: called CC=1 SCAR=0.0
Pod documented
61  
62 2537 ▶sub methods {
Subroutine methods: called CC=4 SCAR=13.9
Pod documented
63 2537 my $self = shift;
64 2537 ▶ return $self->{methods} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1359 1178 2537
65 1178 ▶ my $methods = [map uc($_), @{ref $_[0] ? $_[0] : [@_]}];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1094 84 1178
66 1178 ▶ $self->{methods} = $methods if @$methods;
Branch: 1 branch
BranchTrueFalseTotal
if @$methods 85 1093 1178
67 1178 return $self;
68 }
69  
70 1887 ▶sub name {
Subroutine name: called CC=2 SCAR=6.9
Pod documented
71 1887 my $self = shift;
72 1887 ▶ return $self->{name} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1857 30 1887
73 30 @$self{qw(name custom)} = (shift, 1);
74 30 return $self;
75 }
76  
77 1 ▶sub options { shift->_generate_route(OPTIONS => @_) }
Subroutine options: called CC=1 SCAR=0.0
Pod documented
78  
79 1180 ▶sub parse {
Subroutine parse: called CC=2 SCAR=6.9
Pod documented
80 1180 my $self = shift;
81 1180 ▶ $self->{name} = $self->pattern->parse(@_)->unparsed // '';
Condition100%2/2
$self->pattern->parse(@_)->unparsed // ""
l!l
110080
MC/DC100%1/1
$self->pattern->parse(@_)->unparsed // ""
$self->pattern->parse(@_)->unparsed
Truth table100%2/2
A
A= $self->pattern->parse(@_)->unparsed
Aresult
11
00
82 1180 $self->{name} =~ s/\W+//g;
83 1180 return $self;
84 }
85  
86 1 ▶sub patch { shift->_generate_route(PATCH => @_) }
Subroutine patch: called CC=1 SCAR=0.0
Pod documented
87 4 ▶sub post { shift->_generate_route(POST => @_) }
Subroutine post: called CC=1 SCAR=0.0
Pod documented
88 22 ▶sub put { shift->_generate_route(PUT => @_) }
Subroutine put: called CC=1 SCAR=0.0
Pod documented
89 1 ▶sub query { shift->_generate_route(QUERY => @_) }
Subroutine query: called CC=1 SCAR=0.0
Pod documented
90  
91 1184 ▶sub remove {
Subroutine remove: called CC=2 SCAR=6.9
Pod documented
92 1184 my $self = shift;
93 1184 ▶ return $self unless my $parent = $self->parent;
Branch: 1 branch
BranchTrueFalseTotal
unless my $parent = $self->parent 1181 3 1184
94 3 _invalidate($parent);
95 3 @{$parent->children} = grep { $_ ne $self } @{$parent->children};
96 3 return $self->parent(undef);
97 }
98  
99 145 ▶sub render {
Subroutine render: called CC=3 SCAR=11.0
Pod documented
100 145 my ($self, $values) = @_;
101 145 ▶ my $path = join '', map { $_->pattern->render($values, !@{$_->children} && !$_->partial) } @{$self->_chain};
Condition66%2/3
@{$_->children;} || $_->partial
l!l&&r!l&&!r
2080145
MC/DC50%1/2
@{$_->children;} || $_->partial
@{$_->children;} $_->partial
Truth table66%2/3
A || B
A= @{$_->children;}B= $_->partial
ABresult
1X1
011
000
102 145 ▶ return $path || '/';
Condition100%2/2
$path || '/'
l!l
1441
MC/DC100%1/1
$path || '/'
$path
Truth table100%2/2
A
A= $path
Aresult
11
00
103 }
104  
105 1248 ▶sub root { shift->_chain->[0] }
Subroutine root: called CC=1 SCAR=0.0
Pod documented
106  
107 1970 ▶sub requires {
Subroutine requires: called CC=4 SCAR=13.9
Pod documented
108 1970 my $self = shift;
109  
110 # Routes with conditions can't be cached
111 1970 ▶ return $self->{requires} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 794 1176 1970
112 1176 ▶ my $conditions = ref $_[0] eq 'ARRAY' ? $_[0] : [@_];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 1176 0 1176
113 1176 ▶ return $self unless @$conditions;
Branch: 1 branch
BranchTrueFalseTotal
unless @$conditions 1174 2 1176
114 2 $self->{requires} = $conditions;
115 2 $self->root->cache->max_keys(0);
116  
117 2 return $self;
118 }
119  
120 7 ▶sub suggested_method {
Subroutine suggested_method: called CC=9 SCAR=22.0
Pod documented
121 7 my $self = shift;
122  
123 7 my %via;
124 7 for my $route (@{$self->_chain}) {
125 16 ▶ next unless my @via = @{$route->methods // []};
Branch: 1 branch
BranchTrueFalseTotal
unless my(@via) = @{$route->methods // [];} 9 7 16
Condition100%2/2
$route->methods // []
l!l
79
MC/DC100%1/1
$route->methods // []
$route->methods
Truth table100%2/2
A
A= $route->methods
Aresult
11
00
126 7 ▶ %via = map { $_ => 1 } keys %via ? grep { $via{$_} } @via : @via;
Branch: 1 branch
BranchTrueFalseTotal
keys %via ? : 2 5 7
127 }
128  
129 7 ▶ return 'POST' if $via{POST} && !$via{GET};
Branch: 1 branch
BranchTrueFalseTotal
if $via{"POST"} and not $via{"GET"} 1 6 7
Condition100%3/3
$via{"POST"} and not $via{"GET"}
!ll&&!rl&&r
511
MC/DC100%2/2
$via{"POST"} and not $via{"GET"}
$via{"POST"} not $via{"GET"}
Truth table100%3/3
A and not(B)
A= $via{"POST"}B= not $via{"GET"}
ABresult
0X0
100
111
130 6 ▶ return $via{GET} ? 'GET' : (sort keys %via)[0] || 'GET';
Branch: 1 branch
BranchTrueFalseTotal
$via{'GET'} ? : 3 3 6
Condition100%2/2
(sort keys %via)[0] or "GET"
l!l
12
MC/DC100%1/1
(sort keys %via)[0] or "GET"
(sort keys %via)[0]
Truth table100%2/2
A
A= (sort keys %via)[0]
Aresult
11
00
131 }
132  
133 2248 ▶sub to {
Subroutine to: called CC=8 SCAR=20.8
Pod documented
134 2248 my $self = shift;
135  
136 2248 my $pattern = $self->pattern;
137 2248 ▶ return $pattern->defaults unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 2247 2248
138 2247 my ($shortcut, %defaults) = Mojo::Util::_options(@_);
139  
140 2247 ▶ if ($shortcut) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shortcut) 846 1401 2247
141  
142 # Application
143 846 ▶ if (ref $shortcut || $shortcut =~ /^[\w:]+$/) { $defaults{app} = $shortcut }
Branch: 2 branches
BranchTrueFalseTotal
if (ref $shortcut or $shortcut =~ /^[\w:]+$/u) { } 2 844 846
elsif ($shortcut =~ /^([\w\-:]+)?\#(\w+)?$/u) { } 844 0 844
Condition66%2/3
ref $shortcut or $shortcut =~ /^[\w:]+$/u
l!l&&r!l&&!r
20844
MC/DC50%1/2
ref $shortcut or $shortcut =~ /^[\w:]+$/u
ref $shortcut $shortcut =~ /^[\w:]+$/u
Truth table66%2/3
A or B
A= ref $shortcutB= $shortcut =~ /^[\w:]+$/u
ABresult
1X1
011
000
144  
145 # Controller and action
146 elsif ($shortcut =~ /^([\w\-:]+)?\#(\w+)?$/) {
147 844 ▶ $defaults{controller} = $1 if defined $1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $1 794 50 844
148 844 ▶ $defaults{action} = $2 if defined $2;
Branch: 1 branch
BranchTrueFalseTotal
if defined $2 819 25 844
149 }
150 }
151  
152 2247 @{$pattern->defaults}{keys %defaults} = values %defaults;
153  
154 2247 return $self;
155 }
156  
157 6 ▶sub to_string {
Subroutine to_string: called CC=2 SCAR=6.9
Pod documented
158 6 ▶ join '', map { $_->pattern->unparsed // '' } @{shift->_chain};
Condition100%2/2
$_->pattern->unparsed // ""
l!l
86
MC/DC100%1/1
$_->pattern->unparsed // ""
$_->pattern->unparsed
Truth table100%2/2
A
A= $_->pattern->unparsed
Aresult
11
00
159 }
160  
161 3 ▶sub under { shift->_generate_route(under => @_) }
Subroutine under: called CC=1 SCAR=0.0
Pod documented
162  
163 4 ▶sub websocket {
Subroutine websocket: called CC=1 SCAR=0.0
Pod documented
164 4 my $route = shift->get(@_);
165 4 $route->{websocket} = 1;
166 4 return $route;
167 }
168  
169 1472 ▶sub _chain {
Subroutine _chain: called CC=2 SCAR=6.9
170 1472 my @chain = (my $parent = shift);
171 1472 ▶ unshift @chain, $parent while $parent = $parent->parent;
Branch: 1 branch
BranchTrueFalseTotal
$parent = $parent->parent and do { unshift @chain, $parent } 522 1472 1994
172 1472 return \@chain;
173 }
174  
175 1179 ▶sub _generate_route {
Subroutine _generate_route: called CC=12 SCAR=24.9
176 1179 my ($self, $methods, @args) = @_;
177  
178 1179 my (@conditions, @constraints, %defaults, $name, $pattern);
179 1179 ▶ while (defined(my $arg = shift @args)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $arg = shift @args)) 1260 1179 2439
180  
181 # First scalar is the pattern
182 1260 ▶ if (!ref $arg && !$pattern) { $pattern = $arg }
Branch: 6 branches
BranchTrueFalseTotal
if (not ref $arg || $pattern) { } 1113 147 1260
elsif (not ref $arg and @args) { } 2 145 147
elsif (not ref $arg) { } 4 141 145
elsif (ref $arg eq 'CODE') { } 38 103 141
elsif (ref $arg eq 'ARRAY') { } 74 29 103
elsif (ref $arg eq 'HASH') { } 29 0 29
Condition100%3/3
ref $arg || $pattern
l!l&&r!l&&!r
14161113
MC/DC100%2/2
ref $arg || $pattern
ref $arg $pattern
Truth table100%3/3
A || B
A= ref $argB= $pattern
ABresult
1X1
011
000
183  
184 # Scalar
185 2 ▶ elsif (!ref $arg && @args) { push @conditions, $arg, shift @args }
Condition100%3/3
not ref $arg and @args
!ll&&!rl&&r
14142
MC/DC100%2/2
not ref $arg and @args
not ref $arg @args
Truth table100%3/3
not(A) and B
A= not ref $argB= @args
ABresult
0X0
100
111
186  
187 # Last scalar is the route name
188 4 elsif (!ref $arg) { $name = $arg }
189  
190 # Callback
191 38 elsif (ref $arg eq 'CODE') { $defaults{cb} = $arg }
192  
193 # Constraints
194 74 elsif (ref $arg eq 'ARRAY') { push @constraints, @$arg }
195  
196 # Defaults
197 29 elsif (ref $arg eq 'HASH') { %defaults = (%defaults, %$arg) }
198 }
199  
200 1179 my $route = $self->_route($pattern, @constraints)->requires(\@conditions)->to(\%defaults);
201 1175 ▶ $methods eq 'under' ? $route->inline(1) : $route->methods($methods);
Branch: 1 branch
BranchTrueFalseTotal
$methods eq 'under' ? : 3 1172 1175
202  
203 1175 ▶ return defined $name ? $route->name($name) : $route;
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 4 1171 1175
204 }
205  
206 577 ▶sub _candidates {
Subroutine _candidates: called CC=10 SCAR=23.0
207 577 my ($self, $path) = @_;
208  
209 # Index children by the text their patterns start with (routes are frozen after the first request)
210 577 my $children = $self->children;
211 577 ▶ return $children unless @$children;
Branch: 1 branch
BranchTrueFalseTotal
unless @$children 192 385 577
212 385 my $index = $self->{prefixes};
213 385 ▶ unless ($index) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($index) 56 329 385
214 56 $index = $self->{prefixes} = {any => [], text => {}};
215 56 for my $i (0 .. $#$children) {
216 380 ▶ if (my $prefixes = $children->[$i]->_prefixes) { push @{$index->{text}{length $_}{$_}}, $i for @$prefixes }
Branch: 1 branch
BranchTrueFalseTotal
if (my $prefixes = $children->[$i]->_prefixes) { } 348 32 380
217 32 else { push @{$index->{any}}, $i }
218 }
219 }
220  
221 # Children that could match the path, in their original order
222 385 my $text = $index->{text};
223 385 ▶ return $children unless %$text;
Branch: 1 branch
BranchTrueFalseTotal
unless %$text 20 365 385
224 365 my %i = map { $_ => 1 } @{$index->{any}};
225 365 for my $len (keys %$text) {
226 3685 ▶ if (my $hit = $text->{$len}{substr $path, 0, $len}) { $i{$_} = 1 for @$hit }
Branch: 1 branch
BranchTrueFalseTotal
if (my $hit = $text->{$len}{substr($path, 0, $len)}) 392 3293 3685
227 }
228 365 return [@$children[sort { $a <=> $b } keys %i]];
229 }
230  
231 1184 ▶sub _invalidate {
Subroutine _invalidate: called CC=2 SCAR=6.9
232 1184 ▶ for (my $route = shift; $route; $route = $route->parent) { delete $route->{prefixes} }
Branch: 1 branch
BranchTrueFalseTotal
if ($route) 1364 1184 2548
233 }
234  
235 # Text one of the paths matched by this route has to start with (undef if it could be anything)
236 396 ▶sub _prefixes {
Subroutine _prefixes: called CC=11 SCAR=24.0
237 396 my $self = shift;
238  
239 # Custom regexes and pattern classes could match anything
240 396 my $pattern = $self->pattern;
241 396 my $regex = $pattern->regex;
242 396 ▶ return undef if ref $pattern ne 'Mojolicious::Routes::Pattern' || ($regex && $regex != ($pattern->{compiled} // 0));
Branch: 1 branch
BranchTrueFalseTotal
if ref $pattern ne "Mojolicious::Routes::Pattern" or $regex and $regex != ($pattern->{"compiled"} // 0) 2 394 396
Condition87%7/8
ref $pattern ne "Mojolicious::Routes::Pattern" or $regex and $regex != ($pattern->{"compiled"} // 0)
l!l&&r!l&&!r
02394
$regex and $regex != ($pattern->{"compiled"} // 0)
!ll&&!rl&&r
383112
$pattern->{"compiled"} // 0
l!l
112
MC/DC66%2/3
ref $pattern ne "Mojolicious::Routes::Pattern" or $regex and $regex != ($pattern->{"compiled"} // 0)
ref $pattern ne "Mojolicious::Routes::Pattern" $regex $regex != ($pattern->{"compiled"} // 0)
MC/DC100%1/1
$pattern->{"compiled"} // 0
$pattern->{"compiled"}
Truth table75%3/4
A or B and C
A= ref $pattern ne "Mojolicious::Routes::Pattern"B= $regexC= $regex != ($pattern->{"compiled"} // 0)
ABCresult
1XX1
0111
00X0
0100
Truth table100%2/2
A
A= $pattern->{"compiled"}
Aresult
11
00
243 394 ▶ if (my $first = $pattern->tree->[0]) { return $first->[0] eq 'text' ? [$first->[1]] : undef }
Branch: 2 branches
BranchTrueFalseTotal
if (my $first = $pattern->tree->[0]) 368 26 394
$first->[0] eq 'text' ? : 352 16 368
244  
245 # Routes with an empty pattern pass the path on to their children
246 26 my $children = $self->children;
247 26 ▶ return undef if $self->partial || !@$children;
Branch: 1 branch
BranchTrueFalseTotal
if $self->partial or not @$children 14 12 26
Condition66%2/3
$self->partial or not @$children
l!l&&r!l&&!r
01412
MC/DC50%1/2
$self->partial or not @$children
$self->partial not @$children
Truth table66%2/3
A or not(B)
A= $self->partialB= not @$children
ABresult
1X1
011
000
248 12 my @prefixes;
249 12 for my $child (@$children) {
250 16 ▶ return undef unless my $prefixes = $child->_prefixes;
Branch: 1 branch
BranchTrueFalseTotal
unless my $prefixes = $child->_prefixes 2 14 16
251 14 push @prefixes, @$prefixes;
252 }
253 10 return \@prefixes;
254 }
255  
256 23 ▶sub _index {
Subroutine _index: called CC=5 SCAR=16.1
257 23 my $self = shift;
258  
259 23 my (%auto, %custom);
260 23 my @children = (@{$self->children});
261 23 ▶ while (my $child = shift @children) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $child = shift @children) 1337 23 1360
262 1337 ▶ if ($child->has_custom_name) { $custom{$child->name} ||= $child }
Branch: 1 branch
BranchTrueFalseTotal
if ($child->has_custom_name) { } 62 1275 1337
Condition66%2/3
$custom{$child->name} ||= $child
l!l&&r!l&&!r
10520
MC/DC0%0/2
$custom{$child->name} ||= $child
$custom{$child->name} $child
Truth table66%2/3
A ||= B
A= $custom{$child->name}B= $child
ABresult
1X1
011
000
263 1275 ▶ else { $auto{$child->name} ||= $child }
Condition66%2/3
$auto{$child->name} ||= $child
l!l&&r!l&&!r
17411010
MC/DC0%0/2
$auto{$child->name} ||= $child
$auto{$child->name} $child
Truth table66%2/3
A ||= B
A= $auto{$child->name}B= $child
ABresult
1X1
011
000
264 1337 push @children, @{$child->children};
265 }
266  
267 23 return {%auto, %custom};
268 }
269  
270 1179 ▶sub _route {
Subroutine _route: called CC=6 SCAR=17.9
271 1179 my $self = shift;
272  
273 1179 my $route = $self->add_child(__PACKAGE__->new->parse(@_))->children->[-1];
274 1179 my $new_pattern = $route->pattern;
275 4 croak qq{Route pattern "@{[$new_pattern->unparsed]}" contains a reserved stash value}
276 1179 ▶ if grep { $self->is_reserved($_) } @{$new_pattern->placeholders};
Branch: 1 branch
BranchTrueFalseTotal
if grep {$self->is_reserved($_);} @{$new_pattern->placeholders;} 4 1175 1179
277  
278 1175 my $old_pattern = $self->pattern;
279 1175 my $constraints = $old_pattern->constraints;
280 1175 ▶ $new_pattern->constraints->{format} //= $constraints->{format} if exists $constraints->{format};
Branch: 1 branch
BranchTrueFalseTotal
if exists $constraints->{"format"} 7 1168 1175
Condition66%2/3
$new_pattern->constraints->{"format"} //= $constraints->{"format"}
l!l&&r!l&&!r
250
MC/DC0%0/2
$new_pattern->constraints->{"format"} //= $constraints->{"format"}
$new_pattern->constraints->{"format"} $constraints->{"format"}
Truth table66%2/3
A //= B
A= $new_pattern->constraints->{"format"}B= $constraints->{"format"}
ABresult
1X1
011
000
281 1175 my $defaults = $old_pattern->defaults;
282 1175 ▶ $new_pattern->defaults->{format} //= $defaults->{format} if exists $defaults->{format};
Branch: 1 branch
BranchTrueFalseTotal
if exists $defaults->{"format"} 5 1170 1175
Condition66%2/3
$new_pattern->defaults->{"format"} //= $defaults->{"format"}
l!l&&r!l&&!r
023
MC/DC50%1/2
$new_pattern->defaults->{"format"} //= $defaults->{"format"}
$new_pattern->defaults->{"format"} $defaults->{"format"}
Truth table66%2/3
A //= B
A= $new_pattern->defaults->{"format"}B= $defaults->{"format"}
ABresult
1X1
011
000
283  
284 1175 return $route;
285 }
286  
287 21 1;
288  
289 =encoding utf8
290  
291 =head1 NAME
292  
293 Mojolicious::Routes::Route - Route
294  
295 =head1 SYNOPSIS
296  
297 use Mojolicious::Routes::Route;
298  
299 my $r = Mojolicious::Routes::Route->new;
300  
301 =head1 DESCRIPTION
302  
303 L<Mojolicious::Routes::Route> is the route container used by L<Mojolicious::Routes>.
304  
305 =head1 ATTRIBUTES
306  
307 L<Mojolicious::Routes::Route> implements the following attributes.
308  
309 =head2 children
310  
311 my $children = $r->children;
312 $r = $r->children([Mojolicious::Routes::Route->new]);
313  
314 The children of this route, used for nesting routes.
315  
316 =head2 inline
317  
318 my $bool = $r->inline;
319 $r = $r->inline($bool);
320  
321 Allow L</"under"> semantics for this route.
322  
323 =head2 parent
324  
325 my $parent = $r->parent;
326 $r = $r->parent(Mojolicious::Routes::Route->new);
327  
328 The parent of this route, usually a L<Mojolicious::Routes::Route> object. Note that this attribute is weakened.
329  
330 =head2 partial
331  
332 my $bool = $r->partial;
333 $r = $r->partial($bool);
334  
335 Route has no specific end, remaining characters will be captured in C<path>.
336  
337 =head2 pattern
338  
339 my $pattern = $r->pattern;
340 $r = $r->pattern(Mojolicious::Routes::Pattern->new);
341  
342 Pattern for this route, defaults to a L<Mojolicious::Routes::Pattern> object.
343  
344 =head1 METHODS
345  
346 L<Mojolicious::Routes::Route> inherits all methods from L<Mojo::Base> and implements the following new ones.
347  
348 =head2 add_child
349  
350 $r = $r->add_child(Mojolicious::Routes::Route->new);
351  
352 Add a child to this route, it will be automatically removed from its current parent if necessary.
353  
354 # Reattach route
355 $r->add_child($r->find('foo'));
356  
357 =head2 any
358  
359 my $route = $r->any;
360 my $route = $r->any('/:foo');
361 my $route = $r->any('/:foo' => sub ($c) {...});
362 my $route = $r->any('/:foo' => sub ($c) {...} => 'name');
363 my $route = $r->any('/:foo' => {foo => 'bar'} => sub ($c) {...});
364 my $route = $r->any('/:foo' => [foo => qr/\w+/] => sub ($c) {...});
365 my $route = $r->any('/:foo' => (agent => qr/Firefox/) => sub ($c) {...});
366 my $route = $r->any(['GET', 'POST'] => '/:foo' => sub ($c) {...});
367 my $route = $r->any(['GET', 'POST'] => '/:foo' => [foo => qr/\w+/]);
368  
369 Generate L<Mojolicious::Routes::Route> object matching any of the listed HTTP request methods or all.
370  
371 # Route with pattern and destination
372 $r->any('/user')->to('user#whatever');
373  
374 All arguments are optional, but some have to appear in a certain order, like the two supported array reference values,
375 which contain the HTTP methods to match and restrictive placeholders.
376  
377 # Route with HTTP methods, pattern, restrictive placeholders and destination
378 $r->any(['DELETE', 'PUT'] => '/:foo' => [foo => qr/\w+/])->to('foo#bar');
379  
380 There are also two supported string values, containing the route pattern and the route name, defaulting to the pattern
381 C</> and a name based on the pattern.
382  
383 # Route with pattern, name and destination
384 $r->any('/:foo' => 'foo_route')->to('foo#bar');
385  
386 An arbitrary number of key/value pairs in between the route pattern and name can be used to specify route conditions.
387  
388 # Route with pattern, condition and destination
389 $r->any('/' => (agent => qr/Firefox/))->to('foo#bar');
390  
391 A hash reference is used to specify optional placeholders and default values for the stash.
392  
393 # Route with pattern, optional placeholder and destination
394 $r->any('/:foo' => {foo => 'bar'})->to('foo#bar');
395  
396 And a code reference can be used to specify a C<cb> value to be merged into the default values for the stash.
397  
398 # Route with pattern and a closure as destination
399 $r->any('/:foo' => sub ($c) {
400 $c->render(text => 'Hello World!');
401 });
402  
403 See L<Mojolicious::Guides::Tutorial> and L<Mojolicious::Guides::Routing> for more information.
404  
405 =head2 delete
406  
407 my $route = $r->delete;
408 my $route = $r->delete('/:foo');
409 my $route = $r->delete('/:foo' => sub ($c) {...});
410 my $route = $r->delete('/:foo' => sub ($c) {...} => 'name');
411 my $route = $r->delete('/:foo' => {foo => 'bar'} => sub ($c) {...});
412 my $route = $r->delete('/:foo' => [foo => qr/\w+/] => sub ($c) {...});
413 my $route = $r->delete('/:foo' => (agent => qr/Firefox/) => sub ($c) {...});
414  
415 Generate L<Mojolicious::Routes::Route> object matching only C<DELETE> requests, takes the same arguments as L</"any">
416 (except for the HTTP methods to match, which are implied). See L<Mojolicious::Guides::Tutorial> and
417 L<Mojolicious::Guides::Routing> for more information.
418  
419 # Route with destination
420 $r->delete('/user')->to('user#remove');
421  
422 =head2 find
423  
424 my $route = $r->find('foo');
425  
426 Find child route by name, custom names have precedence over automatically generated ones.
427  
428 # Change default parameters of a named route
429 $r->find('show_user')->to(foo => 'bar');
430  
431 =head2 get
432  
433 my $route = $r->get;
434 my $route = $r->get('/:foo');
435 my $route = $r->get('/:foo' => sub ($c) {...});
436 my $route = $r->get('/:foo' => sub ($c) {...} => 'name');
437 my $route = $r->get('/:foo' => {foo => 'bar'} => sub ($c) {...});
438 my $route = $r->get('/:foo' => [foo => qr/\w+/] => sub ($c) {...});
439 my $route = $r->get('/:foo' => (agent => qr/Firefox/) => sub ($c) {...});
440  
441 Generate L<Mojolicious::Routes::Route> object matching only C<GET> requests, takes the same arguments as L</"any">
442 (except for the HTTP methods to match, which are implied). See L<Mojolicious::Guides::Tutorial> and
443 L<Mojolicious::Guides::Routing> for more information.
444  
445 # Route with destination
446 $r->get('/user')->to('user#show');
447  
448 =head2 has_custom_name
449  
450 my $bool = $r->has_custom_name;
451  
452 Check if this route has a custom name.
453  
454 =head2 has_websocket
455  
456 my $bool = $r->has_websocket;
457  
458 Check if this route has a WebSocket ancestor and cache the result for future checks.
459  
460 =head2 is_endpoint
461  
462 my $bool = $r->is_endpoint;
463  
464 Check if this route qualifies as an endpoint.
465  
466 =head2 is_reserved
467  
468 my $bool = $r->is_reserved('controller');
469  
470 Check if string is a reserved stash value.
471  
472 =head2 is_websocket
473  
474 my $bool = $r->is_websocket;
475  
476 Check if this route is a WebSocket.
477  
478 =head2 methods
479  
480 my $methods = $r->methods;
481 $r = $r->methods('GET');
482 $r = $r->methods('GET', 'POST');
483 $r = $r->methods(['GET', 'POST']);
484  
485 Restrict HTTP methods this route is allowed to handle, defaults to no restrictions.
486  
487 # Route with two methods and destination
488 $r->any('/foo')->methods('GET', 'POST')->to('foo#bar');
489  
490 =head2 name
491  
492 my $name = $r->name;
493 $r = $r->name('foo');
494  
495 The name of this route, defaults to an automatically generated name based on the route pattern. Note that the name
496 C<current> is reserved for referring to the current route.
497  
498 # Route with destination and custom name
499 $r->get('/user')->to('user#show')->name('show_user');
500  
501 =head2 options
502  
503 my $route = $r->options;
504 my $route = $r->options('/:foo');
505 my $route = $r->options('/:foo' => sub ($c) {...});
506 my $route = $r->options('/:foo' => sub ($c) {...} => 'name');
507 my $route = $r->options('/:foo' => {foo => 'bar'} => sub ($c) {...});
508 my $route = $r->options('/:foo' => [foo => qr/\w+/] => sub ($c) {...});
509 my $route = $r->options('/:foo' => (agent => qr/Firefox/) => sub ($c) {...});
510  
511 Generate L<Mojolicious::Routes::Route> object matching only C<OPTIONS> requests, takes the same arguments as L</"any">
512 (except for the HTTP methods to match, which are implied). See L<Mojolicious::Guides::Tutorial> and
513 L<Mojolicious::Guides::Routing> for more information.
514  
515 # Route with destination
516 $r->options('/user')->to('user#overview');
517  
518 =head2 parse
519  
520 $r = $r->parse('/user/:id');
521 $r = $r->parse('/user/:id', id => qr/\d+/);
522 $r = $r->parse(format => ['json', 'yaml']);
523  
524 Parse pattern.
525  
526 =head2 patch
527  
528 my $route = $r->patch;
529 my $route = $r->patch('/:foo');
530 my $route = $r->patch('/:foo' => sub ($c) {...});
531 my $route = $r->patch('/:foo' => sub ($c) {...} => 'name');
532 my $route = $r->patch('/:foo' => {foo => 'bar'} => sub ($c) {...});
533 my $route = $r->patch('/:foo' => [foo => qr/\w+/] => sub ($c) {...});
534 my $route = $r->patch('/:foo' => (agent => qr/Firefox/) => sub ($c) {...});
535  
536 Generate L<Mojolicious::Routes::Route> object matching only C<PATCH> requests, takes the same arguments as L</"any">
537 (except for the HTTP methods to match, which are implied). See L<Mojolicious::Guides::Tutorial> and
538 L<Mojolicious::Guides::Routing> for more information.
539  
540 # Route with destination
541 $r->patch('/user')->to('user#update');
542  
543 =head2 post
544  
545 my $route = $r->post;
546 my $route = $r->post('/:foo');
547 my $route = $r->post('/:foo' => sub ($c) {...});
548 my $route = $r->post('/:foo' => sub ($c) {...} => 'name');
549 my $route = $r->post('/:foo' => {foo => 'bar'} => sub ($c) {...});
550 my $route = $r->post('/:foo' => [foo => qr/\w+/] => sub ($c) {...});
551 my $route = $r->post('/:foo' => (agent => qr/Firefox/) => sub ($c) {...});
552  
553 Generate L<Mojolicious::Routes::Route> object matching only C<POST> requests, takes the same arguments as L</"any">
554 (except for the HTTP methods to match, which are implied). See L<Mojolicious::Guides::Tutorial> and
555 L<Mojolicious::Guides::Routing> for more information.
556  
557 # Route with destination
558 $r->post('/user')->to('user#create');
559  
560 =head2 put
561  
562 my $route = $r->put;
563 my $route = $r->put('/:foo');
564 my $route = $r->put('/:foo' => sub ($c) {...});
565 my $route = $r->put('/:foo' => sub ($c) {...} => 'name');
566 my $route = $r->put('/:foo' => {foo => 'bar'} => sub ($c) {...});
567 my $route = $r->put('/:foo' => [foo => qr/\w+/] => sub ($c) {...});
568 my $route = $r->put('/:foo' => (agent => qr/Firefox/) => sub ($c) {...});
569  
570 Generate L<Mojolicious::Routes::Route> object matching only C<PUT> requests, takes the same arguments as L</"any">
571 (except for the HTTP methods to match, which are implied). See L<Mojolicious::Guides::Tutorial> and
572 L<Mojolicious::Guides::Routing> for more information.
573  
574 # Route with destination
575 $r->put('/user')->to('user#replace');
576  
577 =head2 query
578  
579 my $route = $r->query;
580 my $route = $r->query('/:foo');
581 my $route = $r->query('/:foo' => sub ($c) {...});
582 my $route = $r->query('/:foo' => sub ($c) {...} => 'name');
583 my $route = $r->query('/:foo' => {foo => 'bar'} => sub ($c) {...});
584 my $route = $r->query('/:foo' => [foo => qr/\w+/] => sub ($c) {...});
585 my $route = $r->query('/:foo' => (agent => qr/Firefox/) => sub ($c) {...});
586  
587 Generate L<Mojolicious::Routes::Route> object matching only C<QUERY> requests, takes the same arguments as L</"any">
588 (except for the HTTP methods to match, which are implied). See L<Mojolicious::Guides::Tutorial> and
589 L<Mojolicious::Guides::Routing> for more information.
590  
591 # Route with destination
592 $r->query('/user')->to('user#search');
593  
594 =head2 remove
595  
596 $r = $r->remove;
597  
598 Remove route from parent.
599  
600 # Remove route completely
601 $r->find('foo')->remove;
602  
603 # Reattach route to new parent
604 $r->any('/foo')->add_child($r->find('bar')->remove);
605  
606 =head2 render
607  
608 my $path = $r->render({foo => 'bar'});
609  
610 Render route with parameters into a path.
611  
612 =head2 root
613  
614 my $root = $r->root;
615  
616 The L<Mojolicious::Routes> object this route is a descendant of.
617  
618 =head2 requires
619  
620 my $requires = $r->requires;
621 $r = $r->requires(foo => 1);
622 $r = $r->requires(foo => 1, bar => {baz => 'yada'});
623 $r = $r->requires([foo => 1, bar => {baz => 'yada'}]);
624  
625 Activate conditions for this route. Note that this automatically disables the routing cache, since conditions are too
626 complex for caching.
627  
628 # Route with condition and destination
629 $r->get('/foo')->requires(host => qr/mojolicious\.org/)->to('foo#bar');
630  
631 =head2 suggested_method
632  
633 my $method = $r->suggested_method;
634  
635 Suggested HTTP method for reaching this route, C<GET> and C<POST> are preferred.
636  
637 =head2 to
638  
639 my $defaults = $r->to;
640 $r = $r->to(action => 'foo');
641 $r = $r->to({action => 'foo'});
642 $r = $r->to('controller#action');
643 $r = $r->to('controller#action', foo => 'bar');
644 $r = $r->to('controller#action', {foo => 'bar'});
645 $r = $r->to(Mojolicious->new);
646 $r = $r->to(Mojolicious->new, foo => 'bar');
647 $r = $r->to(Mojolicious->new, {foo => 'bar'});
648 $r = $r->to('MyApp');
649 $r = $r->to('MyApp', foo => 'bar');
650 $r = $r->to('MyApp', {foo => 'bar'});
651  
652 Set default parameters for this route.
653  
654 =head2 to_string
655  
656 my $str = $r->to_string;
657  
658 Stringify the whole route.
659  
660 =head2 under
661  
662 my $route = $r->under(sub ($c) {...});
663 my $route = $r->under('/:foo' => sub ($c) {...});
664 my $route = $r->under('/:foo' => {foo => 'bar'});
665 my $route = $r->under('/:foo' => [foo => qr/\w+/]);
666 my $route = $r->under('/:foo' => (agent => qr/Firefox/));
667 my $route = $r->under([format => ['json', 'yaml']]);
668  
669 Generate L<Mojolicious::Routes::Route> object for a nested route with its own intermediate destination, takes the same
670 arguments as L</"any"> (except for the HTTP methods to match, which are not available). See
671 L<Mojolicious::Guides::Tutorial> and L<Mojolicious::Guides::Routing> for more information.
672  
673 # Longer version
674 $r->any('/:foo' => sub ($c) {...})->inline(1);
675  
676 # Intermediate destination and prefix shared between two routes
677 my $auth = $r->under('/user')->to('user#auth');
678 $auth->get('/show')->to('#show');
679 $auth->post('/create')->to('#create');
680  
681 =head2 websocket
682  
683 my $route = $r->websocket;
684 my $route = $r->websocket('/:foo');
685 my $route = $r->websocket('/:foo' => sub ($c) {...});
686 my $route = $r->websocket('/:foo' => sub ($c) {...} => 'name');
687 my $route = $r->websocket('/:foo' => {foo => 'bar'} => sub ($c) {...});
688 my $route = $r->websocket('/:foo' => [foo => qr/\w+/] => sub ($c) {...});
689 my $route = $r->websocket('/:foo' => (agent => qr/Firefox/) => sub ($c) {...});
690  
691 Generate L<Mojolicious::Routes::Route> object matching only WebSocket handshakes, takes the same arguments as L</"any">
692 (except for the HTTP methods to match, which are implied). See L<Mojolicious::Guides::Tutorial> and
693 L<Mojolicious::Guides::Routing> for more information.
694  
695 # Route with destination
696 $r->websocket('/echo')->to('example#echo');
697  
698 =head1 SHORTCUTS
699  
700 In addition to the L</"ATTRIBUTES"> and L</"METHODS"> above you can also call shortcuts provided by L</"root"> on
701 L<Mojolicious::Routes::Route> objects.
702  
703 # Add a "firefox" shortcut
704 $r->root->add_shortcut(firefox => sub ($r, $path) {
705 $r->get($path, agent => qr/Firefox/);
706 });
707  
708 # Use "firefox" shortcut to generate routes
709 $r->firefox('/welcome')->to('firefox#welcome');
710 $r->firefox('/bye')->to('firefox#bye');
711  
712 =head1 SEE ALSO
713  
714 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
715  
716 =cut
« Mojo/UserAgent/Proxy.pm ↑ Summary Mojo/Promise.pm »