blib/lib/WebDAO/Config.pm

filter: Statementstmt 90.6% 116 / 128 Branchbran 80.0% 48 / 60 Conditioncond 60.0% 12 / 20 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 93.3% 14 / 15 Podpod 16.6% 1 / 6 totaltotal 80.3% 192 / 239 CC 39 SCAR 24.9
CC
39
Cov
85%
convert_ini2hash
17.4
parse_line
17.0
get_full_path
12.0
CRAP 12.0
« blib/lib/WebDAO/Element.pm ↑ Summary blib/lib/WebDAO/CV.pm »
1 package WebDAO::Config;
2  
3 =head1 NAME
4  
5 WebDAO::Config - Configuration file class.
6  
7 =head1 SYNOPSIS
8  
9 use WebDAO::Config;
10 my $conf = new WebDAO::Config:: ( $opt{config} );
11 my $value = $conf->general->{db_name};
12  
13  
14 =head1 DESCRIPTION
15  
16 Configuration file class
17  
18 =head3 Format of INI-FILE
19  
20 Data is organized in sections. Each key/value pair is delimited with an
21 equal (=) sign. Sections are declared on their own lines enclosed in
22 '[' and ']':
23  
24 [BLOCK1]
25 KEY1 ?=VALUE1
26 KEY2 +=VALUE2
27  
28  
29 [BLOCK2]
30 KEY1=VALUE1
31 KEY2=VALUE2
32  
33 #%INCLUDE file.inc%
34  
35 =item B<?=> - set value unless it defined before
36  
37 =item B<+=> - add value
38  
39 =item B<=> - set value to key
40  
41 =item B<#%INCLUDE file.inc%> - include config ini file
42  
43 =cut
44  
45 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use WebDAO::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use base 'WebDAO::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use vars qw($AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 our $VERSION = '0.4';
52 1 our $PERL_SINGLE_QUOTE;
53  
54 1 __PACKAGE__->mk_attr ( __conf=>undef, _path=>undef );
55  
56 sub parse_line {
57 22 ▶ my($delimiter, $keep, $line) = @_;
Subroutine parse_line: called CC=16 SCAR=28.3
Pod undocumented
58 22 my($word, @pieces);
59  
60 1 ▶ no warnings 'uninitialized'; # we will be testing undef strings
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 22 ▶ while (length($line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $line) 28 22 50
63 # This pattern is optimised to be stack conservative on older perls.
64 # Do not refactor without being careful and testing it on very long strings.
65 # See Perl bug #42980 for an example of a stack busting input.
66 28 ▶ $line =~ s/^
Branch: 1 branch
BranchTrueFalseTotal
$line =~ s/^ (?: # double quoted string (") # $quote ((?>[^\\"]*(?:\\.[^\\"]*)*))" # $quoted | # --OR-- # singe quoted string (') # $quote ((?>[^\\']*(?:\\.[^\\']*)*))' # $quoted | # --OR-- # unquoted string ( # $unquoted (?:\\.|[^\\"'])*? ) # followed by ( # $delim \Z(?!\n) # EOL | # --OR-- (?-x:$delimiter) # delimiter | # --OR-- (?!^)(?=["']) # a quote ) )//sx or return 0 28 28
67 (?:
68 # double quoted string
69 (") # $quote
70 ((?>[^\\"]*(?:\\.[^\\"]*)*))" # $quoted
71 | # --OR--
72 # singe quoted string
73 (') # $quote
74 ((?>[^\\']*(?:\\.[^\\']*)*))' # $quoted
75 | # --OR--
76 # unquoted string
77 ( # $unquoted
78 (?:\\.|[^\\"'])*?
79 )
80 # followed by
81 ( # $delim
82 \Z(?!\n) # EOL
83 | # --OR--
84 (?-x:$delimiter) # delimiter
85 | # --OR--
86 (?!^)(?=["']) # a quote
87 )
88 )//xs or return; # extended layout
89 28 ▶ my ($quote, $quoted, $unquoted, $delim) = (($1 ? ($1,$2) : ($3,$4)), $5, $6);
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 14 14 28
90  
91  
92 28 ▶ return() unless( defined($quote) || length($unquoted) || length($delim));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $quote or length $unquoted or length $delim 0 28 28
Condition83%5/6
defined $quote or length $unquoted or length $delim
l!l&&r!l&&!r
2620
defined $quote or length $unquoted
l!l&&r!l&&!r
16102
MC/DC0%0/3
defined $quote or length $unquoted or length $delim
defined $quote length $unquoted length $delim
Truth table75%3/4
A or B or C
A= defined $quoteB= length $unquotedC= length $delim
ABCresult
1XX1
01X1
0011
0000
93  
94 28 ▶ if ($keep) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keep) { } 0 28 28
95 0 $quoted = "$quote$quoted$quote";
96 }
97 else {
98 28 $unquoted =~ s/\\(.)/$1/sg;
99 28 ▶ if (defined $quote) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $quote) 16 12 28
100 16 ▶ $quoted =~ s/\\(.)/$1/sg if ($quote eq '"');
Branch: 1 branch
BranchTrueFalseTotal
if $quote eq "\"" 14 2 16
101 16 ▶ $quoted =~ s/\\([\\'])/$1/g if ( $PERL_SINGLE_QUOTE && $quote eq "'");
Branch: 1 branch
BranchTrueFalseTotal
if $WebDAO::Config::PERL_SINGLE_QUOTE and $quote eq "'" 0 16 16
Condition33%1/3
$WebDAO::Config::PERL_SINGLE_QUOTE and $quote eq "'"
!ll&&!rl&&r
1600
MC/DC0%0/2
$WebDAO::Config::PERL_SINGLE_QUOTE and $quote eq "'"
$WebDAO::Config::PERL_SINGLE_QUOTE $quote eq "'"
Truth table33%1/3
A and B
A= $WebDAO::Config::PERL_SINGLE_QUOTEB= $quote eq "'"
ABresult
0X0
100
111
102 }
103 }
104 28 $word .= substr($line, 0, 0); # leave results tainted
105 28 ▶ $word .= defined $quote ? $quoted : $unquoted;
Branch: 1 branch
BranchTrueFalseTotal
defined $quote ? : 16 12 28
106
107 28 ▶ if (length($delim)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $delim) 4 24 28
108 4 push(@pieces, $word);
109 4 ▶ push(@pieces, $delim) if ($keep eq 'delimiters');
Branch: 1 branch
BranchTrueFalseTotal
if $keep eq "delimiters" 0 4 4
110 4 undef $word;
111 }
112 28 ▶ if (!length($line)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $line) 22 6 28
113 22 push(@pieces, $word);
114 }
115 }
116 22 return(@pieces);
117 }
118  
119 #method for convert 'file_name', \*FH, \$string, <IO::File> to hash
120  
121 sub convert_ini2hash {
122 5 ▶ my $data = shift;
Subroutine convert_ini2hash: called CC=17 SCAR=28.5
Pod undocumented
123  
124 #if we got filename
125 5 ▶ unless ( ref $data ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $data) 1 4 5
126 1 my $fh = new IO::File:: "< $data";
127 1 my $res = &convert_ini2hash($fh);
128 1 close $fh;
129 1 return $res;
130 }
131  
132 #We got file descriptor ?
133 4 ▶ if ( ref $data
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data and UNIVERSAL::isa($data, "IO::Handle") || ref $data eq "GLOB" or UNIVERSAL::isa($data, "Tie::Handle")) 0 3 3
Condition55%5/9
ref $data and UNIVERSAL::isa($data, "IO::Handle") || ref $data eq "GLOB" or UNIVERSAL::isa($data, "Tie::Handle")
l!l&&r!l&&!r
103
ref $data and UNIVERSAL::isa($data, "IO::Handle") || ref $data eq "GLOB"
!ll&&!rl&&r
040
UNIVERSAL::isa($data, "IO::Handle") or ref $data eq "GLOB"
l!l&&r!l&&!r
103
MC/DC25%1/4
ref $data and UNIVERSAL::isa($data, "IO::Handle") || ref $data eq "GLOB" or UNIVERSAL::isa($data, "Tie::Handle")
ref $data UNIVERSAL::isa($data, "IO::Handle") ref $data eq "GLOB" UNIVERSAL::isa($data, "Tie::Handle")
Truth table33%2/6
A and B or C or D
A= ref $dataB= UNIVERSAL::isa($data, "IO::Handle")C= ref $data eq "GLOB"D= UNIVERSAL::isa($data, "Tie::Handle")
ABCDresult
11XX1
101X1
0XX11
10011
0XX00
10000
134 and ( UNIVERSAL::isa( $data, 'IO::Handle' ) or ( ref $data ) eq 'GLOB' )
135 or UNIVERSAL::isa( $data, 'Tie::Handle' ) )
136 {
137  
138 #read all data from file descripto to scalar
139 1 my $str;
140 {
141 1 local $/;
142 1 $str = <$data>;
143 }
144 1 return &convert_ini2hash( \$str );
145 }
146 3 my %result = ();
147 3 my $line_num = 0;
148 3 my $section = 'default';
149  
150 #if in param ref to scalar
151 3 foreach ( split /(?:\015{1,2}\012|\015|\012)/, $$data ) {
152 36 my $line = $_;
153 36 $line_num++;
154  
155 # skipping comments and empty lines:
156  
157 36 ▶ $line =~ /^\s*(\n|\#|;)/ and next;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /^\s*(\n|\#|;)/ and next 5 31 36
158 31 ▶ $line =~ /\S/ or next;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /\S/ or next 1 30 31
159  
160 30 chomp $line;
161  
162 30 $line =~ s/^\s+//g;
163 30 $line =~ s/\s+$//g;
164  
165 # parsing the block name:
166 30 ▶ $line =~ /^\s*\[\s*([^\]]+)\s*\]$/ and $section = lc($1), next;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /^\s*\[\s*([^\]]+)\s*\]$/ and $section = lc $1, next 8 22 30
167  
168 # parsing key/value pairs
169 # process ?= and += features
170 22 ▶ if ( $line =~ /^\s*([^=]*\w)\s*([\?\+]?=)\s*(.*)\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*([^=]*\w)\s*([\?\+]?=)\s*(.*)\s*$/) 22 0 22
171 22 my $key = lc($1);
172 22 my @value = parse_line( '\s*,\s*', 0, $3 );
173 22 my $op = $2;
174  
175 #add current key
176 22 ▶ if ( $op =~ /\+=/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($op =~ /\+=/) { } 2 20 22
elsif ($op =~ /\?=/) { } 4 16 20
177 2 push @{ $result{$section}->{$key} }, @value;
178 2 next;
179 }
180  
181 # skip if already defined key
182 elsif ( $op =~ /\?=/ ) {
183 4 ▶ next if defined $result{$section}->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $result{$section}{$key} 2 2 4
184 }
185  
186 # set current value to result hash
187 18 $result{$section}->{$key} = \@value;
188 18 next;
189 }
190  
191 # if we came this far, the syntax couldn't be validated:
192 0 warn "syntax error on line $line_num: '$line'";
193 0 return {};
194 }
195  
196 #strip values
197 3 ▶ while ( my ( $sect_name, $sect_hash ) = each %result ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %result) 6 3 9
198 6 ▶ while ( my ( $key, $val ) = each %$sect_hash ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$sect_hash) 13 6 19
199 13 ▶ if ( scalar(@$val) < 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$val < 2) 8 5 13
200 8 $result{$sect_name}->{$key} = shift @$val;
201 }
202 }
203 }
204 3 return \%result;
205 }
206  
207 sub get_full_path_for {
208 1 ▶ my $root_file = shift;
Subroutine get_full_path_for: called CC=2 SCAR=7.0
Pod undocumented
209  
210 # my $file_to = shift;
211 1 my @req_path = @_;
212 1 my $req_path = join "/", @req_path;
213 1 ▶ return $req_path if $req_path =~ /^\//;
Branch: 1 branch
BranchTrueFalseTotal
if $req_path =~ m[^/] 0 1 1
214 1 my @ini_path = split( "/", $root_file );
215  
216 #strip file name
217 1 pop @ini_path;
218 1 my $path = join "/" => @ini_path, $req_path;
219  
220 # _log1 $self "File $path not exists" unless -e $path;
221 1 return $path;
222 }
223  
224 sub process_includes {
225 3 ▶ my $file = shift;
Subroutine process_includes: called CC=4 SCAR=13.9
Pod undocumented
226 3 ▶ my $fh = ( new IO::File:: "< $file" ) || die "$file: $!";
Condition50%1/2
'IO::File'->new("< $file") || die("${file}: $!")
l!l
30
MC/DC0%0/1
'IO::File'->new("< $file") || die("${file}: $!")
'IO::File'->new("< $file")
Truth table50%1/2
A
A= 'IO::File'->new("< $file")
Aresult
11
00
227 3 my $str = '';
228 3 ▶ while ( defined( my $line = <$fh> ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 28 3 31
229  
230 28 ▶ $str .=
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /#%INCLUDE\s*(.*)\s*%/ ? : 1 27 28
231 $line =~ /#%INCLUDE\s*(.*)\s*%/
232 ? &process_includes( &get_full_path_for( $file, $1 ) )
233 : $line;
234 }
235 3 close $fh;
236 3 return $str;
237 }
238  
239 sub new {
240 1 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
241 1 my $self = {};
242 1 my $stat;
243 1 bless( $self, $class );
244 1 $self->_init(@_);
245 1 return $self;
246 }
247  
248 sub _init {
249 1 ▶ my $self = shift;
Subroutine _init: called CC=1 SCAR=0.0
250 1 my $file_path = shift;
251  
252 #process inludes in in data
253 1 my $inc = &process_includes($file_path);
254 1 $self->__conf( &convert_ini2hash(\$inc) );
255 1 $self->_path($file_path);
256 1 return 1;
257 }
258  
259 sub get_full_path {
260 0 ▶ my $self = shift;
Subroutine get_full_path: not called CC=3 SCAR=24.8
Pod undocumented
261 0 my @req_path = @_;
262 0 my $req_path = join "/", @req_path;
263 0 ▶ return $req_path if $req_path =~ /^\//;
Branch: 1 branch
BranchTrueFalseTotal
if $req_path =~ m[^/] 0 0 0
264 0 my @ini_path = split( "/", $self->_path );
265 0 pop @ini_path;
266 0 my $path = join "/" => @ini_path, $req_path;
267 0 ▶ _log1 "File $path not exists" unless -e $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $path 0 0 0
268 0 return $path;
269 }
270  
271 sub AUTOLOAD {
272 2 ▶ my $self = shift;
Subroutine AUTOLOAD: called CC=2 SCAR=6.9
273 2 ▶ return if $AUTOLOAD =~ /::DESTROY$/;
Branch: 1 branch
BranchTrueFalseTotal
if $WebDAO::Config::AUTOLOAD =~ /::DESTROY$/ 1 1 2
274 1 ( my $auto_sub ) = $AUTOLOAD =~ /.*::(.*)/;
275 1 return $self->__conf->{$auto_sub};
276 }
277 1 1;
278 __END__
279  
280 =head1 SEE ALSO
281  
282 WebDAO, README
283  
284 =head1 AUTHOR
285  
286 Zahatski Aliaksandr, E<lt>zag@cpan.orgE<gt>
287  
288 =head1 COPYRIGHT AND LICENSE
289  
290 Copyright (C) 2006-2011 by Zahatski Aliaksandr
291  
292 This library is free software; you can redistribute it and/or modify
293 it under the same terms as Perl itself, either Perl version 5.8.8 or,
294 at your option, any later version of Perl 5 you may have available.
295  
296 =cut
297  
« blib/lib/WebDAO/Element.pm ↑ Summary blib/lib/WebDAO/CV.pm »