blib/lib/Dancer/Plugin/Legacy/Routing.pm

filter: Statementstmt 96.4% 55 / 57 Branchbran 75.0% 9 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 93.3% 14 / 15 Podpod n/a totaltotal 92.8% 78 / 84 CC 7 SCAR 6.7
CC
7
Cov
93%
__ANON__
6.0
_log_request
2.1
__ANON__
2.0
CRAP 2.0
↑ Summary
1 package Dancer::Plugin::Legacy::Routing;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use Dancer qw(:syntax);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Dancer::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 our $VERSION = '0.0.4'; # VERSION
10 # ABSTRACT: Dancer Plugin for Deprecating Existing Routes
11  
12 =pod
13  
14 =encoding utf8
15  
16 =head1 NAME
17  
18 Dancer::Plugin::Legacy::Routing - Dancer Plugin for Deprecating Existing Routes
19  
20 =head1 SYNOPSIS
21  
22 package MyDancerApp;
23  
24 use strict;
25 use warnings;
26  
27 use Dancer;
28 use Dancer::Plugin::Legacy::Routing;
29  
30 get "/my/fancy/new/route" => \&still_good_controller;
31 legacy_get "/my/stinky/old/route" => \&still_good_controller;
32  
33 sub still_good_controller {
34 status 200;
35 return "Plugins Rocks!";
36 }
37  
38 =head1 DESCRIPTION
39  
40 Often times in refactoring and general software development of web applications a developer will
41 find themselves with the need to support all existing routes while at the same time building out
42 a better set of routes. The major problem here is to find all applications, templates, bookmarks,
43 and all other references to these old routes, that is not always possible.
44  
45 Enter Dancer::Plugin::Legacy::Routing, this plugin for Dancer allows you to clean up and improve
46 all of your routes while still maintaining all of your old ones in a clean and easy to see and
47 understand way. You can even optionally get log entries that will tell you how people are getting
48 to your old routes and how often they are being called.
49  
50 =head2 Configuration
51  
52 Dancer::Plugin::Legacy::Routing makes use of the standard Dancer environment based routing. Since
53 this is a plugin, be sure to include this is the correct place under plugins.
54  
55 =head3 Options
56  
57 =head4 log
58  
59 Currently "log" is the only option (this will likely be built out in future versions). Having this
60 set to a truthy value will result in messages being written (with the level of I<info>) to your dancer
61 log file.
62  
63 [30494] info @0.000631> [hit #2]Legacy Route GET '/legacy/get' referred from '(none)' in repos/Dancer-Plugin-Legacy-Routing/lib/Dancer/Plugin/Legacy/Routing.pm l. 32
64  
65  
66 =head3 YAML Example Configuration File
67  
68 logger: file
69 log: info
70 warnings: 1
71 show_errors: 1
72 auto_reload: 0
73  
74 plugins:
75 Legacy::Routing:
76 log: 1
77  
78 B<NOTE> The name of this plugin is Legacy::Routing, that is what you want to specify in your
79 environment's configuration file.
80  
81 =head1 METHODS
82  
83 The standard HTTP methods are available for you to legacy-ify, including any.
84  
85 =head2 legacy_get
86  
87 get "/good/get" => \&test_get;
88 legacy_get "/legacy/get" => \&test_get;
89  
90 get "/good/get/:var" => \&test_get_with_var;
91 legacy_get "/legacy/get/:var" => \&test_get_with_var;
92  
93 get "/good/get/:var/params" => \&test_get_with_params;
94 legacy_get "/legacy/get/:var/params" => \&test_get_with_params;
95  
96 =cut
97  
98 15 ▶register legacy_get => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
99 15 my $pattern = shift;
100 15 my $code = shift;
101  
102 15 my $conf = plugin_setting();
103  
104 5 ▶ my $hooked_code = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
105 5 ▶ $conf->{log} and _log_request();
Branch: 1 branch
BranchTrueFalseTotal
$conf->{"log"} and _log_request() 4 1 5
106 5 &$code();
107 15 };
108  
109 15 get $pattern, $hooked_code;
110 6 };
111  
112 =head2 legacy_post
113  
114 post "/good/post" => \&test_post;
115 legacy_post "/legacy/post" => \&test_post;
116  
117 post "/good/post/:var" => \&test_post_with_var;
118 legacy_post "/legacy/post/:var" => \&test_post_with_var;
119  
120 post "/good/post/:var/params" => \&test_post_with_params;
121 legacy_post "/legacy/post/:var/params" => \&test_post_with_params;
122  
123 =cut
124  
125 15 ▶register legacy_post => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
126 15 my $pattern = shift;
127 15 my $code = shift;
128  
129 15 my $conf = plugin_setting();
130  
131 5 ▶ my $hooked_code = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
132 5 ▶ $conf->{log} and _log_request();
Branch: 1 branch
BranchTrueFalseTotal
$conf->{"log"} and _log_request() 4 1 5
133 5 &$code();
134 15 };
135  
136 15 post $pattern, $hooked_code;
137 6 };
138  
139 =head2 legacy_put
140  
141 put "/good/put" => \&test_put;
142 legacy_put "/legacy/put" => \&test_put;
143  
144 put "/good/put/:var" => \&test_put_with_var;
145 legacy_put "/legacy/put/:var" => \&test_put_with_var;
146  
147 put "/good/put/:var/params" => \&test_put_with_params;
148 legacy_put "/legacy/put/:var/params" => \&test_put_with_params;
149  
150 =cut
151  
152 15 ▶register legacy_put => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
153 15 my $pattern = shift;
154 15 my $code = shift;
155  
156 15 my $conf = plugin_setting();
157  
158 5 ▶ my $hooked_code = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
159 5 ▶ $conf->{log} and _log_request();
Branch: 1 branch
BranchTrueFalseTotal
$conf->{"log"} and _log_request() 4 1 5
160 5 &$code();
161 15 };
162  
163 15 put $pattern, $hooked_code;
164 6 };
165  
166 =head2 legacy_del
167  
168 del "/good/delete" => \&test_del;
169 legacy_del "/legacy/delete" => \&test_del;
170  
171 del "/good/delete/:var" => \&test_del_with_var;
172 legacy_del "/legacy/delete/:var" => \&test_del_with_var;
173  
174 del "/good/delete/:var/params" => \&test_del_with_params;
175 legacy_del "/legacy/delete/:var/params" => \&test_del_with_params;
176  
177 =cut
178  
179 15 ▶register legacy_del => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
180 15 my $pattern = shift;
181 15 my $code = shift;
182  
183 15 my $conf = plugin_setting();
184  
185 5 ▶ my $hooked_code = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
186 5 ▶ $conf->{log} and _log_request();
Branch: 1 branch
BranchTrueFalseTotal
$conf->{"log"} and _log_request() 4 1 5
187 5 &$code();
188 15 };
189  
190 15 del $pattern, $hooked_code;
191 6 };
192  
193 =head2 legacy_any
194  
195 any "/good/any" => \&test_any;
196 legacy_any "/legacy/any" => \&test_any;
197  
198 =cut
199  
200 20 ▶register legacy_any => sub {
Subroutine __ANON__: called CC=1 SCAR=0.8
201 20 my $pattern = shift;
202 20 my $code = shift;
203  
204 20 my $conf = plugin_setting();
205  
206 0 ▶ my $hooked_code = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
207 0 ▶ $conf->{log} and _log_request();
Branch: 1 branch
BranchTrueFalseTotal
$conf->{"log"} and _log_request() 0 0 0
208 0 &$code();
209 20 };
210  
211 20 any $pattern, $hooked_code;
212 6 };
213  
214 16 ▶sub _log_request {
Subroutine _log_request: called CC=2 SCAR=7.2
215 16 ▶ info "Legacy Route "
Branch: 1 branch
BranchTrueFalseTotal
defined request()->referer ? : 0 16 16
216 . request->method . " '"
217 . request->path
218 . "' referred from '"
219 . ( defined request->referer ? request->referer : "(none)" ) . "'";
220  
221 16 return;
222 }
223  
224 6 register_plugin;
225 6 1;
226  
227 __END__
↑ Summary