ASP4/HandlerResolver.pm untested

filter: Statementstmt 0.0% 0 / 74 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 7 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 117 CC 11 SCAR 30.2
CC
11
Cov
0%
check_reload
42.0
resolve_request_handler
30.0
_forget_package
6.0
CRAP 20.5
1  
2 package ASP4::HandlerResolver;
3  
4 use strict;
5 use warnings 'all';
6 use ASP4::PageLoader;
7 use File::stat;
8 my %HandlerCache = ( );
9 my %FileTimes = ( );
10  
11  
12 sub new
13 {
14 my ($class, %args) = @_;
15
16 return bless \%args, $class;
17 }# end new()
18  
19  
20 sub context { ASP4::HTTPContext->current }
21  
22  
23 sub resolve_request_handler
24 {
25 my ($s, $uri) = @_;
26
27 ($uri) = split /\?/, $uri;
28 $s->check_reload( $uri );
29 return $HandlerCache{$uri} if $HandlerCache{$uri};
30
31 if( $uri =~ m/^\/handlers\// )
32 {
33 (my $handler = $uri) =~ s/^\/handlers\///;
34 $handler =~ s/[^a-z0-9_]/::/gi;
35 (my $path = "$handler.pm") =~ s/::/\//g;
36 my $filepath = $s->context->config->web->handler_root . "/$path";
37
38 if( -f $filepath )
39 {
40 $s->context->config->load_class( $handler );
41 return $HandlerCache{$uri} = $handler;
42 }
43 else
44 {
45 return;
46 }# end if()
47 }
48 else
49 {
50 my $info = ASP4::PageLoader->discover( script_name => $uri );
51 if( -f $info->{filename} )
52 {
53 my $page = ASP4::PageLoader->load( script_name => $uri );
54 return $HandlerCache{$uri} = $page->package;
55 }
56 else
57 {
58 return;
59 }# end if()
60 }# end if()
61 }# end resolve_request_handler()
62  
63  
64 sub check_reload
65 {
66 my ($s, $uri) = @_;
67  
68 if( $uri =~ m/^\/handlers\// )
69 {
70 (my $handler = $uri) =~ s/^\/handlers\///;
71 $handler =~ s/[^a-z0-9_]/::/gi;
72 (my $path = "$handler.pm") =~ s/::/\//g;
73 my $filepath = $s->context->config->web->handler_root . "/$path";
74 (my $inc_entry = "$handler.pm") =~ s/::/\//g;
75 return unless -f $filepath;
76
77 if( stat($filepath)->mtime > ($FileTimes{ $filepath } || 0) )
78 {
79 $FileTimes{ $filepath } = stat($filepath)->mtime;
80 $s->_forget_package(
81 $inc_entry, $handler
82 );
83 delete( $HandlerCache{$uri} );
84 }# end if()
85 }
86 else
87 {
88 my $info = ASP4::PageLoader->discover( script_name => $uri );
89 return unless -f $info->{saved_to};
90 $FileTimes{ $info->{filename} } ||= 0;
91 if( stat($info->{filename})->mtime > ( $FileTimes{ $info->{filename} } || stat($info->{saved_to})->mtime ) )
92 {
93 $s->_forget_package(
94 $info->{compiled_as}, $info->{package}
95 );
96 delete( $HandlerCache{$uri} );
97 }# end if()
98 }# end if()
99 }# end check_reload()
100  
101  
102 sub _forget_package
103 {
104 my ($s, $inc, $package) = @_;
105
106 # Forcibly forget all about the handler we are going to reload:
107 no strict 'refs';
108 delete( $INC{ $inc } );
109 if( *{"$package\::run"} )
110 {
111 no warnings;
112 *{"$package\::run"} = undef;
113 *{"$package\::before_run"} = undef;
114 *{"$package\::after_run"} = undef;
115 }# end if()
116 }# end _forget_package()
117  
118 1;# return true:
119