Conf/FileBased.pm

filter: Statementstmt 86.2% 144 / 167 Branchbran 67.5% 54 / 80 Conditioncond 67.7% 21 / 31 MC/DCmcdc 44.4% 8 / 18 Subroutinesub 94.4% 17 / 18 Podpod 60.0% 3 / 5 totaltotal 77.4% 247 / 319 CC 52 SCAR 22.7
CC
52
Cov
79%
_parse_line
20.3
_get_conf_root
13.3
local_conf_value
8.2
CRAP 9.7
↑ Summary Conf.pm »
1 package Rose::Conf::FileBased;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Carp();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Rose::Conf;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 require Tie::Hash;
10 1 our @ISA = qw(Rose::Conf Tie::StdHash);
11  
12 1 our($CONF_ROOT, %Refresh_Time, $APACHE_CONF_PATH);
13  
14 1 our $REFRESH_TIMEOUT = 60 * 15; # in seconds
15  
16 1 our $CONF_SUFFIX = '.conf';
17 1 our $LOCAL_CONF_FILE = 'local' . $CONF_SUFFIX;
18  
19 1 ▶use constant PRIVATE_PREFIX => '__' . __PACKAGE__ . '::';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 our $VERSION = '0.02';
22  
23 1 our $Debug = 0;
24  
25 sub import
26 2 ▶{
Subroutine import: called CC=2 SCAR=6.9
27 2 my($class) = shift;
28  
29 2 local $Rose::Conf::ExportLevel;
30 2 $Rose::Conf::ExportLevel++;
31  
32 2 ▶ $class->Rose::Conf::import(@_) if(@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 1 2
33  
34 2 my $conf = $class->conf_hash;
35  
36 2 my %save = %$conf;
37  
38 2 tie(%$conf, $class);
39  
40 2 %$conf = %save;
41 }
42  
43 sub refresh
44 2 ▶{
Subroutine refresh: called CC=6 SCAR=18.4
Pod documented
45 2 my($class) = shift;
46  
47 #return if(time < $Refresh_Time{$class} + $class->refresh_timeout);
48  
49 2 $class->_get_conf_root();
50  
51 2 ▶ if($CONF_ROOT && -d $CONF_ROOT)
Branch: 1 branch
BranchTrueFalseTotal
if ($Rose::Conf::FileBased::CONF_ROOT and -d $Rose::Conf::FileBased::CONF_ROOT) 1 1 2
Condition66%2/3
$Rose::Conf::FileBased::CONF_ROOT and -d $Rose::Conf::FileBased::CONF_ROOT
!ll&&!rl&&r
101
MC/DC50%1/2
$Rose::Conf::FileBased::CONF_ROOT and -d $Rose::Conf::FileBased::CONF_ROOT
$Rose::Conf::FileBased::CONF_ROOT -d $Rose::Conf::FileBased::CONF_ROOT
Truth table66%2/3
A and B
A= $Rose::Conf::FileBased::CONF_ROOTB= -d $Rose::Conf::FileBased::CONF_ROOT
ABresult
0X0
100
111
52 {
53 #$Refresh_Time{$class} ||= 0;
54  
55 # Local conf file
56 1 my $local_conf = File::Spec->catfile($CONF_ROOT, $LOCAL_CONF_FILE);
57  
58 1 ▶ if(-s $local_conf)
Branch: 1 branch
BranchTrueFalseTotal
if (-s $local_conf) 1 0 1
59 {
60 1 $class->_read_combined_conf($local_conf);
61 }
62  
63 # Package-specific conf file
64 1 my $class_conf = File::Spec->catfile($CONF_ROOT, $class . $CONF_SUFFIX);
65  
66 1 ▶ if(-s $class_conf)
Branch: 1 branch
BranchTrueFalseTotal
if (-s $class_conf) { } 0 1 1
67 {
68 0 $class->_read_class_conf($class_conf);
69 }
70 else
71 {
72 1 my $mod_class = $class;
73 1 $mod_class =~ s/::/-/g;
74  
75 1 $class_conf = $CONF_ROOT . '/' . $mod_class . $CONF_SUFFIX;
76  
77 1 ▶ if(-s $class_conf)
Branch: 1 branch
BranchTrueFalseTotal
if (-s $class_conf) 1 0 1
78 {
79 1 $class->_read_class_conf($class_conf);
80 }
81 }
82 }
83 }
84  
85 sub FETCH
86 101 ▶{
Subroutine FETCH: called CC=6 SCAR=17.9
87 101 my($hash, $key) = @_;
88  
89 101 ▶ unless(exists $hash->{PRIVATE_PREFIX . 'IMPORTED'} ||
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $hash->{"__Rose::Conf::FileBased::IMPORTED"} or exists $hash->{"__Rose::Conf::FileBased::IMPORTING"}) 1 100 101
Condition100%3/3
exists $hash->{"__Rose::Conf::FileBased::IMPORTED"} or exists $hash->{"__Rose::Conf::FileBased::IMPORTING"}
l!l&&r!l&&!r
9911
MC/DC100%2/2
exists $hash->{"__Rose::Conf::FileBased::IMPORTED"} or exists $hash->{"__Rose::Conf::FileBased::IMPORTING"}
exists $hash->{"__Rose::Conf::FileBased::IMPORTED"} exists $hash->{"__Rose::Conf::FileBased::IMPORTING"}
Truth table100%3/3
A or B
A= exists $hash->{"__Rose::Conf::FileBased::IMPORTED"}B= exists $hash->{"__Rose::Conf::FileBased::IMPORTING"}
ABresult
1X1
011
000
90 exists $hash->{PRIVATE_PREFIX . 'IMPORTING'})
91 {
92 1 ▶ $Debug && warn "FETCH $hash { $key }\n";
Branch: 1 branch
BranchTrueFalseTotal
$Rose::Conf::FileBased::Debug and warn "FETCH $hash { $key }\n" 0 1 1
93  
94 1 $hash->{PRIVATE_PREFIX . 'IMPORTING'} = 1;
95  
96 1 my $class = ref($hash);
97  
98 1 $class->refresh();
99  
100 1 delete $hash->{PRIVATE_PREFIX . 'IMPORTING'};
101  
102 1 $hash->{PRIVATE_PREFIX . 'IMPORTED'}++;
103 }
104  
105 ##
106 ## This is broken for now...
107 ##
108  
109 # Do not try refresh when looking up private keys
110 # unless(index($key, PRIVATE_PREFIX) == 0)
111 # {
112 # my $class = ref $hash;
113 #
114 # if(my $timeout = $class->refresh_timeout)
115 # {
116 # $Refresh_Time{$class} ||= 0;
117 #
118 # if(time > $Refresh_Time{$class} + $timeout)
119 # {
120 # $class->refresh;
121 # }
122 # }
123 # }
124  
125 Carp::croak "No such conf parameter: '$key'\n"
126 101 ▶ unless(index($key, PRIVATE_PREFIX) == 0 || exists $hash->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless index($key, "__Rose::Conf::FileBased::") == 0 or exists $hash->{$key} 2 99 101
Condition100%3/3
index($key, "__Rose::Conf::FileBased::") == 0 or exists $hash->{$key}
l!l&&r!l&&!r
27722
MC/DC100%2/2
index($key, "__Rose::Conf::FileBased::") == 0 or exists $hash->{$key}
index($key, "__Rose::Conf::FileBased::") == 0 exists $hash->{$key}
Truth table100%3/3
A or B
A= index($key, "__Rose::Conf::FileBased::") == 0B= exists $hash->{$key}
ABresult
1X1
011
000
127  
128 99 return $hash->{$key};
129 }
130  
131 sub _get_conf_root
132 2 ▶{
Subroutine _get_conf_root: called CC=6 SCAR=25.9
133 2 $CONF_ROOT = $ENV{'ROSE_CONF_FILE_ROOT'};
134  
135 2 ▶ if(!$CONF_ROOT && exists $ENV{'MOD_PERL'} && require mod_perl && $mod_perl::VERSION < 1.99)
Branch: 1 branch
BranchTrueFalseTotal
if (not $Rose::Conf::FileBased::CONF_ROOT and exists $ENV{"MOD_PERL"} and require mod_perl and $mod_perl::VERSION < "1.99") 0 2 2
Condition44%4/9
not $Rose::Conf::FileBased::CONF_ROOT and exists $ENV{"MOD_PERL"} and require mod_perl and $mod_perl::VERSION < "1.99"
!ll&&!rl&&r
200
not $Rose::Conf::FileBased::CONF_ROOT and exists $ENV{"MOD_PERL"} and require mod_perl
!ll&&!rl&&r
200
not $Rose::Conf::FileBased::CONF_ROOT and exists $ENV{"MOD_PERL"}
!ll&&!rl&&r
110
MC/DC0%0/4
not $Rose::Conf::FileBased::CONF_ROOT and exists $ENV{"MOD_PERL"} and require mod_perl and $mod_perl::VERSION < "1.99"
not $Rose::Conf::FileBased::CONF_ROOT exists $ENV{"MOD_PERL"} require mod_perl $mod_perl::VERSION < "1.99"
Truth table40%2/5
not(A) and B and C and D
A= not $Rose::Conf::FileBased::CONF_ROOTB= exists $ENV{"MOD_PERL"}C= require mod_perlD= $mod_perl::VERSION < "1.99"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
136 {
137 0 $CONF_ROOT = Apache->server_root_relative($APACHE_CONF_PATH);
138 0 ▶ $CONF_ROOT = undef unless(-d $CONF_ROOT);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $Rose::Conf::FileBased::CONF_ROOT 0 0 0
139 }
140 }
141  
142 sub refresh_timeout
143 0 ▶{
Subroutine refresh_timeout: not called CC=3 SCAR=20.3
Pod undocumented
144 0 my($class) = shift;
145  
146 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
147  
148 0 ▶ if(@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
149 {
150 0 return ${$class . '::REFRESH_TIMEOUT'} = shift;
151 }
152  
153 0 my $timeout = ${$class . '::REFRESH_TIMEOUT'};
154  
155 0 ▶ $timeout = $REFRESH_TIMEOUT unless(defined $timeout);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeout 0 0 0
156  
157 0 return $timeout;
158 }
159  
160 sub _parse_line
161 21 ▶{
Subroutine _parse_line: called CC=17 SCAR=30.1
162 21 my($class, $conf, $line, $file, $line_num) = @_;
163  
164 21 ▶ return unless($line =~ /\S/);
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\S/ 5 16 21
165  
166 16 my($key, $val);
167  
168 16 ▶ if($line =~ /^((?:[^\\ \t]+|\\.)+)\s*=\s*(\S.*|$)/)
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^((?:[^\\ \t]+|\\.)+)\s*=\s*(\S.*|$)/) { } 14 2 16
elsif (not $line =~ /^(#|$)/) { } 0 2 2
169 {
170 14 $key = $1;
171 14 $val = $2;
172 }
173 elsif($line !~ /^(#|$)/)
174 {
175 0 die "Syntax error in $file on line $line_num: $line\n";
176 }
177 2 else { return }
178  
179 14 ▶ if(length($key) && length($val))
Branch: 1 branch
BranchTrueFalseTotal
if (length $key and length $val) { } 14 0 14
Condition33%1/3
length $key and length $val
!ll&&!rl&&r
0014
MC/DC0%0/2
length $key and length $val
length $key length $val
Truth table33%1/3
A and B
A= length $keyB= length $val
ABresult
0X0
100
111
180 {
181 14 ▶ if($val =~ s/(['"])(.*)\1$/$2/)
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ s/(['"])(.*)\1$/$2/) 8 6 14
182 {
183 8 ▶ if($1 eq '"' && index($val, '\\') >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq "\"" and index($val, "\\") != -1) 1 7 8
Condition100%3/3
$1 eq "\"" and index($val, "\\") != -1
!ll&&!rl&&r
611
MC/DC100%2/2
$1 eq "\"" and index($val, "\\") != -1
$1 eq "\"" index($val, "\\") != -1
Truth table100%3/3
A and B
A= $1 eq "\""B= index($val, "\\") != -1
ABresult
0X0
100
111
184 {
185 1 $val = eval qq("$val");
186  
187 1 ▶ if($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
188 {
189 0 die qq(Invalid value "$val" in $file on line $line_num: $@\n);
190 }
191 }
192 }
193  
194 # Hash sub-key access
195 14 ▶ if($key =~ m/^(?:[^\\: \t]+|\\.)+:/)
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^(?:[^\\: \t]+|\\.)+:/) { } 4 10 14
196 {
197 4 my $original_key = $key;
198  
199 4 ▶ if($key =~ /^(?:[^\\: \t]+|\\.)+:$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^(?:[^\\: \t]+|\\.)+:$/) 0 4 4
200 {
201 0 Carp::croak qq($class - Invalid hash sub-key access: "$key", ),
202 qq(missing key name after final ':' in $file line $line_num);
203 }
204  
205 4 my @parts;
206 4 my $param = $conf;
207 4 my $prev_param;
208  
209 4 ▶ while($key =~ m/\G((?:[^\\: \t]+|\\.)+)(?::|$)/g)
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /\G((?:[^\\: \t]+|\\.)+)(?::|$)/g) 13 4 17
210 {
211 13 $prev_param = $param;
212 13 ▶ $param = $param->{$1} ||= {};
Condition100%2/2
$param->{$1} ||= {}
l!l
112
MC/DC100%1/1
$param->{$1} ||= {}
$param->{$1}
Truth table100%2/2
A
A= $param->{$1}
Aresult
11
00
213 13 push(@parts, $1);
214 13 $parts[-1] =~ s{\\(.)}{$1}g;
215 }
216
217 4 ▶ $Debug && warn "\$${class}::CONF{", join('}{', @parts), "} = $val\n";
Branch: 1 branch
BranchTrueFalseTotal
$Rose::Conf::FileBased::Debug and warn "\$${class}::CONF{", join("}{", @parts), "} = $val\n" 0 4 4
218  
219 4 $prev_param->{$parts[-1]} = $val;
220 4 $key = $original_key;
221 }
222 else
223 {
224 10 $key =~ s{\\(.)}{$1}g;
225 10 ▶ $Debug && warn "\$${class}::CONF{$key} = $val\n";
Branch: 1 branch
BranchTrueFalseTotal
$Rose::Conf::FileBased::Debug and warn "\$${class}::CONF{$key} = $val\n" 0 10 10
226 10 $conf->{$key} = $val;
227 10 $key =~ s{:}{\\:}g;
228 }
229 }
230 else
231 {
232 0 ▶ $Debug && warn "\$${class}::CONF{$key} = undef\n";
Branch: 1 branch
BranchTrueFalseTotal
$Rose::Conf::FileBased::Debug and warn "\$${class}::CONF{$key} = undef\n" 0 0 0
233 0 $conf->{$key} = undef;
234 0 $key =~ s{:}{\\:}g;
235 }
236  
237 14 $conf->{PRIVATE_PREFIX . 'MODIFIED'}{$key} =
238 {
239 file => $file,
240 line_number => $line_num,
241 #time => time(),
242 };
243 }
244  
245 sub _read_class_conf
246 1 ▶{
Subroutine _read_class_conf: called CC=3 SCAR=11.3
247 1 my($class, $file) = @_;
248  
249 1 ▶ unless(open(CONF, $file))
Branch: 1 branch
BranchTrueFalseTotal
unless (open CONF, $file) 0 1 1
250 {
251 0 warn "Could not open $file: $!";
252 0 return;
253 }
254  
255 #$Refresh_Time{$class} = time;
256  
257 1 my $conf = $class->conf_hash;
258  
259 1 ▶ while(<CONF>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline CONF)) 9 1 10
260 {
261 9 s/^\s+//;
262 9 s/\s+$//;
263  
264 9 $class->_parse_line($conf, $_, $file, $.);
265 }
266  
267 1 close(CONF);
268 }
269  
270 sub _read_combined_conf
271 1 ▶{
Subroutine _read_combined_conf: called CC=7 SCAR=19.9
272 1 my($class, $file) = @_;
273  
274 1 my $conf_fh;
275  
276 1 ▶ unless(open($conf_fh, $file))
Branch: 1 branch
BranchTrueFalseTotal
unless (open $conf_fh, $file) 0 1 1
277 {
278 0 warn "Could not open $file: $!";
279 0 return;
280 }
281  
282 #$Refresh_Time{$class} = time;
283  
284 1 my $conf = $class->conf_hash;
285  
286 1 my $in_domain = 0;
287  
288 1 my $in_domain_re = qr(^CLASS\s+$class$);
289 1 my $out_domain_re = qr(^CLASS\s*(?!=));
290  
291 1 ▶ while(<$conf_fh>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $conf_fh)) 17 1 18
292 {
293 17 s/^\s+//;
294 17 s/\s+$//;
295  
296 17 ▶ if(/$in_domain_re/)
Branch: 2 branches
BranchTrueFalseTotal
if (/$in_domain_re/) { } 1 16 17
elsif ($in_domain and /$out_domain_re/) { } 0 20 20
297 {
298 1 $in_domain = 1;
299 1 next;
300 }
301 ▶ elsif($in_domain && /$out_domain_re/)
Condition66%2/3
$in_domain and /$out_domain_re/
!ll&&!rl&&r
4120
MC/DC0%0/2
$in_domain and /$out_domain_re/
$in_domain /$out_domain_re/
Truth table66%2/3
A and B
A= $in_domainB= /$out_domain_re/
ABresult
0X0
100
111
302 {
303 0 $in_domain = 0;
304 }
305  
306 16 ▶ next unless($in_domain);
Branch: 1 branch
BranchTrueFalseTotal
unless $in_domain 4 12 16
307  
308 12 $class->_parse_line($conf, $_, $file, $.);
309 }
310  
311 1 close($conf_fh);
312 }
313  
314 sub local_conf_keys
315 1 ▶{
Subroutine local_conf_keys: called CC=1 SCAR=0.0
Pod documented
316 1 my($class) = shift;
317  
318 1 my $conf = $class->conf_hash;
319  
320 1 return keys(%{$conf->{PRIVATE_PREFIX . 'MODIFIED'}});
321 }
322  
323 sub local_conf_setting
324 6 ▶{
Subroutine local_conf_setting: called CC=3 SCAR=11.0
Pod undocumented
325 6 my($class, $key) = @_;
326  
327 6 ▶ Carp::croak "Cannot get setting without $key" unless(defined($key));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 6 6
328  
329 6 my $conf = $class->conf_hash;
330  
331 6 ▶ return unless($conf->{PRIVATE_PREFIX . 'MODIFIED'}{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless $conf->{"__Rose::Conf::FileBased::MODIFIED"}{$key} 1 5 6
332  
333 5 return bless($conf->{PRIVATE_PREFIX . 'MODIFIED'}{$key}, 'Rose::Conf::File::Setting');
334 }
335  
336 sub local_conf_value
337 6 ▶{
Subroutine local_conf_value: called CC=8 SCAR=21.1
Pod documented
338 6 my($class, $key) = @_;
339  
340 6 ▶ Carp::croak "Cannot get setting without $key" unless(defined($key));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 6 6
341  
342 6 ▶ $class->local_conf_setting($key) || return undef;
Branch: 1 branch
BranchTrueFalseTotal
$class->local_conf_setting($key) or return undef 5 1 6
343  
344 5 ▶ if($key =~ m/^(?:[^\\: \t]+|\\.)+:/)
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^(?:[^\\: \t]+|\\.)+:/) 2 3 5
345 {
346 2 ▶ if($key =~ /^(?:[^\\: \t]+|\\.)+:$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^(?:[^\\: \t]+|\\.)+:$/) 0 2 2
347 {
348 0 Carp::croak qq($class - Invalid hash sub-key access: "$key" - missing key name after final ':');
349 }
350  
351 2 my @parts;
352 2 my $param = $class->conf_hash;
353 2 my $prev_param;
354  
355 2 ▶ while($key =~ m/\G((?:[^\\: \t]+|\\.)+)(?::|$)/g)
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /\G((?:[^\\: \t]+|\\.)+)(?::|$)/g) 7 2 9
356 {
357 7 $prev_param = $param;
358 7 ▶ $param = $param->{$1} ||= {};
Condition50%1/2
$param->{$1} ||= {}
l!l
70
MC/DC0%0/1
$param->{$1} ||= {}
$param->{$1}
Truth table50%1/2
A
A= $param->{$1}
Aresult
11
00
359 7 push(@parts, $1);
360 7 $parts[-1] =~ s{\\(.)}{$1}g;
361 }
362
363 2 ▶ $Debug && warn "Get local conf value for \$${class}::CONF{", join('}{', @parts), "}\n";
Branch: 1 branch
BranchTrueFalseTotal
$Rose::Conf::FileBased::Debug and warn "Get local conf value for \$${class}::CONF{", join("}{", @parts), "}\n" 0 2 2
364  
365 2 return $prev_param->{$parts[-1]};
366 }
367  
368 3 $key =~ s{\\:}{:}g;
369 3 return $class->param($key);
370 }
371  
372 BEGIN
373 1 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
374 1 $APACHE_CONF_PATH = 'conf/perl';
375  
376 #_get_conf_root();
377  
378 1 require Rose::Object;
379 1 @Rose::Conf::File::Setting::ISA = qw(Rose::Object);
380 }
381  
382 1 1;
383  
384 # =item B<refresh_timeout SECS>
385 #
386 # If an argument is provided, sets the class's refresh timeout to SECS
387 # seconds. Returns the class's current refresh timeout in seconds.
388 #
389 # The refresh timeout is used to determine when (if ever) to refresh the
390 # configuration hash by re-reading the (possibly changed) configuration
391 # file(s). The configuration file(s) will only be re-read if they have
392 # been modified since they were last read (as determined by the files'
393 # "mtime").
394 #
395 # If the refresh timeout is zero, the files are never re-read. The
396 # default refresh timeout is 15 minutes.
397  
398 __END__
↑ Summary Conf.pm »