blib/lib/Config/Processor.pm

filter: Statementstmt 98.3% 242 / 246 Branchbran 87.7% 79 / 90 Conditioncond 100% 12 / 12 MC/DCmcdc 100% 7 / 7 Subroutinesub 93.5% 29 / 31 Podpod 100% 2 / 2 totaltotal 95.6% 371 / 388 CC 59 SCAR 20.2
CC
59
Cov
96%
_build_tree
15.0
_process_node
12.0
_fetch_value
10.0
CRAP 7.6
↑ Summary
1 package Config::Processor;
2  
3 5 ▶use 5.008000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 our $VERSION = '0.28';
8  
9 5 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use YAML::XS qw( LoadFile );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Cpanel::JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Hash::Merge;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Scalar::Util qw( refaddr readonly );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 my %FILE_EXTENSIONS_MAP = (
17 yml => 'yaml',
18 yaml => 'yaml',
19 json => 'json',
20 jsn => 'json',
21 );
22  
23 7 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
24 7 my $class = shift;
25 7 my %params = @_;
26  
27 7 my $self = bless {}, $class;
28  
29 7 ▶ $self->{dirs} = $params{dirs} || [];
Condition100%2/2
$params{'dirs'} || []
l!l
52
MC/DC100%1/1
$params{'dirs'} || []
$params{'dirs'}
Truth table100%2/2
A
A= $params{'dirs'}
Aresult
11
00
30 7 ▶ unless ( @{ $self->{dirs} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"dirs"};}) 2 5 7
31 2 push( @{ $self->{dirs} }, '.' );
32 }
33  
34 $self->{interpolate_variables} = exists $params{interpolate_variables}
35 7 ▶ ? $params{interpolate_variables} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $params{'interpolate_variables'} ? : 1 6 7
36 $self->{process_directives} = exists $params{process_directives}
37 7 ▶ ? $params{process_directives} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $params{'process_directives'} ? : 1 6 7
38 7 $self->{export_env} = $params{export_env};
39  
40 7 my $merger = Hash::Merge->new;
41 $merger->specify_behavior(
42 { SCALAR => {
43 105 ▶ SCALAR => sub { $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
44 2 ▶ ARRAY => sub { $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
45 4 ▶ HASH => sub { $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
46 },
47 ARRAY => {
48 2 ▶ SCALAR => sub { $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
49 39 ▶ ARRAY => sub { $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
50 0 ▶ HASH => sub { $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
51 },
52 HASH => {
53 8 ▶ SCALAR => sub { $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
54 0 ▶ ARRAY => sub { $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
55 193 ▶ HASH => sub { $merger->_merge_hashes( $_[0], $_[1] ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
56 },
57 },
58 7 'CONFIG_PRECEDENT',
59 );
60 7 $self->{_merger} = $merger;
61  
62 7 $self->{_config} = undef;
63 7 $self->{_vars} = {};
64 7 $self->{_seen_nodes} = {};
65  
66 7 return $self;
67 }
68  
69 {
70 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 5 foreach my $name (
73 qw( interpolate_variables process_directives export_env ) )
74 {
75 15 ▶ *{$name} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
76 16 my $self = shift;
77  
78 16 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 7 9 16
79 7 $self->{$name} = shift;
80 }
81  
82 16 return $self->{$name};
83 }
84 15 }
85 }
86  
87 15 ▶sub load {
Subroutine load: called CC=2 SCAR=6.9
Pod documented
88 15 my $self = shift;
89 15 my @config_sections = @_;
90  
91 15 $self->{_config} = $self->_build_tree(@config_sections);
92 10 ▶ if ( $self->{export_env} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"export_env"}) 1 9 10
93 $self->{_config} = $self->{_merger}->merge( $self->{_config},
94 1 { ENV => {%ENV} } );
95 }
96 10 $self->_process_tree( $self->{_config}, [] );
97  
98 9 $self->{_vars} = {};
99 9 $self->{_seen_nodes} = {};
100  
101 9 return $self->{_config};
102 }
103  
104 32 ▶sub _build_tree {
Subroutine _build_tree: called CC=15 SCAR=27.1
105 32 my $self = shift;
106 32 my @config_sections = @_;
107  
108 32 my $config = {};
109  
110 32 foreach my $config_section (@config_sections) {
111 66 ▶ next unless defined $config_section;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config_section 0 66 66
112  
113 66 ▶ if ( ref($config_section) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $config_section eq 'HASH') { } 2 64 66
114 2 $config = $self->{_merger}->merge( $config, $config_section );
115 }
116 else {
117 64 my %not_found_idx;
118 64 my @file_patterns = split( /\s+/, $config_section );
119  
120 64 foreach my $dir ( @{ $self->{dirs} } ) {
121 65 foreach my $file_pattern ( @file_patterns ) {
122 5 ▶ my @file_pathes = glob( File::Spec->catfile( $dir, $file_pattern ) );
Subroutine BEGIN: called CC=1 SCAR=0.0
123  
124 67 foreach my $file_path (@file_pathes) {
125 73 ▶ next if -d $file_path;
Branch: 1 branch
BranchTrueFalseTotal
if -d $file_path 0 73 73
126  
127 73 ▶ unless ( $file_path =~ m/\.([^.]+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file_path =~ /\.([^.]+)$/) 1 72 73
128 1 croak "File extension not specified."
129 . " Don't known how parse $file_path";
130 }
131  
132 72 my $file_ext = $1;
133 72 my $file_type = $FILE_EXTENSIONS_MAP{$file_ext};
134  
135 72 ▶ unless ( defined $file_type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file_type) 1 71 72
136 1 croak "Unknown file extension \".$file_ext\" encountered."
137 . " Don't known how parse $file_path";
138 }
139  
140 71 ▶ unless ( -e $file_path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file_path) 4 67 71
141 4 ▶ $not_found_idx{$file_pattern} ||= 0;
Condition100%2/2
$not_found_idx{$file_pattern} ||= 0
l!l
22
MC/DC100%1/1
$not_found_idx{$file_pattern} ||= 0
$not_found_idx{$file_pattern}
Truth table100%2/2
A
A= $not_found_idx{$file_pattern}
Aresult
11
00
142 4 $not_found_idx{$file_pattern}++;
143  
144 4 next;
145 }
146  
147 67 my @data = eval {
148 67 my $method = "_load_$file_type";
149 67 return $self->$method($file_path);
150 };
151 67 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 2 65 67
152 2 croak "Can't parse $file_path\n$@";
153 }
154  
155 65 foreach my $data_chunk (@data) {
156 65 $config = $self->{_merger}->merge( $config, $data_chunk );
157 }
158 }
159 }
160 }
161  
162 my @not_found = grep {
163 60 $not_found_idx{$_} == scalar @{ $self->{dirs} }
164 } keys %not_found_idx;
165  
166 60 ▶ if ( @not_found ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@not_found) 1 59 60
167 croak "Can't locate " . join( ', ', @not_found )
168 1 . " in " . join( ', ', @{ $self->{dirs} } );
169 }
170 }
171 }
172  
173 27 return $config;
174 }
175  
176 44 ▶sub _load_yaml {
Subroutine _load_yaml: called CC=1 SCAR=0.0
177 44 my $self = shift;
178 44 my $file_path = shift;
179  
180 44 return LoadFile($file_path);
181 }
182  
183 23 ▶sub _load_json {
Subroutine _load_json: called CC=2 SCAR=7.0
184 23 my $self = shift;
185 23 my $file_path = shift;
186  
187 23 ▶ open( my $fh, '<', $file_path ) || die "Can't open $file_path: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file_path or die "Can't open ${file_path}: $!" 23 0 23
188 23 my @data = ( decode_json( join( '', <$fh> ) ) );
189 22 close($fh);
190  
191 22 return @data;
192 }
193  
194 963 ▶sub _process_tree {
Subroutine _process_tree: called CC=8 SCAR=20.8
195 963 my $self = shift;
196 963 my $ancs = pop;
197  
198 963 ▶ return if readonly( $_[0] );
Branch: 1 branch
BranchTrueFalseTotal
if &readonly($_[0]) 0 963 963
199  
200 963 $_[0] = $self->_process_node( $_[0], $ancs );
201  
202 962 ▶ if ( my $node_addr = refaddr( $_[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $node_addr = builtin::refaddr($_[0])) 266 696 962
203 266 ▶ return if $self->{_seen_nodes}{$node_addr};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_seen_nodes"}{$node_addr} 9 257 266
204  
205 257 $self->{_seen_nodes}{$node_addr} = 1;
206 }
207  
208 953 ▶ if ( ref( $_[0] ) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 175 778 953
elsif (ref $_[0] eq 'ARRAY') { } 82 696 778
209 175 foreach ( values %{ $_[0] } ) {
210 775 $self->_process_tree( $_, [ $_[0], @{$ancs} ] );
211 }
212 }
213 elsif ( ref( $_[0] ) eq 'ARRAY' ) {
214 82 foreach ( @{ $_[0] } ) {
215 178 $self->_process_tree( $_, [ $_[0], @{$ancs} ] );
216 }
217 }
218  
219 951 return;
220 }
221  
222 1092 ▶sub _process_node {
Subroutine _process_node: called CC=12 SCAR=24.9
223 1092 my $self = shift;
224 1092 my $node = shift;
225 1092 my $ancs = shift;
226  
227 1092 ▶ return unless defined $node;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 0 1092 1092
228  
229 1092 ▶ if ( !ref($node) && $self->{interpolate_variables} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $node and $self->{'interpolate_variables'}) { } 699 393 1092
elsif (ref $node eq 'HASH' and $self->{'process_directives'}) { } 198 343 541
Condition100%3/3
not ref $node and $self->{"interpolate_variables"}
!ll&&!rl&&r
33063699
MC/DC100%2/2
not ref $node and $self->{"interpolate_variables"}
not ref $node $self->{"interpolate_variables"}
Truth table100%3/3
not(A) and B
A= not ref $nodeB= $self->{"interpolate_variables"}
ABresult
0X0
100
111
230 699 $node =~ s/\$((\$?)\{([^\}]*)\})/
231 85 ▶ $2 ? $1 : ( $self->_resolve_var( $3, [ @{$ancs} ] ) || '' )/ge;
Branch: 1 branch
BranchTrueFalseTotal
$2 ? : 2 83 85
Condition100%2/2
$self->_resolve_var($3, [@{$ancs;}]) or ""
l!l
749
MC/DC100%1/1
$self->_resolve_var($3, [@{$ancs;}]) or ""
$self->_resolve_var($3, [@{$ancs;}])
Truth table100%2/2
A
A= $self->_resolve_var($3, [@{$ancs;}])
Aresult
11
00
232 }
233 ▶ elsif ( ref($node) eq 'HASH' && $self->{process_directives} ) {
Condition100%3/3
ref $node eq "HASH" and $self->{"process_directives"}
!ll&&!rl&&r
14847198
MC/DC100%2/2
ref $node eq "HASH" and $self->{"process_directives"}
ref $node eq "HASH" $self->{"process_directives"}
Truth table100%3/3
A and B
A= ref $node eq "HASH"B= $self->{"process_directives"}
ABresult
0X0
100
111
234 198 ▶ if ( defined $node->{var} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $node->{'var'}) { } 35 163 198
elsif (defined $node->{'include'}) { } 17 146 163
235 35 $node = $self->_resolve_var( $node->{var}, [ @{$ancs} ] );
236 }
237 elsif ( defined $node->{include} ) {
238 17 $node = $self->_build_tree( $node->{include} );
239 }
240 else {
241 146 ▶ if ( defined $node->{underlay} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node->{"underlay"}) 18 128 146
242 18 my $layer = delete $node->{underlay};
243 18 $layer = $self->_process_layer( $layer, $ancs );
244 18 $node = $self->{_merger}->merge( $layer, $node );
245 }
246  
247 146 ▶ if ( defined $node->{overlay} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node->{"overlay"}) 15 131 146
248 15 my $layer = delete $node->{overlay};
249 15 $layer = $self->_process_layer( $layer, $ancs );
250 15 $node = $self->{_merger}->merge( $node, $layer );
251 }
252 }
253 }
254  
255 1091 return $node;
256 }
257  
258 33 ▶sub _process_layer {
Subroutine _process_layer: called CC=4 SCAR=13.9
259 33 my $self = shift;
260 33 my $layer = shift;
261 33 my $ancs = shift;
262  
263 33 ▶ if ( ref($layer) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $layer eq 'HASH') { } 18 15 33
elsif (ref $layer eq 'ARRAY') { } 11 4 15
264 18 $layer = $self->_process_node( $layer, $ancs );
265 }
266 elsif ( ref($layer) eq 'ARRAY' ) {
267 11 my $new_layer = {};
268  
269 11 foreach my $node ( @{$layer} ) {
270 28 $node = $self->_process_node( $node, $ancs );
271 28 $new_layer = $self->{_merger}->merge( $new_layer, $node );
272 }
273  
274 11 $layer = $new_layer;
275 }
276  
277 33 return $layer;
278 }
279  
280 118 ▶sub _resolve_var {
Subroutine _resolve_var: called CC=8 SCAR=20.9
281 118 my $self = shift;
282 118 my $name = shift;
283 118 my $ancs = shift;
284  
285 118 my $value;
286  
287 118 ▶ if ( $name =~ m/^\./ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^\./) { } 10 108 118
288 10 my $node;
289 10 my @tokens = split( /\./, $name, -1 );
290  
291 10 while (1) {
292 24 my $token = $tokens[0];
293 24 $token =~ s/^\s+//;
294 24 $token =~ s/\s+$//;
295  
296 24 ▶ last if length($token) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $token > 0 10 14 24
297  
298 14 shift @tokens;
299  
300 14 ▶ last unless @tokens;
Branch: 1 branch
BranchTrueFalseTotal
unless @tokens 0 14 14
301 14 ▶ next unless @{$ancs};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$ancs;} 0 14 14
302  
303 14 $node = shift @{$ancs};
304 }
305  
306 10 $value = eval {
307 10 $self->_fetch_value( $node, $ancs, \@tokens );
308 };
309  
310 10 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 10 10
311 0 chomp $@;
312 0 die qq{Can't resolve variable "$name"; $@\n};
313 }
314 }
315 else {
316 108 my $vars = $self->{_vars};
317  
318 108 ▶ unless ( defined $vars->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $vars->{$name}) 83 25 108
319 83 my @tokens = split( /\./, $name, -1 );
320  
321 83 $vars->{$name} = eval {
322 83 $self->_fetch_value( $self->{_config}, [], \@tokens );
323 };
324  
325 83 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 82 83
326 1 chomp $@;
327 1 die qq{Can't resolve variable "$name"; $@\n};
328 }
329 }
330  
331 107 $value = $vars->{$name};
332 }
333  
334 117 return $value;
335 }
336  
337 ####
338 93 ▶sub _fetch_value {
Subroutine _fetch_value: called CC=10 SCAR=23.0
339 93 my $self = shift;
340 93 my $node = shift;
341 93 my $ancs = shift;
342 93 my $tokens = shift;
343  
344 93 ▶ return $node unless @{$tokens};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$tokens;} 0 93 93
345  
346 93 my $value;
347  
348 93 while (1) {
349 240 my $token = shift @{$tokens};
350 240 $token =~ s/^\s+//;
351 240 $token =~ s/\s+$//;
352  
353 240 ▶ if ( ref($node) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node eq 'HASH') { } 205 35 240
354 205 ▶ last unless defined $node->{$token};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node->{$token} 7 198 205
355  
356 198 unshift( @{$ancs}, $node );
357  
358 198 ▶ unless ( @{$tokens} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$tokens;}) 55 143 198
359 55 $node->{$token} = $self->_process_node( $node->{$token}, $ancs );
360 55 $value = $node->{$token};
361  
362 55 last;
363 }
364  
365 143 ▶ last unless ref( $node->{$token} );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node->{$token} 0 143 143
366  
367 143 $node = $node->{$token};
368 }
369 else { # ARRAY
370 35 ▶ if ( $token =~ m/\D/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /\D/) 1 34 35
371 1 die qq{Argument "$token" isn't numeric in array element.\n};
372 }
373  
374 34 ▶ last unless defined $node->[$token];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node->[$token] 2 32 34
375  
376 32 unshift( @{$ancs}, $node );
377  
378 32 ▶ unless ( @{$tokens} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$tokens;}) 28 4 32
379 28 $node->[$token] = $self->_process_node( $node->[$token], $ancs );
380 28 $value = $node->[$token];
381  
382 28 last;
383 }
384  
385 4 ▶ last unless ref( $node->[$token] );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node->[$token] 0 4 4
386  
387 4 $node = $node->[$token];
388 }
389 }
390  
391 92 return $value;
392 }
393  
394 5 1;
395 __END__
↑ Summary