File Coverage

blib/lib/ASP4/HandlerResolver.pm
Criterion Covered Total %
statement 63 67 94.0
branch 15 20 75.0
condition 4 7 57.1
subroutine 11 11 100.0
pod 0 4 0.0
total 93 109 85.3


line stmt bran cond sub pod time code
1              
2             package ASP4::HandlerResolver;
3              
4 6     6   28 use strict;
  6         11  
  6         266  
5 6     6   34 use warnings 'all';
  6         9  
  6         365  
6 6     6   2559 use ASP4::PageLoader;
  6         15  
  6         191  
7 6     6   39 use File::stat;
  6         9  
  6         20  
8             my %HandlerCache = ( );
9             my %FileTimes = ( );
10              
11              
12             sub new
13             {
14 5130     5130 0 5985 my ($class, %args) = @_;
15            
16 5130         14358 return bless \%args, $class;
17             }# end new()
18              
19              
20 2007     2007 0 3688 sub context { ASP4::HTTPContext->current }
21              
22              
23             sub resolve_request_handler
24             {
25 5130     5130 0 4242 my ($s, $uri) = @_;
26            
27 5130         9247 ($uri) = split /\?/, $uri;
28 5130         7946 $s->check_reload( $uri );
29 5130 100       775004 return $HandlerCache{$uri} if $HandlerCache{$uri};
30            
31 21 100       101 if( $uri =~ m/^\/handlers\// )
32             {
33 3         12 (my $handler = $uri) =~ s/^\/handlers\///;
34 3         13 $handler =~ s/[^a-z0-9_]/::/gi;
35 3         20 (my $path = "$handler.pm") =~ s/::/\//g;
36 3         8 my $filepath = $s->context->config->web->handler_root . "/$path";
37            
38 3 50       57 if( -f $filepath )
39             {
40 3         8 $s->context->config->load_class( $handler );
41 3         45 return $HandlerCache{$uri} = $handler;
42             }
43             else
44             {
45 0         0 return;
46             }# end if()
47             }
48             else
49             {
50 18         79 my $info = ASP4::PageLoader->discover( script_name => $uri );
51 18 50       359 if( -f $info->{filename} )
52             {
53 18         126 my $page = ASP4::PageLoader->load( script_name => $uri );
54 18         187 return $HandlerCache{$uri} = $page->package;
55             }
56             else
57             {
58 0         0 return;
59             }# end if()
60             }# end if()
61             }# end resolve_request_handler()
62              
63              
64             sub check_reload
65             {
66 5130     5130 0 4144 my ($s, $uri) = @_;
67              
68 5130 100       9909 if( $uri =~ m/^\/handlers\// )
69             {
70 2001         4345 (my $handler = $uri) =~ s/^\/handlers\///;
71 2001         5211 $handler =~ s/[^a-z0-9_]/::/gi;
72 2001         4774 (my $path = "$handler.pm") =~ s/::/\//g;
73 2001         3059 my $filepath = $s->context->config->web->handler_root . "/$path";
74 2001         4410 (my $inc_entry = "$handler.pm") =~ s/::/\//g;
75 2001 50       29600 return unless -f $filepath;
76            
77 2001 100 100     4219 if( stat($filepath)->mtime > ($FileTimes{ $filepath } || 0) )
78             {
79 3         542 $FileTimes{ $filepath } = stat($filepath)->mtime;
80 3         316 $s->_forget_package(
81             $inc_entry, $handler
82             );
83 3         5 delete( $HandlerCache{$uri} );
84             }# end if()
85             }
86             else
87             {
88 3129         8853 my $info = ASP4::PageLoader->discover( script_name => $uri );
89 3129 100       32859 return unless -f $info->{saved_to};
90 3117   50     12249 $FileTimes{ $info->{filename} } ||= 0;
91 3117 50 33     6082 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 0         0 );
96 0         0 delete( $HandlerCache{$uri} );
97             }# end if()
98             }# end if()
99             }# end check_reload()
100              
101              
102             sub _forget_package
103             {
104 3     3   7 my ($s, $inc, $package) = @_;
105            
106             # Forcibly forget all about the handler we are going to reload:
107 6     6   4030 no strict 'refs';
  6         8  
  6         386  
108 3         7 delete( $INC{ $inc } );
109 3 50       5 if( *{"$package\::run"} )
  3         26  
110             {
111 6     6   26 no warnings;
  6         11  
  6         583  
112 3         5 *{"$package\::run"} = undef;
  3         9  
113 3         4 *{"$package\::before_run"} = undef;
  3         11  
114 3         6 *{"$package\::after_run"} = undef;
  3         17  
115             }# end if()
116             }# end _forget_package()
117              
118             1;# return true:
119