File Coverage

lib/Cache/Static/HTML_Mason_Util.pm
Criterion Covered Total %
statement 6 64 9.3
branch 0 30 0.0
condition 0 3 0.0
subroutine 2 3 66.6
pod 0 1 0.0
total 8 101 7.9


line stmt bran cond sub pod time code
1             ##
2             #
3             # Copyright 2005-2006, Brian Szymanski
4             #
5             # This file is part of Cache::Static
6             #
7             # Cache::Static is free software; you can redistribute it and/or modify
8             # it under the terms of the GNU General Public License as published by
9             # the Free Software Foundation; either version 2 of the License, or
10             # any later version.
11             #
12             # This program is distributed in the hope that it will be useful,
13             # but WITHOUT ANY WARRANTY; without even the implied warranty of
14             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15             # GNU General Public License for more details.
16             #
17             # For more information about Cache::Static, point a web browser at
18             # http://chronicle.allafrica.com/scache/ or read the
19             # documentation included with the Cache::Static distribution in the
20             # doc/ directory
21             #
22             ##
23              
24             package Cache::Static::HTML_Mason_Util;
25              
26 1     1   1708 use strict;
  1         1  
  1         58  
27 1     1   5 use warnings;
  1         3  
  1         2011  
28              
29             #TODO: store / pull this from Configuration.pm
30             eval { require Cache::Static::HTML_Mason_Util::hmc; };
31             my $no_hmc = $@;
32              
33             my $hmc = $no_hmc ? undef : Cache::Static::HTML_Mason_Util::hmc->new;
34              
35             sub cache_it {
36 0     0 0   my ($r, $m, $verbose, $deps) = @_;
37 0 0         $deps = [] unless($deps); #Mason sometimes translates this to zero...
38 0           my $args = $m->caller_args(0);
39              
40 0           Cache::Static::_log(4, "in cache_it");
41 0 0         unless($args->{_Cache_Static_final}) {
42 0           Cache::Static::_log(4, "in cache_it : unless");
43              
44 0           my $cc = $m->current_comp;
45 0 0         my $uri = ( $m->dhandler_arg ?
46             $cc->dir_path.'/'.$m->dhandler_arg :
47             $cc->dir_path.'/'.$cc->name );
48 0           $uri =~ s/\/\//\//g;
49              
50             #find the file we're using, and add a file dependency
51 0           my $file_dep;
52 0 0         if($hmc) {
53 0 0 0       if(($uri eq $r->uri) && !$m->dhandler_arg) {
    0          
54             #a plain vanilla non-dhandler top level page
55 0           $file_dep = $r->filename;
56             } elsif($uri eq $r->uri) {
57             #we have a dhandler, figure out which one...
58 0           $file_dep = $uri;
59 0           my $arg = $m->dhandler_arg;
60 0           $file_dep =~ s/$arg$//;
61 0           $file_dep = $r->document_root.$file_dep.$m->dhandler_name;
62             } else {
63             #a subcomponent
64 0           $file_dep = $r->document_root.$cc->dir_path.'/'.$cc->name;
65             }
66             }
67 0           my %deps;
68 0           foreach my $d (@$deps) {
69 0           $deps{$d} = 1;
70             }
71 0 0         if($hmc) {
72 0           my $spec = "file|$file_dep";
73 0           my $hmc_depstring = '';
74 0 0         unless($deps{$spec}) {
75 0           $hmc_depstring .= "$spec ";
76 0           $deps{$spec} = 1;
77             # Cache::Static::_log(3, "HTML_Mason_Util: added extra dep 1: $file_dep");
78             }
79             #add file deps on any components we detect
80 0           foreach my $i (@{$hmc->find_extra_deps($file_dep, r => $r, m => $m)}) {
  0            
81 0 0         unless($deps{$i}) {
82 0           $hmc_depstring .= "$i ";
83 0           $deps{$i} = 1;
84             }
85             }
86 0           $hmc_depstring =~ s/ $//;
87 0           Cache::Static::_log(3, "HTML_Mason_Util: added extra deps: $hmc_depstring");
88             }
89              
90             #extract dependencies to arrayref
91 0           my @t = keys %deps;
92 0           $deps = \@t;
93              
94 0           my $friendly_key;
95 0 0         $friendly_key = Cache::Static::make_friendly_key($uri,
96             $args) if($verbose);
97 0           my $key = Cache::Static::make_key($uri, $args);
98 0           my $ret = Cache::Static::get_if_same($key, $deps);
99 0 0         if(defined($ret)) {
100 0           Cache::Static::_log(4, "in cache_it : then");
101 0 0         if($verbose) {
102 0 0         if($verbose > 1) {
103 0           $m->out("

serving cached component for $friendly_key ($key)

\n")
104             } else {
105 0           $m->out("\n")
106             }
107             }
108             } else {
109 0           Cache::Static::_log(4, "in cache_it : else");
110 0 0         if($verbose) {
111 0 0         if($verbose > 1) {
112 0           $m->out("

(re)generating component for $friendly_key ($key)

\n")
113             } else {
114 0           $m->out("\n")
115             }
116             }
117 0           my %newargs = %$args;
118 0           $newargs{_Cache_Static_final} = 1;
119 0           $ret = $m->scomp( $m->current_comp->path, %newargs );
120 0           Cache::Static::set($key, $ret, $deps);
121             }
122 0           $m->out($ret);
123 0           return 1;
124             }
125 0           return 0;
126             }
127              
128             1;
129