lib/Mojolicious/Plugin/INIConfig/Extended.pm

filter: Statementstmt 98.8% 83 / 84 Branchbran 96.6% 29 / 30 Conditioncond 70.2% 26 / 37 MC/DCmcdc 50.0% 11 / 22 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 86.9% 160 / 184 CC 37 SCAR 29.0
CC
37
Cov
91%
register
26.9
inherit
10.0
_get_keys
3.0
CRAP 18.1
↑ Summary
1 package Mojolicious::Plugin::INIConfig::Extended;
2 2 ▶use Mojo::Base 'Mojolicious::Plugin::INIConfig';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Config::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use File::Spec::Functions 'file_name_is_absolute';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Mojo::Util qw/encode decode/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '0.05';
9  
10 8 ▶sub register {
Subroutine register: called CC=26 SCAR=32.9
Pod documented
11 8 my ($self, $app, $conf) = @_;
12
13 # Config file
14 8 ▶ my $file = $conf->{file} || $ENV{MOJO_CONFIG};
Condition100%3/3
$conf->{'file'} || $ENV{'MOJO_CONFIG'}
l!l&&r!l&&!r
314
MC/DC100%2/2
$conf->{'file'} || $ENV{'MOJO_CONFIG'}
$conf->{'file'} $ENV{'MOJO_CONFIG'}
Truth table100%3/3
A || B
A= $conf->{'file'}B= $ENV{'MOJO_CONFIG'}
ABresult
1X1
011
000
15 8 ▶ $file ||= $app->moniker . '.' . ($conf->{ext} || 'ini');
Condition75%3/4
$file ||= $app->moniker . '.' . ($conf->{"ext"} || "ini")
l!l
44
$conf->{'ext'} || 'ini'
l!l
04
MC/DC100%1/1
$file ||= $app->moniker . '.' . ($conf->{"ext"} || "ini")
$file
MC/DC0%0/1
$conf->{'ext'} || 'ini'
$conf->{'ext'}
Truth table100%2/2
A
A= $file
Aresult
11
00
Truth table50%1/2
A
A= $conf->{'ext'}
Aresult
11
00
16  
17 # Mode specific config file
18 8 ▶ my $mode = $file =~ /^(.*)\.([^.]+)$/ ? join('.', $1, $app->mode, $2) : '';
Branch: 1 branch
BranchTrueFalseTotal
$file =~ /^(.*)\.([^.]+)$/u ? : 5 3 8
19  
20 8 my $home = $app->home;
21 8 ▶ $file = $home->rel_file($file) unless file_name_is_absolute $file;
Branch: 1 branch
BranchTrueFalseTotal
unless file_name_is_absolute($file) 7 1 8
22 8 ▶ $mode = $home->rel_file($mode) if $mode && !file_name_is_absolute $mode;
Branch: 1 branch
BranchTrueFalseTotal
if $mode and not file_name_is_absolute($mode) 4 4 8
Condition100%3/3
$mode and not file_name_is_absolute($mode)
!ll&&!rl&&r
314
MC/DC100%2/2
$mode and not file_name_is_absolute($mode)
$mode not file_name_is_absolute($mode)
Truth table100%3/3
A and not(B)
A= $modeB= not file_name_is_absolute($mode)
ABresult
0X0
100
111
23 8 ▶ $mode = undef unless $mode && -e $mode;
Branch: 1 branch
BranchTrueFalseTotal
unless $mode and -e $mode 7 1 8
Condition100%3/3
$mode and -e $mode
!ll&&!rl&&r
341
MC/DC100%2/2
$mode and -e $mode
$mode -e $mode
Truth table100%3/3
A and B
A= $modeB= -e $mode
ABresult
0X0
100
111
24  
25 # Read config file
26 8 my $config = {};
27  
28 8 ▶ if (-e $file) {
Branch: 3 branches
BranchTrueFalseTotal
if (-e $file) { } 1 7 8
elsif (exists $conf->{'base_config'} and exists $conf->{'config_files'}) { } 4 3 7
elsif (not $conf->{'default'} || $mode) { } 2 1 3
29 1 $config = $self->load($file, $conf, $app);
30 # check to see if we should overload a base configuration
31 ▶ } elsif( exists $conf->{'base_config'} && exists $conf->{'config_files'} ) {
Condition66%2/3
exists $conf->{"base_config"} and exists $conf->{"config_files"}
!ll&&!rl&&r
304
MC/DC50%1/2
exists $conf->{"base_config"} and exists $conf->{"config_files"}
exists $conf->{"base_config"} exists $conf->{"config_files"}
Truth table66%2/3
A and B
A= exists $conf->{"base_config"}B= exists $conf->{"config_files"}
ABresult
0X0
100
111
32 4 $config = $self->inherit( $conf );
33 # Check for default and mode specific config file
34 ▶ } elsif (!$conf->{default} && !$mode) {
Condition66%2/3
$conf->{'default'} || $mode
l!l&&r!l&&!r
102
MC/DC50%1/2
$conf->{'default'} || $mode
$conf->{'default'} $mode
Truth table66%2/3
A || B
A= $conf->{'default'}B= $mode
ABresult
1X1
011
000
35 2 die qq{Config file "$file" missing, maybe you need to create it?\n};
36 }
37  
38 # Merge everything
39 4 ▶ if ($mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode) 1 3 4
40 1 my $mode_config = $self->load($mode, $conf, $app);
41 1 for my $key (keys %$mode_config) {
42 $config->{$key}
43 1 ▶ = {%{$config->{$key} || {}}, %{$mode_config->{$key} || {}}};
Condition50%2/4
$config->{$key} || {}
l!l
10
$mode_config->{$key} || {}
l!l
10
MC/DC0%0/1
$config->{$key} || {}
$config->{$key}
MC/DC0%0/1
$mode_config->{$key} || {}
$mode_config->{$key}
Truth table50%1/2
A
A= $config->{$key}
Aresult
11
00
Truth table50%1/2
A
A= $mode_config->{$key}
Aresult
11
00
44 }
45 }
46 4 ▶ if ($conf->{default}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{"default"}) 1 3 4
47 1 my $default_config = $conf->{default};
48 1 for my $key (keys %$default_config) {
49 $config->{$key}
50 1 ▶ = {%{$default_config->{$key} || {}}, %{$config->{$key} || {}}, };
Condition50%2/4
$default_config->{$key} || {}
l!l
10
$config->{$key} || {}
l!l
01
MC/DC0%0/1
$default_config->{$key} || {}
$default_config->{$key}
MC/DC0%0/1
$config->{$key} || {}
$config->{$key}
Truth table50%1/2
A
A= $default_config->{$key}
Aresult
11
00
Truth table50%1/2
A
A= $config->{$key}
Aresult
11
00
51 }
52 }
53 4 my $current = $app->defaults(config => $app->config)->config;
54 4 for my $key (keys %$config) {
55 2 %{$current->{$key}}
56 2 ▶ = (%{$current->{$key} || {}}, %{$config->{$key} || {}});
Condition50%2/4
$current->{$key} || {}
l!l
20
$config->{$key} || {}
l!l
20
MC/DC0%0/1
$current->{$key} || {}
$current->{$key}
MC/DC0%0/1
$config->{$key} || {}
$config->{$key}
Truth table50%1/2
A
A= $current->{$key}
Aresult
11
00
Truth table50%1/2
A
A= $config->{$key}
Aresult
11
00
57 }
58  
59 4 return $current;
60 }
61  
62 4 ▶sub inherit {
Subroutine inherit: called CC=10 SCAR=23.1
Pod documented
63 4 my $self = shift;
64 4 my $args = shift;
65 # print STDERR '::inherit() got these $args: ' . Dumper( $args );
66  
67 die 'config_files key expects an ARRAYREF'
68 4 ▶ unless ref $args->{'config_files'} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args->{"config_files"} eq "ARRAY" 1 3 4
69 3 foreach my $ini ( @{$args->{'config_files'}} ){
70 4 ▶ die 'Unable to read ' . Dumper( $ini ) unless -r $ini;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $ini 1 3 4
71 }
72 2 my $cfg_overloaded;
73 2 ▶ if(ref $args->{'base_config'} ne 'HASH'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args->{'base_config'} ne 'HASH') { } 0 2 2
74 0 return;
75 } else {
76 2 my $cfg_overloaded = $args->{'base_config'};
77 2 my $cfg_overload = Config::Tiny->read( $args->{'config_files'}->[0], 'utf8' );
78 # print STDERR '->inherit() says the $cfg_overload is: ' . Dumper $cfg_overload;
79 2 my $stanzas_base_config = _get_stanzas( $args->{'base_config'} );
80 2 my $stanzas_cfg_overload = _get_stanzas( $cfg_overload );
81 # print STDERR '->inherit() says stanza include: ' . Dumper $stanzas;
82 2 push @{ $cfg_overloaded->{'default'}->{'config_files'} }, $args->{'config_files'}->[0];
83 2 foreach my $stanza ( @{$stanzas_base_config}, @{$stanzas_cfg_overload} ){
84 4 my $keys_base_config = _get_keys( $args->{'base_config'}, $stanza );
85 4 my $keys_cfg_overload = _get_keys( $cfg_overload, $stanza );
86 # print STDERR "->inherit() says base configuration's $stanza stanza includes: \n";
87 4 foreach my $key ( @{$keys_base_config}, @{$keys_cfg_overload} ){
88 # print STDERR "\t" . $key . ' => ' . $cfg_overloaded->{$stanza}->{$key} . "\n";
89 6 ▶ next if( $stanza eq 'default' && $key eq 'config_files' );
Branch: 1 branch
BranchTrueFalseTotal
if $stanza eq "default" and $key eq "config_files" 1 5 6
Condition66%2/3
$stanza eq "default" and $key eq "config_files"
!ll&&!rl&&r
501
MC/DC50%1/2
$stanza eq "default" and $key eq "config_files"
$stanza eq "default" $key eq "config_files"
Truth table66%2/3
A and B
A= $stanza eq "default"B= $key eq "config_files"
ABresult
0X0
100
111
90 $cfg_overloaded->{$stanza}->{$key}
91 = exists $cfg_overload->{$stanza}->{$key}
92 ? $cfg_overload->{$stanza}->{$key}
93 5 ▶ : $cfg_overloaded->{$stanza}->{$key}
Branch: 1 branch
BranchTrueFalseTotal
exists $cfg_overload->{$stanza}{$key} ? : 3 2 5
94 }
95 }
96 }
97  
98 2 return $cfg_overloaded;
99 }
100  
101 4 ▶sub _get_stanzas {
Subroutine _get_stanzas: called CC=1 SCAR=0.0
102 4 my $cfg = shift;
103 4 my @stanzas = keys %{$cfg};
104 4 return \@stanzas;
105 }
106  
107 8 ▶sub _get_keys {
Subroutine _get_keys: called CC=3 SCAR=11.0
108 8 my $cfg = shift;
109 8 my $stanza = shift;
110 my @keys = exists $cfg->{$stanza} && (ref $cfg->{$stanza} eq 'HASH' )
111 8 ▶ ? keys %{$cfg->{$stanza}}
Branch: 1 branch
BranchTrueFalseTotal
exists $cfg->{$stanza} && ref $cfg->{$stanza} eq 'HASH' ? : 4 4 8
Condition66%2/3
exists $cfg->{$stanza} and ref $cfg->{$stanza} eq "HASH"
!ll&&!rl&&r
404
MC/DC50%1/2
exists $cfg->{$stanza} and ref $cfg->{$stanza} eq "HASH"
exists $cfg->{$stanza} ref $cfg->{$stanza} eq "HASH"
Truth table66%2/3
A and B
A= exists $cfg->{$stanza}B= ref $cfg->{$stanza} eq "HASH"
ABresult
0X0
100
111
112 : ();
113 8 return \@keys;
114 }
115  
116 2 1;
117  
118 =head1 NAME
119  
120 Mojolicious::Plugin::INIConfig::Extended - Mojolicious Plugin to overload a Configuration
121  
122 =head1 CAUTION
123  
124 B<This module is alpha release. the feature will be changed without warnings.>
125  
126 =head1 SYNOPSIS
127  
128 # myapp.ini
129 [section]
130 foo=bar
131 music_dir=<%= app->home->rel_dir('music') %>
132  
133 # Mojolicious
134 my $config = $self->plugin('INIConfig::Extended');
135  
136 # Mojolicious::Lite
137 my $config = plugin 'INIConfig::Extended';
138  
139 # foo.html.ep
140 %= $config->{section}{foo}
141  
142 # The configuration is available application wide
143 my $config = app->config;
144  
145 # Everything can be customized with options
146 my $config = plugin INIConfig::Extended => {file => '/etc/myapp.conf'};
147  
148 $self->plugin('INIConfig::Extended', {
149 base_config => $self->app->config,
150 config_files => \@config_files });
151  
152 If no $self->app->config already exists, you can provide an empty hashref {} instead
153 and this ought to work, but please see the KNOWN BUGS section below.
154  
155 =head1 DESCRIPTION
156  
157 L<Mojolicious-Plugin-INIConfig-Extended>
158 provides configuration inheritance and overloading
159  
160 L<Mojolicious::Plugin::INIConfig> is a INI configuration plugin that
161 preprocesses its input with L<Mojo::Template>.
162  
163 The application object can be accessed via C<$app> or the C<app> function. You
164 can extend the normal config file C<myapp.ini> with C<mode> specific ones
165 like C<myapp.$mode.ini>. A default configuration filename will be generated
166 from the value of L<Mojolicious/"moniker">.
167  
168 This ::INIConfig::Extended module seeks to do for Mojolicious::Plugin::INIConfig,
169 what my earlier cpan contribution, Config::Simple::Extended
170 did for Config::Simple.
171  
172 The code here barely refactors the INIConfig plugin's ->register method
173 to route to a new ->inherit method when appropriate. I copied over the
174 test suite from ::INIConfig and ::INIConfig::Extended introduces no
175 regression and may be used as a drop in replacement.
176  
177 =head1 OPTIONS
178  
179 L<Mojolicious::Plugin::INIConfig::Extended> inherits all options from
180 L<Mojolicious::Plugin::INIConfig> and supports the following new ones.
181  
182 =head2 base_config
183  
184 # Mojolicious::Lite
185 plugin Config => { base_config => $app->cfg, file => 'conf.d/example.com/site_config.ini' };
186  
187 Overload a base configuration with key->value pairs from an
188 additional configuration file.
189  
190 =head2 config_files
191  
192 # Mojolicious::Lite
193 plugin Config => { config_files => [ qw{ conf.d/base_config.ini conf.d/example.com/site_config.ini ] };
194  
195 Build configuration from an ordered list of configuration files,
196 subsequent ones overloading preceeding ones.
197  
198 =head1 METHODS
199  
200 L<Mojolicious::Plugin::INIConfig::Extended> inherits all methods from
201 L<Mojolicious::Plugin::INIConfig> and implements the following new ones.
202  
203 =head2 inherit
204  
205 ## $plugin->inherit($content, $file, $conf, $app); <-- UNTESTED
206  
207 $self->plugin('INIConfig::Extended', {
208 base_config => $self->app->config,
209 config_files => \@config_files });
210  
211  
212 Overload a Config::Tiny configuration, return it as $app->cfg
213  
214 =head1 BACKWARDS COMPATIBILITY POLICY
215  
216 At least for now, in its early stages of development,
217 this module should be considered experimental.
218 EXPERIMENTAL features may be changed without warnings.
219  
220 =head1 KNOWN BUGS
221  
222 For the moment, as currently implemented, the ->inherit method, although
223 it expects both a base_config (hash ref) and a config_files (array ref),
224 and its design anticipates in the future processing that array of config files
225 to overload the configuration; it currently only processes the first ini file
226 in that array. All other config files will be ignored.
227  
228 Patches with tests are welcome in the form of a Pull Request. Or with
229 patience I will soon enough encounter a use case which should make me
230 return to this project and to complete the implementation of its original
231 design. For the moment, though, this serves my immediate needs. For clues
232 on how to invoke the ->inherit method to overcome this limitation please
233 see `perldoc Config::Simple::Extended`.
234  
235 =head1 BUGS
236  
237 Please tell me bugs if you find bug.
238  
239 C<< <hesco at yourmessagedelivered.com> >>
240  
241 L<http://github.com/yuki-kimoto/Mojolicious-Plugin-INIConfig>
242 L<http://github.com/hesco/Mojolicious-Plugin-INIConfig-Extended>
243  
244 =head1 COPYRIGHT & LICENSE
245  
246 Copyright 2015 Hugh Esco and YMD Partners LLC, all rights reserved.
247  
248 with appreciation to the original author for their work:
249 Copyright 2013 Yuki Kimoto, all rights reserved.
250  
251 This program is free software; you can redistribute it and/or modify it
252 under the same terms as Perl itself.
253  
254 =cut
255  
↑ Summary