File Coverage

blib/lib/Dancer/Plugin/Legacy/Routing.pm
Criterion Covered Total %
statement 47 49 95.9
branch 9 12 75.0
condition n/a
subroutine 14 15 93.3
pod n/a
total 70 76 92.1


line stmt bran cond sub pod time code
1             package Dancer::Plugin::Legacy::Routing;
2              
3 5     5   2685252 use strict;
  5         17  
  5         214  
4 5     5   31 use warnings;
  5         11  
  5         169  
5              
6 5     5   26 use Dancer qw(:syntax);
  5         9  
  5         38  
7 5     5   8187 use Dancer::Plugin;
  5         8924  
  5         5478  
8              
9             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) 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 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             register legacy_get => sub {
99 15     15   27899 my $pattern = shift;
100 15         25 my $code = shift;
101              
102 15         44 my $conf = plugin_setting();
103              
104             my $hooked_code = sub {
105 5 100   5   55435 $conf->{log} and _log_request();
106 5         22 &$code();
107 15         486 };
108              
109 15         45 get $pattern, $hooked_code;
110             };
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             register legacy_post => sub {
126 15     15   8030 my $pattern = shift;
127 15         25 my $code = shift;
128              
129 15         103 my $conf = plugin_setting();
130              
131             my $hooked_code = sub {
132 5 100   5   86162 $conf->{log} and _log_request();
133 5         23 &$code();
134 15         481 };
135              
136 15         53 post $pattern, $hooked_code;
137             };
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             register legacy_put => sub {
153 15     15   6305 my $pattern = shift;
154 15         26 my $code = shift;
155              
156 15         40 my $conf = plugin_setting();
157              
158             my $hooked_code = sub {
159 5 100   5   59458 $conf->{log} and _log_request();
160 5         23 &$code();
161 15         459 };
162              
163 15         47 put $pattern, $hooked_code;
164             };
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             register legacy_del => sub {
180 15     15   5983 my $pattern = shift;
181 15         21 my $code = shift;
182              
183 15         39 my $conf = plugin_setting();
184              
185             my $hooked_code = sub {
186 5 100   5   44589 $conf->{log} and _log_request();
187 5         28 &$code();
188 15         435 };
189              
190 15         44 del $pattern, $hooked_code;
191             };
192              
193             =head2 legacy_any
194              
195             any "/good/any" => \&test_any;
196             legacy_any "/legacy/any" => \&test_any;
197              
198             =cut
199              
200             register legacy_any => sub {
201 20     20   48411 my $pattern = shift;
202 20         43 my $code = shift;
203              
204 20         59 my $conf = plugin_setting();
205              
206             my $hooked_code = sub {
207 0 0   0   0 $conf->{log} and _log_request();
208 0         0 &$code();
209 20         689 };
210              
211 20         75 any $pattern, $hooked_code;
212             };
213              
214             sub _log_request {
215 16 50   16   115 info "Legacy Route "
216             . request->method . " '"
217             . request->path
218             . "' referred from '"
219             . ( defined request->referer ? request->referer : "(none)" ) . "'";
220              
221 16         1529 return;
222             }
223              
224             register_plugin;
225             1;
226              
227             __END__