line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::AutoParams; |
2
|
3
|
|
|
3
|
|
2126
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
19
|
|
3
|
3
|
|
|
3
|
|
527
|
use strict; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
58
|
|
4
|
3
|
|
|
3
|
|
24
|
use warnings; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
914
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub register { |
9
|
3
|
|
|
3
|
1
|
103
|
my ($self,$app,$config) = @_; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
$app->hook(around_action => sub { |
12
|
7
|
|
|
7
|
|
189766
|
my ($next, $c, $action, $last) = @_; |
13
|
7
|
|
|
|
|
173
|
my $endpoint = $c->match->endpoint; |
14
|
7
|
50
|
|
|
|
212
|
return $c->$action unless ref $endpoint eq 'Mojolicious::Routes::Route'; |
15
|
7
|
|
|
|
|
14
|
my @placeholders = @{ $endpoint->pattern->placeholders }; |
|
7
|
|
|
|
|
143
|
|
16
|
7
|
|
|
|
|
197
|
my @params; |
17
|
|
|
|
|
|
|
my %params; |
18
|
7
|
|
|
|
|
12
|
for my $level ( @{ $c->match->stack }) { |
|
7
|
|
|
|
|
127
|
|
19
|
7
|
100
|
|
|
|
206
|
my $placeholder = shift @placeholders or last; |
20
|
6
|
|
66
|
|
|
52
|
while ($placeholder and exists( $level->{$placeholder} ) ) { |
21
|
9
|
|
|
|
|
30
|
push @params, $level->{$placeholder}; |
22
|
9
|
|
|
|
|
26
|
$params{$placeholder} = $level->{$placeholder}; |
23
|
9
|
|
|
|
|
34
|
$placeholder = shift @placeholders; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
} |
26
|
7
|
100
|
|
|
|
41
|
return $c->$action(@params) unless $config->{named}; |
27
|
4
|
100
|
|
|
|
25
|
return $c->$action(%params) if $config->{named} eq '1'; |
28
|
2
|
50
|
|
|
|
14
|
if ($config->{named} eq '_') { |
29
|
2
|
|
|
|
|
3
|
local $_ = \%params; |
30
|
2
|
|
|
|
|
7
|
return $c->$action; |
31
|
|
|
|
|
|
|
} |
32
|
0
|
|
|
|
|
|
return $c->$action; |
33
|
3
|
|
|
|
|
24
|
}); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
1; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=encoding utf8 |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=head1 NAME |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
Mojolicious::Plugin::AutoParams - Send captured placeholder values as parameters for routes. |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=head1 SYNOPSIS |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
use Mojolicious::Lite; |
47
|
|
|
|
|
|
|
use experimental 'signatures'; |
48
|
|
|
|
|
|
|
plugin 'auto_params'; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
get '/hello/:name' => sub ($c,$name) { $c->render(text => "hi, $name") } |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
OR |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
use Mojolicious::Lite; |
55
|
|
|
|
|
|
|
plugin 'auto_params', { named => 1 } |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
get '/hello/:name' => sub { |
58
|
|
|
|
|
|
|
my $c = shift; |
59
|
|
|
|
|
|
|
my %args = @_; |
60
|
|
|
|
|
|
|
$c->render(text => "hi, $args{name}") |
61
|
|
|
|
|
|
|
}; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
OR |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
use Mojolicious::Lite; |
66
|
|
|
|
|
|
|
plugin 'auto_params', { named => '_' } |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
get '/hello/:name' => sub { shift->render(text => "hi, $_->{name}") } |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head1 DESCRIPTION |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
This module automatically sends placeholders as a list of parameters to routes. |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
By default it uses positional parameters, but it will optionally send a hash if |
75
|
|
|
|
|
|
|
the "named" option is set to 1. |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
Setting 'named' to '_' will set the local $_ to a hashref of the placeholders. |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head1 AUTHOR |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
Brian Duggan |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=cut |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
|