line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Dancer2::Plugin::Routing; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
358160
|
use 5.10.0; |
|
2
|
|
|
|
|
5
|
|
4
|
2
|
|
|
2
|
|
6
|
use strict; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
29
|
|
5
|
2
|
|
|
2
|
|
6
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
34
|
|
6
|
2
|
|
|
2
|
|
956
|
use Dancer2::Plugin; |
|
2
|
|
|
|
|
62519
|
|
|
2
|
|
|
|
|
12
|
|
7
|
2
|
|
|
2
|
|
16270
|
use Ref::Util qw/is_hashref/; |
|
2
|
|
|
|
|
1066
|
|
|
2
|
|
|
|
|
118
|
|
8
|
2
|
|
|
2
|
|
10
|
use base qw/Dancer2::Plugin::RootURIFor/; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
849
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 NAME |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Dancer2::Plugin::Routing - A dancer2 plugin for configurable routing. |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 VERSION |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
Version 0.01 |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=cut |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
my ( $_settings, $_routings, $_packages ) = undef; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# ----------------------------------------------- |
25
|
|
|
|
|
|
|
# Preloaded methods go here. |
26
|
|
|
|
|
|
|
# ----------------------------------------------- |
27
|
|
|
|
|
|
|
# Encapsulated class data. |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
{ |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub _load_settings { |
32
|
5
|
100
|
|
5
|
|
17
|
return $_settings if $_settings; |
33
|
1
|
|
|
|
|
4
|
$_settings = plugin_setting; |
34
|
1
|
|
|
|
|
392
|
return $_settings; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub _routings { |
38
|
4
|
100
|
|
4
|
|
18
|
return $_routings if $_routings; |
39
|
1
|
|
|
|
|
2
|
my $stg = _load_settings->{routes}; |
40
|
1
|
100
|
66
|
|
|
17
|
$_routings->{$_} = is_hashref $stg->{$_} && $stg->{$_}->{route} ? $stg->{$_}->{route} : $stg->{$_} foreach keys %$stg; |
41
|
1
|
|
|
|
|
3
|
return $_routings; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub _packages { |
45
|
2
|
100
|
|
2
|
|
4579
|
return $_packages if $_packages; |
46
|
1
|
|
|
|
|
4
|
my $stg = _load_settings->{routes}; |
47
|
1
|
|
|
|
|
4
|
foreach ( keys %$stg ) { |
48
|
3
|
100
|
66
|
|
|
12
|
next if ( !is_hashref $stg->{$_} || !$stg->{$_}->{package} ); |
49
|
2
|
|
|
|
|
4
|
$_packages->{$_} = $stg->{$_}->{package}; |
50
|
|
|
|
|
|
|
} |
51
|
1
|
|
|
|
|
2
|
return $_packages; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub _routing_for { |
55
|
2
|
100
|
|
2
|
|
9437
|
return undef unless $_[1]; |
56
|
1
|
|
|
|
|
4
|
return _routings->{ $_[1] }; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub _package_for { |
60
|
2
|
100
|
|
2
|
|
8097
|
return undef unless $_[1]; |
61
|
1
|
|
|
|
|
3
|
my $packages = _packages(); |
62
|
1
|
50
|
|
|
|
7
|
return $packages->{ $_[1] } if $packages; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub _redirect { |
66
|
5
|
|
|
5
|
|
30234
|
my $dsl = shift; |
67
|
5
|
|
|
|
|
35
|
return $dsl->app->redirect( $dsl->SUPER::root_uri_for(@_) ); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=head1 SUBROUTINES/METHODS |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=head2 routing_for |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
Get route from a given route key |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=cut |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
register routing_for => \&_routing_for; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=head2 package_for |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
Get package name from a given route key |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=cut |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
register package_for => \&_package_for; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head2 routings |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
Returns a list of all configured routes to mount |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=cut |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
register routings => \&_routings; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head2 packages |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
Returns a list of all configured packages to mount |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=cut |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
register packages => \&_packages; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head2 root_redirect |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
overloaded sub to redirect to root uri from |
108
|
|
|
|
|
|
|
C |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=cut |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
register root_redirect => \&_redirect; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
on_plugin_import { |
115
|
|
|
|
|
|
|
$_[0]->app->add_hook( |
116
|
|
|
|
|
|
|
Dancer2::Core::Hook->new( |
117
|
|
|
|
|
|
|
name => 'before_template', |
118
|
|
|
|
|
|
|
code => sub { |
119
|
|
|
|
|
|
|
my $key = _load_settings()->{template_key}; |
120
|
|
|
|
|
|
|
$_[0]->{$key} = \%{ _routings() }; |
121
|
|
|
|
|
|
|
$_ =~ s~^/$~~ig foreach ( values %{ $_[0]->{$key} } ); |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
) |
124
|
|
|
|
|
|
|
); |
125
|
|
|
|
|
|
|
}; |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
register_plugin for_versions => [2]; |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
1; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
__END__ |