| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Dancer::Route::Registry; |
|
2
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:SUKRIA'; |
|
3
|
|
|
|
|
|
|
# ABSTRACT: Route registry for Dancer |
|
4
|
|
|
|
|
|
|
$Dancer::Route::Registry::VERSION = '1.3521'; |
|
5
|
170
|
|
|
170
|
|
1159
|
use strict; |
|
|
170
|
|
|
|
|
368
|
|
|
|
170
|
|
|
|
|
5077
|
|
|
6
|
170
|
|
|
170
|
|
894
|
use warnings; |
|
|
170
|
|
|
|
|
381
|
|
|
|
170
|
|
|
|
|
3993
|
|
|
7
|
170
|
|
|
170
|
|
958
|
use Carp; |
|
|
170
|
|
|
|
|
367
|
|
|
|
170
|
|
|
|
|
9175
|
|
|
8
|
170
|
|
|
170
|
|
76444
|
use Dancer::Route; |
|
|
170
|
|
|
|
|
661
|
|
|
|
170
|
|
|
|
|
7257
|
|
|
9
|
170
|
|
|
170
|
|
1232
|
use base 'Dancer::Object'; |
|
|
170
|
|
|
|
|
503
|
|
|
|
170
|
|
|
|
|
19182
|
|
|
10
|
170
|
|
|
170
|
|
1417
|
use Dancer::Logger; |
|
|
170
|
|
|
|
|
504
|
|
|
|
170
|
|
|
|
|
5115
|
|
|
11
|
170
|
|
|
170
|
|
1194
|
use Dancer::Exception qw(:all); |
|
|
170
|
|
|
|
|
534
|
|
|
|
170
|
|
|
|
|
164160
|
|
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Dancer::Route::Registry->attributes(qw( id )); |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my $id = 1; |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub init { |
|
18
|
114
|
|
|
114
|
1
|
438
|
my ($self) = @_; |
|
19
|
|
|
|
|
|
|
|
|
20
|
114
|
50
|
|
|
|
725
|
unless (defined $self->{id}) { |
|
21
|
114
|
|
|
|
|
853
|
$self->id($id++); |
|
22
|
|
|
|
|
|
|
} |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# Routes are stored here. Keys are route methods, and values are |
|
25
|
|
|
|
|
|
|
# arrays of routes. Routes with options are stored in the |
|
26
|
|
|
|
|
|
|
# beginning of the array while routes without options are stored |
|
27
|
|
|
|
|
|
|
# at the end. |
|
28
|
114
|
|
|
|
|
418
|
$self->{routes} = {}; |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# Keep track of the border between routes with and without |
|
31
|
|
|
|
|
|
|
# options, so that routes with options can be easily inserted into |
|
32
|
|
|
|
|
|
|
# its routes array. |
|
33
|
114
|
|
|
|
|
332
|
$self->{routes_border} = {}; |
|
34
|
|
|
|
|
|
|
|
|
35
|
114
|
|
|
|
|
286
|
return $self; |
|
36
|
|
|
|
|
|
|
} |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub is_empty { |
|
39
|
2
|
|
|
2
|
0
|
4
|
my ($self) = @_; |
|
40
|
2
|
|
|
|
|
5
|
for my $method ( keys %{ $self->routes } ) { |
|
|
2
|
|
|
|
|
5
|
|
|
41
|
1
|
50
|
|
|
|
4
|
return 0 if $self->routes($method); |
|
42
|
|
|
|
|
|
|
} |
|
43
|
1
|
|
|
|
|
7
|
return 1; |
|
44
|
|
|
|
|
|
|
} |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# replace any ':foo' by '(.+)' and stores all the named |
|
47
|
|
|
|
|
|
|
# matches defined in $REG->{route_params}{$route} |
|
48
|
|
|
|
|
|
|
sub routes { |
|
49
|
3467
|
|
|
3467
|
0
|
5972
|
my ($self, $method) = @_; |
|
50
|
|
|
|
|
|
|
|
|
51
|
3467
|
100
|
|
|
|
6064
|
if ($method) { |
|
52
|
3465
|
|
|
|
|
5651
|
my $route = $self->{routes}{$method}; |
|
53
|
3465
|
100
|
|
|
|
10505
|
return $route ? $route : []; |
|
54
|
|
|
|
|
|
|
} |
|
55
|
|
|
|
|
|
|
else { |
|
56
|
2
|
|
|
|
|
9
|
return $self->{routes}; |
|
57
|
|
|
|
|
|
|
} |
|
58
|
|
|
|
|
|
|
} |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub add_route { |
|
61
|
1438
|
|
|
1438
|
0
|
2709
|
my ($self, $route) = @_; |
|
62
|
1438
|
|
100
|
|
|
3355
|
$self->{routes}{$route->method} ||= []; |
|
63
|
1438
|
|
|
|
|
2364
|
my @registered = @{$self->routes($route->method)}; |
|
|
1438
|
|
|
|
|
2976
|
|
|
64
|
1438
|
|
|
|
|
2545
|
my $last = $registered[-1]; |
|
65
|
1438
|
100
|
|
|
|
4656
|
$route->set_previous($last) if defined $last; |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# Routes are stored in the order they are declared. However, |
|
68
|
|
|
|
|
|
|
# routes with options (such as ajax routes) are stored before |
|
69
|
|
|
|
|
|
|
# routes without options. This way, we can have the following |
|
70
|
|
|
|
|
|
|
# routes: |
|
71
|
|
|
|
|
|
|
# |
|
72
|
|
|
|
|
|
|
# get '/' => sub {}; |
|
73
|
|
|
|
|
|
|
# get qr{.*} => sub {}; |
|
74
|
|
|
|
|
|
|
# ajax '/' => sub {}; |
|
75
|
|
|
|
|
|
|
# ajax qr{.*} => sub {}; |
|
76
|
|
|
|
|
|
|
# |
|
77
|
|
|
|
|
|
|
# And the user won't have to declare the ajax routes before the |
|
78
|
|
|
|
|
|
|
# get routes. |
|
79
|
1438
|
100
|
|
|
|
2103
|
if (keys %{$route->{options}}) { |
|
|
1438
|
|
|
|
|
3374
|
|
|
80
|
11
|
|
|
|
|
22
|
splice @{$self->routes($route->method)}, |
|
81
|
11
|
|
|
|
|
17
|
$self->{routes_border}{$route->method}++, |
|
82
|
|
|
|
|
|
|
0, |
|
83
|
|
|
|
|
|
|
$route; |
|
84
|
|
|
|
|
|
|
} |
|
85
|
|
|
|
|
|
|
else { |
|
86
|
1427
|
|
|
|
|
2166
|
push @{$self->routes($route->method)}, $route; |
|
|
1427
|
|
|
|
|
3013
|
|
|
87
|
|
|
|
|
|
|
} |
|
88
|
|
|
|
|
|
|
|
|
89
|
1438
|
|
|
|
|
7840
|
return $route; |
|
90
|
|
|
|
|
|
|
} |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
# sugar for add_route |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub register_route { |
|
95
|
1439
|
|
|
1439
|
0
|
3853
|
my ($self, %args) = @_; |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# look if the caller (where the route is declared) exists as a Dancer::App |
|
98
|
|
|
|
|
|
|
# object |
|
99
|
1439
|
|
|
|
|
7751
|
my ($package) = caller(2); |
|
100
|
1439
|
100
|
100
|
|
|
6165
|
if ($package && Dancer::App->app_exists($package)) { |
|
101
|
651
|
|
|
|
|
1660
|
my $app = Dancer::App->get($package); |
|
102
|
651
|
|
|
|
|
1761
|
my $route = Dancer::Route->new(prefix => $app->prefix, %args); |
|
103
|
650
|
|
|
|
|
1702
|
return $app->registry->add_route($route); |
|
104
|
|
|
|
|
|
|
} |
|
105
|
|
|
|
|
|
|
else { |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
# FIXME maybe this code is useless, drop it later if so |
|
108
|
788
|
|
|
|
|
2711
|
my $route = Dancer::Route->new(%args); |
|
109
|
788
|
|
|
|
|
1810
|
return $self->add_route($route); |
|
110
|
|
|
|
|
|
|
} |
|
111
|
|
|
|
|
|
|
} |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# sugar for Dancer.pm |
|
114
|
|
|
|
|
|
|
# class, any, ARRAY(0x9864818), '/path', CODE(0x990ac88) |
|
115
|
|
|
|
|
|
|
# or |
|
116
|
|
|
|
|
|
|
# class, any, '/path', CODE(0x990ac88) |
|
117
|
|
|
|
|
|
|
sub any_add { |
|
118
|
24
|
|
|
24
|
0
|
106
|
my ($self, $pattern, @rest) = @_; |
|
119
|
|
|
|
|
|
|
|
|
120
|
24
|
|
|
|
|
200
|
my @methods = qw(get post put patch delete options head); |
|
121
|
|
|
|
|
|
|
|
|
122
|
24
|
100
|
|
|
|
186
|
if (ref($pattern) eq 'ARRAY') { |
|
123
|
19
|
|
|
|
|
81
|
@methods = @$pattern; |
|
124
|
|
|
|
|
|
|
# 'get' defaults to 'get' and 'head' |
|
125
|
38
|
|
|
|
|
191
|
push @methods, 'head' if ((grep { $_ eq 'get' } @methods) and |
|
126
|
19
|
100
|
66
|
|
|
72
|
not (grep { $_ eq 'head' } @methods)); |
|
|
4
|
|
|
|
|
16
|
|
|
127
|
19
|
|
|
|
|
70
|
$pattern = shift @rest; |
|
128
|
|
|
|
|
|
|
} |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
raise core_route => "Syntax error, methods should be provided as an ARRAY ref" |
|
131
|
24
|
100
|
|
|
|
105
|
if grep {$_ eq $pattern} @methods; |
|
|
75
|
|
|
|
|
220
|
|
|
132
|
|
|
|
|
|
|
|
|
133
|
23
|
|
|
|
|
136
|
$self->universal_add($_, $pattern, @rest) for @methods; |
|
134
|
23
|
|
|
|
|
109
|
return scalar(@methods); |
|
135
|
|
|
|
|
|
|
} |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub universal_add { |
|
138
|
1439
|
|
|
1439
|
0
|
3523
|
my ($self, $method, $pattern, @rest) = @_; |
|
139
|
|
|
|
|
|
|
|
|
140
|
1439
|
|
|
|
|
2324
|
my %options; |
|
141
|
|
|
|
|
|
|
my $code; |
|
142
|
|
|
|
|
|
|
|
|
143
|
1439
|
100
|
|
|
|
3287
|
if (@rest == 1) { |
|
144
|
1427
|
|
|
|
|
2270
|
$code = $rest[0]; |
|
145
|
|
|
|
|
|
|
} |
|
146
|
|
|
|
|
|
|
else { |
|
147
|
12
|
|
|
|
|
19
|
%options = %{$rest[0]}; |
|
|
12
|
|
|
|
|
41
|
|
|
148
|
12
|
|
|
|
|
25
|
$code = $rest[1]; |
|
149
|
|
|
|
|
|
|
} |
|
150
|
|
|
|
|
|
|
|
|
151
|
1439
|
|
|
|
|
4818
|
my %route_args = ( |
|
152
|
|
|
|
|
|
|
method => $method, |
|
153
|
|
|
|
|
|
|
code => $code, |
|
154
|
|
|
|
|
|
|
options => \%options, |
|
155
|
|
|
|
|
|
|
pattern => $pattern, |
|
156
|
|
|
|
|
|
|
); |
|
157
|
|
|
|
|
|
|
|
|
158
|
1439
|
|
|
|
|
4440
|
return $self->register_route(%route_args); |
|
159
|
|
|
|
|
|
|
} |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
# look for a route in the given array |
|
162
|
|
|
|
|
|
|
sub find_route { |
|
163
|
0
|
|
|
0
|
0
|
|
my ($self, $r, $reg) = @_; |
|
164
|
0
|
|
|
|
|
|
foreach my $route (@$reg) { |
|
165
|
0
|
0
|
|
|
|
|
return $route if $r->equals($route); |
|
166
|
|
|
|
|
|
|
} |
|
167
|
0
|
|
|
|
|
|
return; |
|
168
|
|
|
|
|
|
|
} |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
1; |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
__END__ |