Mojolicious/Routes.pm

filter: Statementstmt 98.4% 130 / 132 Branchbran 90.7% 69 / 76 Conditioncond 85.7% 12 / 14 MC/DCmcdc 66.6% 6 / 9 Subroutinesub 96.1% 25 / 26 Podpod 100% 7 / 7 totaltotal 94.3% 249 / 264 CC 46 SCAR 18.3
CC
46
Cov
95%
_class
13.0
match
9.0
_controller
9.0
CRAP 6.3
« Mojo/File.pm ↑ Summary Mojo/Parameters.pm »
1 package Mojolicious::Routes;
2 21 ▶use Mojo::Base 'Mojolicious::Routes::Route';
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 List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 21 ▶use Mojo::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 21 ▶use Mojo::DynamicMethods;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 21 ▶use Mojo::Loader qw(load_class);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 21 ▶use Mojo::Util qw(camelize);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 ▶has base_classes => sub { [qw(Mojolicious::Controller Mojolicious)] };
Subroutine __ANON__: called CC=1 SCAR=0.0
12 17 ▶has cache => sub { Mojo::Cache->new };
Subroutine __ANON__: called CC=1 SCAR=0.0
13 104 ▶has [qw(conditions shortcuts)] => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
14 60 ▶has types => sub { {num => qr/[0-9]+/} };
Subroutine __ANON__: called CC=1 SCAR=0.0
15 0 ▶has namespaces => sub { [] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
16  
17 251 ▶sub add_condition { $_[0]->conditions->{$_[1]} = $_[2] and return $_[0] }
Subroutine add_condition: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->conditions->{$_[1]} = $_[2] and return $_[0] 251 0 251
18  
19 21 ▶sub add_shortcut {
Subroutine add_shortcut: called CC=1 SCAR=0.0
Pod documented
20 21 my ($self, $name, $cb) = @_;
21 21 $self->shortcuts->{$name} = $cb;
22 21 Mojo::DynamicMethods::register 'Mojolicious::Routes::Route', $self, $name, $cb;
23 21 return $self;
24 }
25  
26 1 ▶sub add_type { $_[0]->types->{$_[1]} = $_[2] and return $_[0] }
Subroutine add_type: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->types->{$_[1]} = $_[2] and return $_[0] 1 0 1
27  
28 234 ▶sub continue {
Subroutine continue: called CC=6 SCAR=17.9
Pod documented
29 234 my ($self, $c) = @_;
30  
31 234 my $match = $c->match;
32 234 my $stack = $match->stack;
33 234 my $position = $match->position;
34 234 ▶ return _render($c) unless my $field = $stack->[$position];
Branch: 1 branch
BranchTrueFalseTotal
unless my $field = $stack->[$position] 102 132 234
35  
36 # Merge captures into stash
37 132 my $stash = $c->stash;
38 132 ▶ @{$stash->{'mojo.captures'} //= {}}{keys %$field} = values %$field;
Condition100%2/2
$stash->{"mojo.captures"} //= {}
l!l
7125
MC/DC100%1/1
$stash->{"mojo.captures"} //= {}
$stash->{"mojo.captures"}
Truth table100%2/2
A
A= $stash->{"mojo.captures"}
Aresult
11
00
39 132 @$stash{keys %$field} = values %$field;
40  
41 132 my $continue;
42 132 my $last = !$stack->[++$position];
43 132 ▶ if (my $cb = $field->{cb}) { $continue = $self->_callback($c, $cb, $last) }
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $field->{'cb'}) { } 26 106 132
44 106 else { $continue = $self->_controller($c, $field, $last) }
45 110 $match->position($position);
46 110 ▶ $self->continue($c) if $last || $continue;
Branch: 1 branch
BranchTrueFalseTotal
if $last or $continue 106 4 110
Condition100%3/3
$last or $continue
l!l&&r!l&&!r
10244
MC/DC100%2/2
$last or $continue
$last $continue
Truth table100%3/3
A or B
A= $lastB= $continue
ABresult
1X1
011
000
47 }
48  
49 150 ▶sub dispatch {
Subroutine dispatch: called CC=2 SCAR=6.9
Pod documented
50 150 my ($self, $c) = @_;
51 150 $self->match($c);
52 150 ▶ @{$c->match->stack} ? $self->continue($c) : return undef;
Branch: 1 branch
BranchTrueFalseTotal
@{$c->match->stack;} ? : 126 24 150
53 101 return 1;
54 }
55  
56 22 ▶sub lookup { ($_[0]{reverse} //= $_[0]->_index)->{$_[1]} }
Subroutine lookup: called CC=2 SCAR=7.2
Pod documented
Condition66%2/3
$_[0]{"reverse"} //= $_[0]->_index
l!l&&r!l&&!r
1840
MC/DC0%0/2
$_[0]{"reverse"} //= $_[0]->_index
$_[0]{"reverse"} $_[0]->_index
Truth table66%2/3
A //= B
A= $_[0]{"reverse"}B= $_[0]->_index
ABresult
1X1
011
000
57  
58 151 ▶sub match {
Subroutine match: called CC=9 SCAR=22.0
Pod documented
59 151 my ($self, $c) = @_;
60  
61 # Path (partial path gets priority)
62 151 my $req = $c->req;
63 151 my $path = $c->stash->{path};
64 151 ▶ if (defined $path) { $path = "/$path" if $path !~ m!^/! }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $path) { } 1 150 151
unless $path =~ m[^/]u 0 1 1
65 150 else { $path = $req->url->path->to_route }
66  
67 # Method (HEAD will be treated as GET)
68 151 my $method = uc $req->method;
69 151 ▶ if ($method eq 'POST' && (my $override = $req->url->query->clone->param('_method'))) { $method = uc $override }
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "POST" and my $override = $req->url->query->clone->param("_method")) 1 150 151
Condition100%3/3
$method eq "POST" and my $override = $req->url->query->clone->param("_method")
!ll&&!rl&&r
14371
MC/DC100%2/2
$method eq "POST" and my $override = $req->url->query->clone->param("_method")
$method eq "POST" my $override = $req->url->query->clone->param("_method")
Truth table100%3/3
A and B
A= $method eq "POST"B= my $override = $req->url->query->clone->param("_method")
ABresult
0X0
100
111
70 151 ▶ $method = 'GET' if $method eq 'HEAD';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "HEAD" 0 151 151
71  
72 # Check cache
73 151 ▶ my $ws = $c->tx->is_websocket ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$c->tx->is_websocket ? : 1 150 151
74 151 my $match = $c->match;
75 151 $match->root($self);
76 151 my $cache = $self->cache;
77 151 ▶ if (my $result = $cache->get("$method:$path:$ws")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $result = $cache->get("${method}:${path}:$ws")) 19 132 151
78 19 return $match->endpoint($result->{endpoint})->stack($result->{stack});
79 }
80  
81 # Check routes
82 132 $match->find($c => {method => $method, path => $path, websocket => $ws});
83 132 ▶ return undef unless my $route = $match->endpoint;
Branch: 1 branch
BranchTrueFalseTotal
unless my $route = $match->endpoint 24 108 132
84 108 $cache->set("$method:$path:$ws" => {endpoint => $route, stack => $match->stack});
85 }
86  
87 88 ▶sub _action { shift->plugins->emit_chain(around_action => @_) }
Subroutine _action: called CC=1 SCAR=0.0
88  
89 26 ▶sub _callback {
Subroutine _callback: called CC=2 SCAR=6.9
90 26 my ($self, $c, $cb, $last) = @_;
91 26 ▶ $c->stash->{'mojo.routed'} = 1 if $last;
Branch: 1 branch
BranchTrueFalseTotal
if $last 24 2 26
92 26 $c->helpers->log->trace('Routing to a callback');
93 26 return _action($c->app, $c, $cb, $last);
94 }
95  
96 106 ▶sub _class {
Subroutine _class: called CC=13 SCAR=25.7
97 106 my ($self, $c, $field) = @_;
98  
99 # Application instance
100 106 ▶ return $field->{app} if ref $field->{app};
Branch: 1 branch
BranchTrueFalseTotal
if ref $field->{"app"} 1 105 106
101  
102 # Application class
103 105 my @classes;
104 105 ▶ my $class = $field->{controller} ? camelize $field->{controller} : '';
Branch: 1 branch
BranchTrueFalseTotal
$field->{'controller'} ? : 82 23 105
105 0 ▶ if ($field->{app}) { push @classes, $field->{app} }
Branch: 3 branches
BranchTrueFalseTotal
if ($field->{'app'}) { } 0 105 105
elsif (defined(my $ns = $field->{'namespace'})) { } 3 102 105
elsif ($class) { } 80 22 102
106  
107 # Specific namespace
108 elsif (defined(my $ns = $field->{namespace})) {
109 3 ▶ croak qq{Namespace "$ns" requires a controller} unless $class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class 1 2 3
110 2 ▶ push @classes, $ns ? "${ns}::$class" : $class;
Branch: 1 branch
BranchTrueFalseTotal
$ns ? : 1 1 2
111 }
112  
113 # All namespaces
114 80 elsif ($class) { push @classes, "${_}::$class" for @{$self->namespaces} }
115  
116 # Try to load all classes
117 104 my $log = $c->helpers->log;
118 104 for my $class (@classes) {
119  
120 # Failed
121 200 ▶ next unless defined(my $found = $self->_load($class));
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $found = $self->_load($class)) 121 76 197
122 76 ▶ croak qq{Class "$class" is not a controller} unless $found;
Branch: 1 branch
BranchTrueFalseTotal
unless $found 4 72 76
123  
124 # Success
125 72 return $class->new(%$c);
126 }
127  
128 # Nothing found
129 25 ▶ return @classes ? croak qq{Controller "$classes[-1]" does not exist} : 0;
Branch: 1 branch
BranchTrueFalseTotal
@classes ? : 3 22 25
130 }
131  
132 106 ▶sub _controller {
Subroutine _controller: called CC=9 SCAR=22.0
133 106 my ($self, $old, $field, $last) = @_;
134  
135 # Load and instantiate controller/application
136 106 my $new;
137 106 ▶ unless ($new = $self->_class($old, $field)) { return defined $new }
Branch: 1 branch
BranchTrueFalseTotal
unless ($new = $self->_class($old, $field)) 22 73 95
138  
139 # Application
140 73 my $class = ref $new;
141 73 my $log = $old->helpers->log;
142 73 ▶ if ($new->isa('Mojolicious')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($new->isa('Mojolicious')) { } 5 68 73
elsif (my $method = $field->{'action'}) { } 67 1 68
143 5 $log->trace(qq{Routing to application "$class"});
144  
145 # Try to connect routes
146 5 ▶ if (my $sub = $new->can('routes')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = $new->can("routes")) 5 0 5
147 5 my $r = $new->$sub;
148 5 ▶ $r->parent($old->match->endpoint) unless $r->parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $r->parent 5 0 5
149 }
150 5 $new->handler($old);
151 5 $old->stash->{'mojo.routed'} = 1;
152 }
153  
154 # Action
155 elsif (my $method = $field->{action}) {
156 67 $log->trace(qq{Routing to controller "$class" and action "$method"});
157  
158 67 ▶ if (my $sub = $new->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = $new->can($method)) { } 62 5 67
159 62 ▶ $old->stash->{'mojo.routed'} = 1 if $last;
Branch: 1 branch
BranchTrueFalseTotal
if $last 54 8 62
160 62 ▶ return 1 if _action($old->app, $new, $sub, $last);
Branch: 1 branch
BranchTrueFalseTotal
if _action($old->app, $new, $sub, $last) 44 8 52
161 }
162  
163 5 else { $log->trace('Action not found in controller') }
164 }
165  
166 1 else { croak qq{Controller "$class" requires an action} }
167  
168 18 return undef;
169 }
170  
171 200 ▶sub _load {
Subroutine _load: called CC=5 SCAR=16.1
172 200 my ($self, $app) = @_;
173  
174 # Load unless already loaded
175 200 ▶ return 1 if $self->{loaded}{$app};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"loaded"}{$app} 56 144 200
176 144 ▶ if (my $e = load_class $app) { ref $e ? die $e : return undef }
Branch: 2 branches
BranchTrueFalseTotal
if (my $e = load_class($app)) 124 20 144
ref $e ? : 3 121 124
177  
178 # Check base classes
179 20 ▶ return 0 unless first { $app->isa($_) } @{$self->base_classes};
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless &first(sub { $app->isa($_); } , @{$self->base_classes;}) 4 16 20
180 16 return $self->{loaded}{$app} = 1;
181 }
182  
183 102 ▶sub _render {
Subroutine _render: called CC=4 SCAR=13.9
184 102 my $c = shift;
185 102 my $stash = $c->stash;
186 102 ▶ return if $stash->{'mojo.rendered'};
Branch: 1 branch
BranchTrueFalseTotal
if $stash->{"mojo.rendered"} 61 41 102
187 41 ▶ $c->render_maybe or $stash->{'mojo.routed'} or croak 'Route without action and nothing to render';
Branch: 1 branch
BranchTrueFalseTotal
$c->render_maybe or $stash->{"mojo.routed"} or croak("Route without action and nothing to render") 38 3 41
Condition66%2/3
$c->render_maybe or $stash->{"mojo.routed"}
l!l&&r!l&&!r
3803
MC/DC50%1/2
$c->render_maybe or $stash->{"mojo.routed"}
$c->render_maybe $stash->{"mojo.routed"}
Truth table66%2/3
A or B
A= $c->render_maybeB= $stash->{"mojo.routed"}
ABresult
1X1
011
000
188 }
189  
190 21 1;
191  
192 =encoding utf8
193  
194 =head1 NAME
195  
196 Mojolicious::Routes - Always find your destination with routes
197  
198 =head1 SYNOPSIS
199  
200 use Mojolicious::Routes;
201  
202 # Simple route
203 my $r = Mojolicious::Routes->new;
204 $r->any('/')->to(controller => 'blog', action => 'welcome');
205  
206 # More advanced routes
207 my $blog = $r->under('/blog');
208 $blog->get('/list')->to('blog#list');
209 $blog->get('/:id' => [id => qr/\d+/])->to('blog#show', id => 23);
210 $blog->patch(sub ($c) { $c->render(text => 'Go away!', status => 405) });
211  
212 =head1 DESCRIPTION
213  
214 L<Mojolicious::Routes> is the core of the L<Mojolicious> web framework.
215  
216 See L<Mojolicious::Guides::Routing> for more.
217  
218 =head1 TYPES
219  
220 These placeholder types are available by default.
221  
222 =head2 num
223  
224 $r->get('/article/<id:num>');
225  
226 Placeholder value needs to be a non-fractional number, similar to the regular expression C<([0-9]+)>.
227  
228 =head1 ATTRIBUTES
229  
230 L<Mojolicious::Routes> inherits all attributes from L<Mojolicious::Routes::Route> and implements the following new
231 ones.
232  
233 =head2 base_classes
234  
235 my $classes = $r->base_classes;
236 $r = $r->base_classes(['MyApp::Controller']);
237  
238 Base classes used to identify controllers, defaults to L<Mojolicious::Controller> and L<Mojolicious>.
239  
240 =head2 cache
241  
242 my $cache = $r->cache;
243 $r = $r->cache(Mojo::Cache->new);
244  
245 Routing cache, defaults to a L<Mojo::Cache> object.
246  
247 =head2 conditions
248  
249 my $conditions = $r->conditions;
250 $r = $r->conditions({foo => sub {...}});
251  
252 Contains all available conditions.
253  
254 =head2 namespaces
255  
256 my $namespaces = $r->namespaces;
257 $r = $r->namespaces(['MyApp::Controller', 'MyApp']);
258  
259 Namespaces to load controllers from.
260  
261 # Add another namespace to load controllers from
262 push @{$r->namespaces}, 'MyApp::MyController';
263  
264 =head2 shortcuts
265  
266 my $shortcuts = $r->shortcuts;
267 $r = $r->shortcuts({foo => sub {...}});
268  
269 Contains all available shortcuts.
270  
271 =head2 types
272  
273 my $types = $r->types;
274 $r = $r->types({lower => qr/[a-z]+/});
275  
276 Registered placeholder types, by default only L</"num"> is already defined.
277  
278 =head1 METHODS
279  
280 L<Mojolicious::Routes> inherits all methods from L<Mojolicious::Routes::Route> and implements the following new ones.
281  
282 =head2 add_condition
283  
284 $r = $r->add_condition(foo => sub ($route, $c, $captures, $arg) {...});
285  
286 Register a condition.
287  
288 $r->add_condition(foo => sub ($route, $c, $captures, $arg) {
289 ...
290 return 1;
291 });
292  
293 =head2 add_shortcut
294  
295 $r = $r->add_shortcut(foo => sub ($route, @args) {...});
296  
297 Register a shortcut.
298  
299 $r->add_shortcut(foo => sub ($route, @args) {...});
300  
301 =head2 add_type
302  
303 $r = $r->add_type(foo => qr/\w+/);
304 $r = $r->add_type(foo => ['bar', 'baz']);
305  
306 Register a placeholder type.
307  
308 $r->add_type(lower => qr/[a-z]+/);
309  
310 =head2 continue
311  
312 $r->continue(Mojolicious::Controller->new);
313  
314 Continue dispatch chain and emit the hook L<Mojolicious/"around_action"> for every action.
315  
316 =head2 dispatch
317  
318 my $bool = $r->dispatch(Mojolicious::Controller->new);
319  
320 Match routes with L</"match"> and dispatch with L</"continue">.
321  
322 =head2 lookup
323  
324 my $route = $r->lookup('foo');
325  
326 Find route by name with L<Mojolicious::Routes::Route/"find"> and cache all results for future lookups.
327  
328 =head2 match
329  
330 $r->match(Mojolicious::Controller->new);
331  
332 Match routes with L<Mojolicious::Routes::Match>.
333  
334 =head1 SEE ALSO
335  
336 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
337  
338 =cut
« Mojo/File.pm ↑ Summary Mojo/Parameters.pm »