Muto.pm

filter: Statementstmt 33.7% 62 / 184 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 59.3% 19 / 32 Podpod 75.0% 6 / 8 totaltotal 25.5% 87 / 340 CC 48 SCAR 41.6
CC
48
Cov
22%
_check_protected_methods
182.0
_match_type
110.0
_fetch_controller_method
90.0
CRAP 64.4
↑ Summary Muto/MethodAttributes.pm »
1 package CGI::Application::Muto;
2  
3 2 ▶use base 'CGI::Application';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 $VERSION = '0.02';
10  
11 # Load our recommended plugins
12 2 ▶use CGI::Application::Plugin::DBH qw/dbh_config dbh/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use CGI::Application::Plugin::ConfigAuto qw/cfg_file cfg/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use CGI::Application::Plugin::Redirect;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use CGI::Application::Plugin::LogDispatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use CGI::Application::Plugin::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 # Load our magic maker plugins
20 2 ▶use Class::Inspector;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use CGI::Application::Muto::MethodAttributes;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25  
26 # We overload the CGI::App run method to provide some extra functionality
27 # of our own
28 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
Pod documented
29  
30 0 my $self = shift;
31  
32 0 $self->_init_controllers();
33 0 $self->_init_controller_methods();
34  
35 #Some callbacks
36 0 $self->add_callback('prerun', \&_fetch_controller_method);
37 0 $self->add_callback('prerun', \&_check_protected_methods);
38  
39 0 return $self->SUPER::run(); #call CGI::App run method
40  
41 }
42  
43  
44 0 ▶sub _init_controllers{
Subroutine _init_controllers: not called CC=3 SCAR=18.7
45  
46 0 my $self = shift;
47  
48 # @controller_paths define the search path where we want to search for
49 # plugins
50 0 our @controller_paths = ('Muto::App::Controller');
51 0 ▶ push @controller_paths, $self->param('controller_path') if $self->param('controller_path');
Branch: 1 branch
BranchTrueFalseTotal
if $self->param("controller_path") 0 0 0
52  
53 # Using Module::Pluggable we fetch the list of all available plugins
54 # and have them available by the '_contr' method in our namespace
55 2 ▶ use Module::Pluggable search_path => \@controller_paths,
Subroutine BEGIN: called CC=1 SCAR=0.0
56 2 sub_name => '_contr';
57  
58 # We iterate through the plugin list and using Module::Load we
59 # attempt to load them
60 0 for my $Controller( $self->_contr ){
61 0 load $Controller;
62 }
63  
64 0 return;
65 }
66  
67  
68 # We are gonna provide a really cool way of making our methods work similar to
69 # the ones on Catalyst
70 # This code has been taken almost entirely from the module
71 # CGI::Application::Plugin::ActionDispatch by Jason Yates, E<lt>jaywhy@gmail.comE<gt>
72 # so all credit goes to him on this aspect of the code
73 2 our %_attr_cache;
74 2 my %methods;
75 2 my $init_attr_handlers = 1;
76  
77 0 ▶sub CGI::Application::Muto::Path :ATTR {
Subroutine Path: not called CC=3 SCAR=24.8
Pod documented
78 0 my ($class, $referent, $attr, $data) = @_;
79  
80 0 ▶ $data ||='';
Condition0%0/2
$data ||= ""
l!l
00
MC/DC0%0/1
$data ||= ""
$data
Truth table0%0/2
A
A= $data
Aresult
11
00
81 0 $data =~ s/\/$//;
82 0 ▶ unless( $data =~ /^\// ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data =~ m[^/]) 0 0 0
83 0 $data = "/" . $data;
84 }
85  
86 0 my $regex = qr/^$data\/?(\/.*)?$/;
87 0 push(@{ $_attr_cache{$class}{$attr} }, [ $referent, $regex ]);
88 2 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
89  
90 0 ▶sub CGI::Application::Muto::Regex :ATTR {
Subroutine Regex: not called CC=1 SCAR=6.9
Pod documented
91 0 my ($package, $referent, $attr, $data) = @_;
92 0 my $regex = qr/$data/;
93 0 push(@{ $_attr_cache{$package}{$attr} }, [$referent, $regex ]);
94 2 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
95  
96 0 ▶sub CGI::Application::Muto::Runmode :ATTR {
Subroutine Runmode: not called CC=1 SCAR=6.9
Pod documented
97 0 my ($package, $referent, $attr, $data) = @_;
98  
99 0 $data = $methods{$referent};
100 0 my $regex = qr/^\/$data\/?$/;
101 0 push(@{ $_attr_cache{$package}{$attr} }, [ $referent, $regex ]);
102 2 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
103  
104 0 ▶sub CGI::Application::Muto::Default :ATTR {
Subroutine Default: not called CC=1 SCAR=6.9
Pod documented
105 0 my ($package, $referent, $attr, $data) = @_;
106 0 $_attr_cache{$package}{$attr} = $referent;
107 2 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
108  
109 0 ▶sub CGI::Application::Muto::ErrorRunmode :ATTR {
Subroutine ErrorRunmode: not called CC=1 SCAR=6.9
Pod undocumented
110 0 my ($package, $referent, $attr, $data) = @_;
111 0 $_attr_cache{$package}{$attr} = $referent;
112 2 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
113  
114 # We register the methods into CGI::App's run mode map
115 0 ▶sub _init_controller_methods {
Subroutine _init_controller_methods: not called CC=6 SCAR=37.4
116  
117 0 my $self = shift;
118 0 ▶ my $class = ref $self || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
119  
120 # Setup a hash table of all the methods in the class.
121 $methods{$self->can($_)} = $_
122 0 ▶ foreach @{ Class::Inspector->methods($class) || [] }; #NOTE: This will search through ISA also.
Condition0%0/2
'Class::Inspector'->methods($class) || []
l!l
00
MC/DC0%0/1
'Class::Inspector'->methods($class) || []
'Class::Inspector'->methods($class)
Truth table0%0/2
A
A= 'Class::Inspector'->methods($class)
Aresult
11
00
123  
124 0 CGI::Application::Muto::MethodAttributes::init();
125  
126 0 ▶ if(defined $_attr_cache{$class}{'Default'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $CGI::Application::Muto::_attr_cache{$class}{"Default"}) 0 0 0
127 0 my $runmode = $methods{$_attr_cache{$class}{'Default'}};
128 0 $self->start_mode($runmode);
129 0 $self->run_modes($runmode => $runmode);
130 }
131  
132 0 ▶ if(defined $_attr_cache{$class}{'ErrorRunmode'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $CGI::Application::Muto::_attr_cache{$class}{"ErrorRunmode"}) 0 0 0
133 0 $self->error_mode($methods{$_attr_cache{$class}{'ErrorRunmode'}});
134 }
135  
136 }
137  
138 # Based on the 'path_env' variable we try to identify the correct
139 # method to execute
140 0 ▶sub _fetch_controller_method {
Subroutine _fetch_controller_method: not called CC=9 SCAR=45.0
141  
142 0 my $self = shift;
143 0 ▶ my $class = ref $self || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
144  
145 0 ▶ my $_TEST_PATH_ON = $self->param('path_env') || 'PATH_INFO';
Condition0%0/2
$self->param('path_env') || 'PATH_INFO'
l!l
00
MC/DC0%0/1
$self->param('path_env') || 'PATH_INFO'
$self->param('path_env')
Truth table0%0/2
A
A= $self->param('path_env')
Aresult
11
00
146 0 ▶ my $_PATH_PREFIX = $self->param('path_prefix') || '';
Condition0%0/2
$self->param('path_prefix') || ''
l!l
00
MC/DC0%0/1
$self->param('path_prefix') || ''
$self->param('path_prefix')
Truth table0%0/2
A
A= $self->param('path_prefix')
Aresult
11
00
147  
148 0 ▶ return unless defined $ENV{$_TEST_PATH_ON};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ENV{$_TEST_PATH_ON} 0 0 0
149  
150 0 my $_PATH_INFO = $ENV{$_TEST_PATH_ON};
151  
152 0 $_PATH_INFO =~ s/\?.*$//;
153  
154 0 ▶ if( $_PATH_PREFIX ){
Branch: 1 branch
BranchTrueFalseTotal
if ($_PATH_PREFIX) 0 0 0
155 0 $_PATH_INFO =~ s/^$_PATH_PREFIX//;
156 }
157  
158 0 my $start_mode = $self->start_mode();
159 0 ATTR: foreach my $type (qw( Runmode Regex Path )) {
160 0 my($code, @args) = _match_type($class, $type, $_PATH_INFO);
161 0 ▶ if($code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) 0 0 0
162 # Make sure the runmode isn't set already and prerun_mode isn't set.
163 0 ▶ if(! $self->prerun_mode()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->prerun_mode) 0 0 0
164 # Sorta of a hack here to actually get the runmode to run.
165 0 my $runmode = $methods{$code};
166 0 $self->run_modes($runmode => $runmode);
167 0 $self->prerun_mode($runmode);
168  
169 # Set the action_args array.
170 0 $self->action_args(@args);
171 }
172  
173 0 last ATTR;
174 }
175 }
176  
177 }
178  
179 # This little function tries to match the method on the $path_info
180 # if several methods mathc, then it uses the one that matches
181 # the closest
182 0 ▶sub _match_type {
Subroutine _match_type: not called CC=10 SCAR=47.0
183  
184 0 my($class, $type, $path_info) = @_;
185  
186 0 my $min;
187 0 my(@path_args, $code);
188 0 foreach my $attr (@{ $_attr_cache{$class}{$type} }) {
189 0 ▶ if(my @args = ($path_info =~ $attr->[1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@args) = $path_info =~ /$attr->[1]/) 0 0 0
190 # We want to match the most accurate Path(). This is
191 # done by counting the args, and finding the Path with
192 # the fewest amount of args left over.
193 0 ▶ if($type eq 'Path') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'Path') { } 0 0 0
194 0 ▶ if(@args && $args[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args and $args[0]) 0 0 0
Condition0%0/3
@args and $args[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@args and $args[0]
@args $args[0]
Truth table0%0/3
A and B
A= @argsB= $args[0]
ABresult
0X0
100
111
195 0 $args[0] =~ s/^\///;
196 0 @path_args = split('/', $args[0]);
197 }
198  
199 # Set min if not defined.
200 0 ▶ $min = scalar(@path_args) if( not defined $min );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $min 0 0 0
201  
202 # If complete match return.
203 0 ▶ if( scalar(@path_args) == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @path_args == 0) { } 0 0 0
elsif (scalar @path_args <= $min) { } 0 0 0
204 0 return ($attr->[0], undef);
205 } elsif(scalar(@path_args) <= $min) {
206 # Has fewest @path_args so far.
207 0 $min = scalar(@path_args);
208 0 $code = $attr->[0];
209 }
210 } else {
211 0 return ($attr->[0], @args);
212 }
213 }
214 }
215 0 ▶ return @path_args ? ($code, @path_args) : 0;
Branch: 1 branch
BranchTrueFalseTotal
@path_args ? : 0 0 0
216  
217 }
218  
219  
220 0 ▶sub action_args {
Subroutine action_args: not called CC=4 SCAR=30.0
Pod documented
221 0 my($self, @args) = @_;
222  
223 # If args are passed set them.
224 0 ▶ if(@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 0 0 0
225 0 $self->{__CAP_ACTION_ARGS} = [ @args ];
226 0 return;
227 }
228  
229 0 ▶ return undef unless defined $self->{__CAP_ACTION_ARGS};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"__CAP_ACTION_ARGS"} 0 0 0
230 0 ▶ return wantarray ? @{$self->{__CAP_ACTION_ARGS}} : shift @{$self->{__CAP_ACTION_ARGS}};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
231 }
232  
233  
234  
235  
236 # Add run mode protection
237 0 ▶sub protect_rm{
Subroutine protect_rm: not called CC=7 SCAR=40.3
Pod undocumented
238  
239 0 my $self = shift;
240 0 my %args = @_;
241  
242 0 ▶ if( !$args{'Regex'} && !$args{'Path'} ){
Branch: 3 branches
BranchTrueFalseTotal
if (not $args{'Regex'} || $args{'Path'}) { } 0 0 0
elsif (not $args{'auth_check'} && ref $args{'auth_check'} or ref $args{'auth_check'} ne 'CODE') { } 0 0 0
elsif (not $args{'login_page'}) { } 0 0 0
Condition0%0/3
$args{'Regex'} || $args{'Path'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'Regex'} || $args{'Path'}
$args{'Regex'} $args{'Path'}
Truth table0%0/3
A || B
A= $args{'Regex'}B= $args{'Path'}
ABresult
1X1
011
000
243 0 return;
244 }
245 ▶ elsif(!$args{'auth_check'}
Condition0%0/6
not $args{"auth_check"} && ref $args{"auth_check"} or ref $args{"auth_check"} ne "CODE"
l!l&&r!l&&!r
000
$args{'auth_check'} && ref $args{'auth_check'}
!ll&&!rl&&r
000
MC/DC0%0/3
not $args{"auth_check"} && ref $args{"auth_check"} or ref $args{"auth_check"} ne "CODE"
$args{'auth_check'} ref $args{'auth_check'} ref $args{"auth_check"} ne "CODE"
Truth table0%0/4
not(A && B) or C
A= $args{'auth_check'}B= ref $args{'auth_check'}C= ref $args{"auth_check"} ne "CODE"
ABCresult
0XX1
10X1
1111
1100
246 || !ref($args{'auth_check'})
247 || ref($args{'auth_check'}) ne 'CODE' ){
248 0 return;
249 }
250 elsif( !$args{'login_page'} ){
251 0 return;
252 }
253  
254 #Register the new protected rm
255 0 push @{$self->{'_PROTECTED_RM'}}, \%args;
256  
257 0 return 1;
258  
259 }
260  
261  
262 # This function checks if the path that is about to be executed
263 # is protected
264 0 ▶sub _check_protected_methods{
Subroutine _check_protected_methods: not called CC=13 SCAR=52.0
265  
266 0 my $self = shift;
267  
268 0 ▶ my $_TEST_PATH_ON = $self->param('path_env') || 'PATH_INFO';
Condition0%0/2
$self->param('path_env') || 'PATH_INFO'
l!l
00
MC/DC0%0/1
$self->param('path_env') || 'PATH_INFO'
$self->param('path_env')
Truth table0%0/2
A
A= $self->param('path_env')
Aresult
11
00
269 0 ▶ my $_PATH_PREFIX = $self->param('path_prefix') || '';
Condition0%0/2
$self->param('path_prefix') || ''
l!l
00
MC/DC0%0/1
$self->param('path_prefix') || ''
$self->param('path_prefix')
Truth table0%0/2
A
A= $self->param('path_prefix')
Aresult
11
00
270  
271 0 ▶ return unless defined $ENV{$_TEST_PATH_ON};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ENV{$_TEST_PATH_ON} 0 0 0
272  
273 0 my $_PATH_INFO = $ENV{$_TEST_PATH_ON};
274  
275 0 $_PATH_INFO =~ s/\?.*$//;
276  
277 0 ▶ if( $_PATH_PREFIX ){
Branch: 1 branch
BranchTrueFalseTotal
if ($_PATH_PREFIX) 0 0 0
278 0 $_PATH_INFO =~ s/^$_PATH_PREFIX//;
279 }
280  
281 0 $_PATH_INFO =~ s/^\///;
282  
283 0 my $_IS_PROTECTED = 0;
284 0 my $_PRM;
285  
286 0 for my $prm( @{$self->{'_PROTECTED_RM'}} ){
287  
288 0 ▶ if( $prm->{'Path'} && $prm->{'Path'} eq $_PATH_INFO ){
Branch: 2 branches
BranchTrueFalseTotal
if ($prm->{'Path'} and $prm->{'Path'} eq $_PATH_INFO) { } 0 0 0
elsif ($prm->{'Regex'} and $_PATH_INFO =~ /$prm->{'Regex'}/) { } 0 0 0
Condition0%0/3
$prm->{"Path"} and $prm->{"Path"} eq $_PATH_INFO
!ll&&!rl&&r
000
MC/DC0%0/2
$prm->{"Path"} and $prm->{"Path"} eq $_PATH_INFO
$prm->{"Path"} $prm->{"Path"} eq $_PATH_INFO
Truth table0%0/3
A and B
A= $prm->{"Path"}B= $prm->{"Path"} eq $_PATH_INFO
ABresult
0X0
100
111
289 0 $_IS_PROTECTED = 1;
290 0 $_PRM = $prm;
291 0 last;
292 }
293 ▶ elsif( $prm->{'Regex'} && $_PATH_INFO =~ $prm->{'Regex'} ){
Condition0%0/3
$prm->{"Regex"} and $_PATH_INFO =~ /$prm->{"Regex"}/
!ll&&!rl&&r
000
MC/DC0%0/2
$prm->{"Regex"} and $_PATH_INFO =~ /$prm->{"Regex"}/
$prm->{"Regex"} $_PATH_INFO =~ /$prm->{"Regex"}/
Truth table0%0/3
A and B
A= $prm->{"Regex"}B= $_PATH_INFO =~ /$prm->{"Regex"}/
ABresult
0X0
100
111
294 0 $_IS_PROTECTED = 1;
295 0 $_PRM = $prm;
296 0 last;
297 }
298  
299 }
300  
301  
302 #we are on a protected method
303 0 ▶ if( $_IS_PROTECTED && $_PRM ){
Branch: 1 branch
BranchTrueFalseTotal
if ($_IS_PROTECTED and $_PRM) { } 0 0 0
Condition0%0/3
$_IS_PROTECTED and $_PRM
!ll&&!rl&&r
000
MC/DC0%0/2
$_IS_PROTECTED and $_PRM
$_IS_PROTECTED $_PRM
Truth table0%0/3
A and B
A= $_IS_PROTECTEDB= $_PRM
ABresult
0X0
100
111
304 0 ▶ if( $_PRM->{'auth_check'}->($self) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($_PRM->{'auth_check'}->($self)) { } 0 0 0
305 0 return 1;
306 }
307 else{
308 0 return $self->redirect($_PRM->{'login_page'});
309 }
310 }
311 else{
312 0 return 1;
313 }
314  
315 }
316  
317 2 1;
318 __END__
↑ Summary Muto/MethodAttributes.pm »