blib/lib/Poet/Conf.pm

filter: Statementstmt 96.2% 284 / 295 Branchbran 84.7% 61 / 72 Conditioncond 67.8% 19 / 28 MC/DCmcdc 64.7% 11 / 17 Subroutinesub 95.7% 67 / 70 Podpod n/a totaltotal 91.7% 442 / 482 CC 83 SCAR 11.4
CC
83
Cov
92%
__ANON__
7.1
read_conf_data
7.1
__ANON__
6.0
CRAP 3.1
« blib/lib/Poet/Tools.pm ↑ Summary blib/lib/Poet/t/PSGIHandler.pm »
1 package Poet::Conf;
2 10 $Poet::Conf::VERSION = '0.16';
3 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use Cwd qw(realpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use Data::Rmap qw(rmap_scalar);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use File::Spec::Functions qw(catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Guard;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Poet::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Poet::Tools qw(read_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use YAML::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 10 has 'conf_dir' => ( required => 1 );
17 10 has 'data' => ( init_arg => undef );
18 10 has 'is_development' => ( init_arg => undef, lazy_build => 1 );
19 10 has 'is_live' => ( init_arg => undef, lazy_build => 1 );
20 10 has 'layer' => ( init_arg => undef, lazy_build => 1 );
21 10 has 'root_dir' => ( required => 1 );
22  
23 10 our %get_cache;
24  
25 10 ▶method BUILD () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BUILD: called CC=1 SCAR=0.0
26 17 $self->{data} = $self->read_conf_data();
27 10 }
28  
29 10 ▶method initial_conf_data () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine initial_conf_data: called CC=1 SCAR=0.0
30 3 return ( root_dir => $self->root_dir, root => $self->root_dir );
31 10 }
32  
33 10 ▶method read_conf_data () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine read_conf_data: called CC=7 SCAR=19.5
34 15 my %data = $self->initial_conf_data();
35  
36 # Collect list of conf files in appropriate order
37 #
38 15 my @conf_files = $self->ordered_conf_files();
39  
40 # Stores the file where each global/* key is declared.
41 #
42 14 my %global_keys;
43  
44 14 foreach my $file (@conf_files) {
45 62 ▶ if ( defined $file && -f $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file and -f $file) 45 17 62
Condition50%3/6
defined $file and -f $file
!ll&&!rl&&r
12545
defined $file and -f $file
!ll&&!rl&&r
000
MC/DC100%2/2
defined $file and -f $file
defined $file -f $file
MC/DC0%0/2
defined $file and -f $file
defined $file -f $file
Truth table100%3/3
A and B
A= defined $fileB= -f $file
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $fileB= -f $file
ABresult
0X0
100
111
46  
47 # Read conf file into hash
48 #
49 45 my $new_data = $self->read_conf_file($file);
50  
51 # Make sure no keys are defined in multiple global conf files
52 #
53 45 ▶ if ( $file =~ m{/global/} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ m[/global/]) 5 40 45
54 5 foreach my $key ( keys(%$new_data) ) {
55 13 ▶ if ( my $previous_file = $global_keys{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $previous_file = $global_keys{$key}) { } 1 12 13
56 1 die sprintf(
57 "top-level key '%s' defined in both '%s' and '%s' - global conf files must be mutually exclusive",
58 $key, $previous_file, $file );
59 }
60 else {
61 12 $global_keys{$key} = $file;
62 }
63 }
64 }
65  
66 # Merge new hash into current data
67 #
68 44 $self->merge_conf_data( \%data, $new_data, $file );
69 }
70 60 $self->_flush_get_cache();
71 }
72  
73 12 return \%data;
74 10 }
75  
76 10 ▶method _build_layer () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=4 SCAR=13.9
Subroutine _build_layer: called CC=4 SCAR=13.9
77 19 my $conf_dir = $self->conf_dir;
78 19 my $local_cfg_file = catfile( $conf_dir, "local.cfg" );
79 19 ▶ my $local_cfg =
Branch: 2 branches
BranchTrueFalseTotal
-f $local_cfg_file ? : 19 0 19
-f $local_cfg_file ? : 16 47 63
80 ( -f $local_cfg_file )
81 ? $self->read_conf_file($local_cfg_file)
82 : {};
83 my $layer = $local_cfg->{layer}
84 19 ▶ || die "must specify layer in '$local_cfg_file'";
Condition80%4/5
$local_cfg->{'layer'} || die("must specify layer in '${local_cfg_file}'")
l!l
181
$local_cfg->{'layer'} || die("must specify layer in '${local_cfg_file}'")
l!l
4013
MC/DC100%1/1
$local_cfg->{'layer'} || die("must specify layer in '${local_cfg_file}'")
$local_cfg->{'layer'}
MC/DC100%1/1
$local_cfg->{'layer'} || die("must specify layer in '${local_cfg_file}'")
$local_cfg->{'layer'}
Truth table100%2/2
A
A= $local_cfg->{'layer'}
Aresult
11
00
Truth table100%2/2
A
A= $local_cfg->{'layer'}
Aresult
11
00
85 18 ▶ die "invalid layer '$layer' - no such file '$conf_dir/layer/$layer.cfg'"
Branch: 2 branches
BranchTrueFalseTotal
unless -f "$conf_dir/layer/$layer.cfg" 0 18 18
unless -f "$conf_dir/layer/$layer.cfg" 1 14 15
86 unless -f "$conf_dir/layer/$layer.cfg";
87  
88 18 return $layer;
89 10 }
90  
91 0 ▶method _build_is_development () {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _build_is_development: called CC=1 SCAR=0.0
92 18 return $self->layer eq 'development';
93 10 }
94  
95 10 ▶method _build_is_live () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _build_is_live: called CC=1 SCAR=0.0
96 16 return !$self->is_development;
97 10 }
98  
99 10 ▶method ordered_conf_files () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=2 SCAR=6.9
100 14 my $conf_dir = $self->conf_dir();
101 14 my $layer = $self->layer();
102  
103 return (
104 "$conf_dir/global.cfg",
105 3 ▶ glob("$conf_dir/global/*.cfg"),
Subroutine BEGIN: called CC=1 SCAR=0.0
106 (
107 $self->is_live
108 ? ("$conf_dir/layer/live.cfg")
109 : ()
110 ),
111 "$conf_dir/layer/$layer.cfg",
112 "$conf_dir/local.cfg",
113 $ENV{POET_EXTRA_CONF_FILE},
114 13 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$self->is_live ? : 5 8 13
115 10 }
116  
117 10 ▶method read_conf_file ($file) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine read_conf_file: called CC=2 SCAR=7.0
118  
119 # Read a yaml file into a hash, adding a dummy key pair to handle empty
120 # files or files with nothing but comments, and checking for errors.
121 # Return the hash.
122 #
123 78 my $dummy = "__yaml_init";
124 78 my $yaml = read_file($file) . "\n\n$dummy: 0";
125 78 my $hash;
126 78 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
127 78 $hash = YAML::XS::Load($yaml);
128 }
129 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
130 0 die "error parsing conf file '$file': $_";
131 78 };
132 78 ▶ die "'$file' did not parse to a hash" unless ref($hash) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $hash eq "HASH" 0 78 78
unless ref $hash eq "HASH" 0 1 1
133 78 delete( $hash->{$dummy} );
134 78 return $hash;
135 10 }
136  
137 10 ▶method merge_conf_data ($current_data, $new_data, $file) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine merge_conf_data: called CC=5 SCAR=16.1
138 64 ▶ while ( my ( $key, $value ) = each(%$new_data) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$new_data) 72 63 135
139 72 my $orig_key = $key;
140 72 my $assign_to_hash = $current_data;
141 72 ▶ while ( $key =~ /\./ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /\./) 22 71 93
142 22 my ( $first, $rest ) = split( /\./, $key, 2 );
143 22 ▶ if ( !defined( $assign_to_hash->{$first} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $assign_to_hash->{$first}) 16 6 22
144 16 $assign_to_hash->{$first} = {};
145 }
146 22 $assign_to_hash = $assign_to_hash->{$first};
147 22 ▶ if ( ref($assign_to_hash) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $assign_to_hash ne "HASH") 1 21 22
148 1 die sprintf( "error assigning to '%s' in '%s'; '%s' already has non-hash value",
149 $orig_key, $file, substr( $orig_key, 0, -1 * length($rest) - 1 ) );
150 }
151 21 $key = $rest;
152 }
153 71 $assign_to_hash->{$key} = $value;
154 }
155 10 }
156  
157 # Memoize get, since conf can normally not change at runtime. This will
158 # benefit all get() and get_*() calls. Clear cache on set_local.
159 #
160 10 ▶method _flush_get_cache () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
161 91 %get_cache = ();
162 10 }
163  
164 10 ▶method get ($key, $default) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine get: called CC=5 SCAR=16.1
165 464 ▶ croak "key required" if !defined($key);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 464 464
166 464 ▶ return $get_cache{$key} if exists( $get_cache{$key} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $Poet::Conf::get_cache{$key} 174 290 464
167  
168 290 my $orig_key = $key;
169 290 my @firsts;
170 290 ▶ if ( $key =~ /\./ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /\./) 206 84 290
171 206 return $self->_get_dotted_key( $key, $default );
172 }
173 84 my $value = $self->data->{$key};
174 84 ▶ rmap_scalar { $_ = $self->interpolate_value($_) if defined && !ref } $value;
Subroutine __ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ and not ref $_ 62 42 104
Condition66%2/3
defined $_ and not ref $_
!ll&&!rl&&r
42062
MC/DC50%1/2
defined $_ and not ref $_
defined $_ not ref $_
Truth table66%2/3
A and not(B)
A= defined $_B= not ref $_
ABresult
0X0
100
111
175 83 $get_cache{$key} = $value;
176 83 ▶ return defined($value) ? $value : $default;
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 41 42 83
177 10 }
178  
179 10 ▶method _get_dotted_key ($key, $default) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _get_dotted_key: called CC=2 SCAR=6.9
180 165 my ( $rest, $last ) = ( $key =~ /^(.*)\.([^\.]+)$/ );
181 165 my $value = $self->get_hash($rest)->{$last};
182 164 ▶ return defined($value) ? $value : $default;
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 39 125 164
183 10 }
184  
185 10 ▶method interpolate_value ($value) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine interpolate_value: called CC=3 SCAR=11.0
186 60 ▶ while ( $value =~ /(\$ \{ ([\w\.\-]+) \} )/x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /(\$ \{ ([\w\.\-]+) \} )/x) 6 59 65
187 6 my $var_decl = $1;
188 6 my $var_key = $2;
189 6 my $var_value = $self->get_or_die($var_key);
190 5 ▶ $var_value = '' if !defined($var_value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $var_value 0 5 5
191 5 $value =~ s/\Q$var_decl\E/$var_value/;
192 }
193 59 return $value;
194 10 }
195  
196 10 ▶method get_or_die ($key) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=2 SCAR=6.9
197 6 ▶ if ( defined( my $value = $self->get($key) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $value = $self->get($key))) { } 5 1 6
198 5 return $value;
199 }
200 else {
201 1 croak "could not get conf for '$key'";
202 }
203 10 }
204  
205 10 ▶method get_list ($key, $default) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine get_list: called CC=4 SCAR=13.9
206 7 ▶ if ( defined( my $value = $self->get($key) ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $value = $self->get($key))) { } 5 2 7
elsif (defined $default) { } 0 2 2
207 5 ▶ if ( ref($value) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 3 2 5
208 3 return $value;
209 }
210 else {
211 2 my $error =
212 sprintf( "list value expected for conf key '%s', got non-list '%s'", $key, $value );
213 2 croak($error);
214 }
215 }
216 elsif ( defined $default ) {
217 0 return $default;
218 }
219 else {
220 2 return [];
221 }
222 10 }
223  
224 10 ▶method get_hash ($key, $default) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine get_hash: called CC=4 SCAR=13.9
225 230 ▶ if ( defined( my $value = $self->get($key) ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $value = $self->get($key))) { } 59 171 230
elsif (defined $default) { } 8 163 171
226 59 ▶ if ( ref($value) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'HASH') { } 56 3 59
227 56 return $value;
228 }
229 else {
230 3 my $error =
231 sprintf( "hash value expected for conf key '%s', got non-hash '%s'", $key, $value );
232 3 croak($error);
233 }
234 }
235 elsif ( defined $default ) {
236 8 return $default;
237 }
238 else {
239 163 return {};
240 }
241 10 }
242  
243 10 ▶method get_boolean ($key) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=6 SCAR=17.9
244 11 ▶ my $value = $self->get($key) || 0;
Condition50%1/2
$self->get($key) || 0
l!l
110
MC/DC0%0/1
$self->get($key) || 0
$self->get($key)
Truth table50%1/2
A
A= $self->get($key)
Aresult
11
00
245 return
246 11 ▶ ( !ref($value) && $value =~ /^(1|t|true|y|yes)$/i ) ? 1
Branch: 2 branches
BranchTrueFalseTotal
!ref($value) && $value =~ /^(1|t|true|y|yes)$/i ? : 4 7 11
!ref($value) && $value =~ /^(0|f|false|n|no)$/i ? : 4 3 7
Condition100%6/6
not ref $value and $value =~ /^(1|t|true|y|yes)$/i
!ll&&!rl&&r
254
not ref $value and $value =~ /^(0|f|false|n|no)$/i
!ll&&!rl&&r
214
MC/DC100%2/2
not ref $value and $value =~ /^(1|t|true|y|yes)$/i
not ref $value $value =~ /^(1|t|true|y|yes)$/i
MC/DC100%2/2
not ref $value and $value =~ /^(0|f|false|n|no)$/i
not ref $value $value =~ /^(0|f|false|n|no)$/i
Truth table100%3/3
not(A) and B
A= not ref $valueB= $value =~ /^(1|t|true|y|yes)$/i
ABresult
0X0
100
111
Truth table100%3/3
not(A) and B
A= not ref $valueB= $value =~ /^(0|f|false|n|no)$/i
ABresult
0X0
100
111
247 : ( !ref($value) && $value =~ /^(0|f|false|n|no)$/i ) ? 0
248 : croak(
249 sprintf( "boolean value expected for conf key '%s', got non-boolean '%s'", $key, $value ) );
250 10 }
251  
252 10 ▶method set_local ($pairs) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine set_local: called CC=3 SCAR=11.1
253 7 ▶ if ( !defined(wantarray) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 7 7
254 0 warn "result of set_local must be assigned!";
255 }
256 7 ▶ croak "set_local expects hashref" unless ref($pairs) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $pairs eq "HASH" 0 7 7
257  
258 # Make a deep copy of current data, then merge in the new pairs
259 #
260 7 my $orig_data = dclone( $self->{data} );
261 7 $self->merge_conf_data( $self->{data}, $pairs, "set_local" );
262 7 $self->conf_has_changed();
263  
264 # Restore original data when $guard goes out of scope
265 #
266 7 ▶ my $guard = guard { $self->{data} = $orig_data; $self->conf_has_changed() };
Subroutine __ANON__: called CC=1 SCAR=0.0
267 7 return $guard;
268 10 }
269  
270 # Get key from secure conf, fallback to normal conf. Maintain separate
271 # secure_conf hash for each Conf object (e.g. for testing).
272 #
273 10 my %secure_confs;
274  
275 10 ▶method get_secure ($key) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine get_secure: called CC=3 SCAR=11.7
276 4 ▶ die "key required" unless defined($key);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 4 4
277 4 ▶ return $self->_get_secure_conf->{$key} || $self->get($key);
Condition50%3/6
$self->_get_secure_conf->{$key} || $self->get($key)
l!l&&r!l&&!r
211
$self->_get_secure_conf->{$key} || $self->get($key)
l!l&&r!l&&!r
000
MC/DC100%2/2
$self->_get_secure_conf->{$key} || $self->get($key)
$self->_get_secure_conf->{$key} $self->get($key)
MC/DC0%0/2
$self->_get_secure_conf->{$key} || $self->get($key)
$self->_get_secure_conf->{$key} $self->get($key)
Truth table100%3/3
A || B
A= $self->_get_secure_conf->{$key}B= $self->get($key)
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $self->_get_secure_conf->{$key}B= $self->get($key)
ABresult
1X1
011
000
278 10 }
279  
280 10 ▶method _get_secure_conf () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _get_secure_conf: called CC=3 SCAR=11.0
281 4 ▶ if ( !$secure_confs{"$self"} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($secure_confs{"$self"}) 1 3 4
282 1 my $secure_conf_file =
283 $self->get( 'conf.secure_conf_file' => $self->conf_dir . "/secure.cfg" );
284 1 ▶ $secure_confs{"$self"} =
Branch: 1 branch
BranchTrueFalseTotal
-f $secure_conf_file ? : 1 0 1
285 ( -f $secure_conf_file )
286 ? $self->read_conf_file($secure_conf_file)
287 : {};
288 }
289 4 return $secure_confs{"$self"};
290 10 }
291  
292 10 ▶method generate_dynamic_conf () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
293 1 require MasonX::ProcessDir;
294 1 my $poet = Poet::Environment->current_env;
295 1 my $source_dir = $self->conf_dir . "/dynamic";
296 1 my $dest_dir = $poet->data_path("conf/dynamic");
297 my $pd = MasonX::ProcessDir->new(
298 source_dir => $source_dir,
299 dest_dir => $dest_dir,
300 2 ▶ ignore_files => sub { $_[0] =~ /Base\.|\.mi$|gen\.pl|README/ },
Subroutine __ANON__: called CC=1 SCAR=0.0
301 1 mason_options => {},
302 @_
303 );
304 1 $pd->process_dir();
305 10 }
306  
307 10 ▶method get_keys () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine get_keys: called CC=1 SCAR=0.0
308 1 return keys( %{ $self->{data} } );
309 10 }
310  
311 0 ▶method as_hash () {
Subroutine BEGIN: called CC=1 SCAR=2.6
Subroutine as_hash: not called CC=1 SCAR=4.1
312 0 return { map { ( $_, $self->get($_) ) } $self->get_keys() };
313 10 }
314  
315 0 ▶method as_string () {
Subroutine BEGIN: called CC=1 SCAR=2.6
Subroutine __ANON__: not called CC=1 SCAR=3.5
316 0 return YAML::XS::Dump( $self->as_hash );
317 10 }
318  
319 # Things we need to do whenever the conf changes.
320 #
321 10 ▶method conf_has_changed () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine conf_has_changed: called CC=1 SCAR=0.0
322 14 $self->_flush_get_cache();
323 10 }
324  
325 10 __PACKAGE__->meta->make_immutable();
326  
327 10 1;
328  
329 __END__
« blib/lib/Poet/Tools.pm ↑ Summary blib/lib/Poet/t/PSGIHandler.pm »