line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::AutoParams; |
2
|
3
|
|
|
3
|
|
1962
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
19
|
|
3
|
3
|
|
|
3
|
|
472
|
use strict; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
58
|
|
4
|
3
|
|
|
3
|
|
18
|
use warnings; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
945
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub register { |
9
|
3
|
|
|
3
|
1
|
107
|
my ($self,$app,$config) = @_; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
$app->hook(around_action => sub { |
12
|
7
|
|
|
7
|
|
168272
|
my ($next, $c, $action, $last) = @_; |
13
|
7
|
|
|
|
|
146
|
my $endpoint = $c->match->endpoint; |
14
|
7
|
50
|
|
|
|
170
|
return $c->$action unless ref $endpoint eq 'Mojolicious::Routes::Route'; |
15
|
7
|
|
|
|
|
10
|
my @placeholders = @{ $endpoint->pattern->placeholders }; |
|
7
|
|
|
|
|
113
|
|
16
|
7
|
|
|
|
|
159
|
my @params; |
17
|
|
|
|
|
|
|
my %params; |
18
|
7
|
|
|
|
|
7
|
for my $level ( @{ $c->match->stack }) { |
|
7
|
|
|
|
|
116
|
|
19
|
7
|
100
|
|
|
|
173
|
my $placeholder = shift @placeholders or last; |
20
|
6
|
|
66
|
|
|
53
|
while ($placeholder and exists( $level->{$placeholder} ) ) { |
21
|
9
|
|
|
|
|
16
|
push @params, $level->{$placeholder}; |
22
|
9
|
|
|
|
|
20
|
$params{$placeholder} = $level->{$placeholder}; |
23
|
9
|
|
|
|
|
43
|
$placeholder = shift @placeholders; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
} |
26
|
7
|
100
|
|
|
|
35
|
return $c->$action(@params) unless $config->{named}; |
27
|
4
|
100
|
|
|
|
21
|
return $c->$action(%params) if $config->{named} eq '1'; |
28
|
2
|
50
|
|
|
|
6
|
if ($config->{named} eq '_') { |
29
|
2
|
|
|
|
|
4
|
local $_ = \%params; |
30
|
2
|
|
|
|
|
9
|
return $c->$action; |
31
|
|
|
|
|
|
|
} |
32
|
0
|
|
|
|
|
|
return $c->$action; |
33
|
3
|
|
|
|
|
31
|
}); |
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
|
|
|
|
|
|
|
|