blib/lib/Mojolicious/Plugin/ConfigGeneral.pm

filter: Statementstmt 92.0% 81 / 88 Branchbran 42.8% 18 / 42 Conditioncond 32.0% 16 / 50 MC/DCmcdc 0.0% 0 / 30 Subroutinesub 100% 12 / 12 Podpod 100% 1 / 1 totaltotal 57.4% 128 / 223 CC 40 SCAR 30.3
CC
40
Cov
64%
register
36.0
__ANON__
20.7
__ANON__
20.7
CRAP 20.7
↑ Summary
1 package Mojolicious::Plugin::ConfigGeneral;
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Mojolicious::Plugin::ConfigGeneral - Config::General Configuration Plugin for Mojolicious
8  
9 =head1 SYNOPSIS
10  
11 use Mojolicious::Plugin::ConfigGeneral;
12  
13 # Mojolicious
14 my $config = $app->plugin('ConfigGeneral');
15 say $config->{foo};
16  
17 # Mojolicious::Lite
18 my $config = plugin 'ConfigGeneral';
19 say $config->{foo};
20  
21 # The configuration is available application-wide
22 my $config = app->config;
23 say $config->{foo};
24  
25 # Everything can be customized with options
26 my $config = plugin ConfigGeneral => {file => '/etc/myapp.conf'};
27  
28 =head1 DESCRIPTION
29  
30 Mojolicious::Plugin::ConfigGeneral is a L<Config::General> Configuration Plugin for Mojolicious
31  
32 =head1 OPTIONS
33  
34 This plugin supports the following options
35  
36 =head2 defaults
37  
38 # Mojolicious::Lite
39 plugin ConfigGeneral => {defaults => {foo => 'bar'}};
40  
41 Default configuration
42  
43 =head2 file
44  
45 # Mojolicious::Lite
46 plugin ConfigGeneral => {file => 'myapp.conf'};
47 plugin ConfigGeneral => {file => '/etc/foo.stuff'};
48  
49 Path to configuration file, absolute or relative to the application home directory, defaults to the value of the
50 C<MOJO_CONFIG> environment variable or C<$moniker.conf> in the application home directory.
51  
52 =head2 noload
53  
54 plugin ConfigGeneral => {noload => 1};
55  
56 This option disables loading config file
57  
58 =head2 options
59  
60 # Mojolicious::Lite
61 plugin ConfigGeneral => {options => {'-AutoTrue' => 0}};
62 plugin ConfigGeneral => {options => {'-AutoTrue' => 0}};
63  
64 Sets the L<Config::General> options directly
65  
66 =head1 METHODS
67  
68 This plugin inherits all methods from L<Mojolicious::Plugin> and implements the following new ones
69  
70 =head2 register
71  
72 my $config = $plugin->register(Mojolicious->new);
73 my $config = $plugin->register(Mojolicious->new, {file => '/etc/foo.conf'});
74  
75 Register plugin in L<Mojolicious> application and set configuration.
76  
77 =head1 HELPERS
78  
79 All helpers of this plugin are allows get access to configuration parameters by path-pointers.
80 See L<Mojo::JSON::Pointer> and L<RFC 6901|https://tools.ietf.org/html/rfc6901>
81  
82 =over 8
83  
84 =item conf-E<gt>get
85  
86 say $self->conf->get('/datadir');
87  
88 Returns configuration value by path
89  
90 =item conf-E<gt>first
91  
92 dumper $self->conf->first('/foo'); # ['first', 'second', 'third']
93 # 'first'
94  
95 Returns an first value of found values from configuration
96  
97 =item conf-E<gt>latest
98  
99 dumper $self->conf->latest('/foo'); # ['first', 'second', 'third']
100 # 'third'
101  
102 Returns an latest value of found values from configuration
103  
104 =item conf-E<gt>array, conf-E<gt>list
105  
106 dumper $self->conf->array('/foo'); # ['first', 'second', 'third']
107 # ['first', 'second', 'third']
108 dumper $self->conf->array('/foo'); # 'value'
109 # ['value']
110  
111 Returns an array of found values from configuration
112  
113 =item conf-E<gt>hash, conf-E<gt>object
114  
115 dumper $self->conf->array('/foo'); # { foo => 'first', bar => 'second' }
116 # { foo => 'first', bar => 'second' }
117  
118 Returns an hash of found values from configuration
119  
120 =back
121  
122 =head1 HISTORY
123  
124 See C<Changes> file
125  
126 =head1 TO DO
127  
128 See C<TODO> file
129  
130 =head1 SEE ALSO
131  
132 L<Mojolicious::Plugin::Config>, L<Config::General>
133  
134 =head1 AUTHOR
135  
136 Serż Minus (Sergey Lepenkov) L<https://www.serzik.com> E<lt>abalama@cpan.orgE<gt>
137  
138 =head1 COPYRIGHT
139  
140 Copyright (C) 1998-2026 D&D Corporation
141  
142 =head1 LICENSE
143  
144 This program is distributed under the terms of the Artistic License Version 2.0
145  
146 See the C<LICENSE> file or L<https://opensource.org/license/artistic-2-0> for details
147  
148 =cut
149  
150 2 our $VERSION = '1.05';
151  
152 2 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
153 2 ▶use Config::General qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
154 2 ▶use Mojo::File qw/path/;
Subroutine BEGIN: called CC=1 SCAR=0.0
155 2 ▶use Mojo::JSON::Pointer qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
156  
157 2 ▶use constant DEFAULT_CG_OPTS => {
Subroutine BEGIN: called CC=1 SCAR=0.0
158 '-ApacheCompatible' => 1, # Makes possible to tweak all options in a way that Apache configs can be parsed
159 '-LowerCaseNames' => 1, # All options found in the config will be converted to lowercase
160 '-UTF8' => 1, # All files will be opened in utf8 mode
161 '-AutoTrue' => 1, # All options in your config file, whose values are set to true or false values, will be normalised to 1 or 0 respectively
162 2 };
163  
164 1 ▶has 'config_pointer' => sub { Mojo::JSON::Pointer->new };
Subroutine __ANON__: called CC=1 SCAR=0.0
165  
166 1 ▶sub register {
Subroutine register: called CC=17 SCAR=35.8
Pod documented
167 1 my ($self, $app, $args) = @_;
168  
169 # NoLoad
170 1 ▶ my $noload = $args->{noload} || 0;
Condition50%1/2
$args->{'noload'} || 0
l!l
01
MC/DC0%0/1
$args->{'noload'} || 0
$args->{'noload'}
Truth table50%1/2
A
A= $args->{'noload'}
Aresult
11
00
171  
172 # Config file
173 1 ▶ my $file = $args->{file} || $ENV{MOJO_CONFIG};
Condition33%1/3
$args->{'file'} || $ENV{'MOJO_CONFIG'}
l!l&&r!l&&!r
100
MC/DC0%0/2
$args->{'file'} || $ENV{'MOJO_CONFIG'}
$args->{'file'} $ENV{'MOJO_CONFIG'}
Truth table33%1/3
A || B
A= $args->{'file'}B= $ENV{'MOJO_CONFIG'}
ABresult
1X1
011
000
174 1 ▶ $file ||= $app->home->child($app->moniker . '.conf'); # Relative to the home directory by default
Condition33%1/3
$file ||= $app->home->child($app->moniker . '.conf')
l!l&&r!l&&!r
100
MC/DC0%0/2
$file ||= $app->home->child($app->moniker . '.conf')
$file $app->home->child($app->moniker . '.conf')
Truth table33%1/3
A ||= B
A= $fileB= $app->home->child($app->moniker . '.conf')
ABresult
1X1
011
000
175 1 ▶ unless ($noload) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($noload) 1 0 1
176 # Configuration file not found
177 1 ▶ $noload = 1 unless -r $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $file 0 1 1
178 }
179  
180 # Config::General Options
181 1 ▶ my $opts = $args->{options} || $args->{opts} || {};
Condition40%2/5
$args->{'options'} || $args->{'opts'} || {}
l!l
01
$args->{"options"} or $args->{"opts"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$args->{'options'} || $args->{'opts'} || {}
$args->{"options"} $args->{"opts"}
Truth table33%1/3
A or B
A= $args->{"options"}B= $args->{"opts"}
ABresult
1X1
011
000
182 1 ▶ $opts = {} unless ref($opts) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opts eq "HASH" 0 1 1
183 1 my %options = (%{DEFAULT_CG_OPTS()}, %$opts); # Merge everything
184 1 $options{'-ConfigFile'} = $file;
185  
186 # Load
187 1 my %config = ();
188 1 my @files = ();
189 1 ▶ unless ($noload) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($noload) 1 0 1
190 1 my $cfg = eval { Config::General->new(%options) };
191 1 ▶ die sprintf("Can't load configuration from file \"%s\": %s", $file, $@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
192 1 ▶ die sprintf("Configuration file \"%s\" did not return a Config::General object", $file)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cfg eq "Config::General" 0 1 1
193 unless ref $cfg eq 'Config::General';
194 1 %config = $cfg->getall;
195 1 @files = $cfg->files;
196 }
197  
198 # Merge defaults
199 1 ▶ my $defaults = $args->{defaults} || $args->{default} || {};
Condition40%2/5
$args->{'defaults'} || $args->{'default'} || {}
l!l
01
$args->{"defaults"} or $args->{"default"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$args->{'defaults'} || $args->{'default'} || {}
$args->{"defaults"} $args->{"default"}
Truth table33%1/3
A or B
A= $args->{"defaults"}B= $args->{"default"}
ABresult
1X1
011
000
200 1 ▶ %config = (%$defaults, %config) if (ref($defaults) eq 'HASH') && scalar keys %$defaults;
Branch: 1 branch
BranchTrueFalseTotal
if ref $defaults eq "HASH" and scalar keys %$defaults 0 1 1
Condition50%1/2
ref $defaults eq "HASH" and scalar keys %$defaults
!ll
01
MC/DC0%0/1
ref $defaults eq "HASH" and scalar keys %$defaults
ref $defaults eq "HASH"
Truth table50%1/2
A
A= ref $defaults eq "HASH"
Aresult
00
11
201  
202 # Add system values
203 1 $config{'_config_files'} = [@files];
204 1 $config{'_config_loaded'} = scalar @files;
205  
206 # Set config data
207 1 $app->config(\%config);
208 1 $self->config_pointer->data(\%config);
209  
210 # Helpers
211 1 ▶ my $_conf_get = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
212 1 my $this = shift;
213 1 my $key = shift;
214 1 return $self->config_pointer->get($key);
215 1 };
216 1 ▶ my $_conf_fisrt = sub {
Subroutine __ANON__: called CC=8 SCAR=30.3
217 1 my $this = shift;
218 1 ▶ return undef unless defined($_[0]) && length($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] and length $_[0] 0 1 1
Condition33%1/3
defined $_[0] and length $_[0]
!ll&&!rl&&r
001
MC/DC0%0/2
defined $_[0] and length $_[0]
defined $_[0] length $_[0]
Truth table33%1/3
A and B
A= defined $_[0]B= length $_[0]
ABresult
0X0
100
111
219 1 my $node = $self->config_pointer->get($_[0]);
220 1 ▶ if ($node && ref($node) eq 'ARRAY') { # Array
Branch: 2 branches
BranchTrueFalseTotal
if ($node and ref $node eq 'ARRAY') { } 1 0 1
elsif (defined $node and not ref $node) { } 0 0 0
Condition33%1/3
$node and ref $node eq "ARRAY"
!ll&&!rl&&r
001
MC/DC0%0/2
$node and ref $node eq "ARRAY"
$node ref $node eq "ARRAY"
Truth table33%1/3
A and B
A= $nodeB= ref $node eq "ARRAY"
ABresult
0X0
100
111
221 1 ▶ return exists($node->[0]) ? $node->[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $node->[0] ? : 1 0 1
222 ▶ } elsif (defined($node) && !ref($node)) { # Scalar
Condition0%0/3
defined $node and not ref $node
!ll&&!rl&&r
000
MC/DC0%0/2
defined $node and not ref $node
defined $node not ref $node
Truth table0%0/3
A and not(B)
A= defined $nodeB= not ref $node
ABresult
0X0
100
111
223 0 return $node;
224 }
225 0 return undef;
226 1 };
227 1 ▶ my $_conf_latest = sub {
Subroutine __ANON__: called CC=8 SCAR=30.3
228 1 my $this = shift;
229 1 ▶ return undef unless defined($_[0]) && length($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] and length $_[0] 0 1 1
Condition33%1/3
defined $_[0] and length $_[0]
!ll&&!rl&&r
001
MC/DC0%0/2
defined $_[0] and length $_[0]
defined $_[0] length $_[0]
Truth table33%1/3
A and B
A= defined $_[0]B= length $_[0]
ABresult
0X0
100
111
230 1 my $node = $self->config_pointer->get($_[0]);
231 1 ▶ if ($node && ref($node) eq 'ARRAY') { # Array
Branch: 2 branches
BranchTrueFalseTotal
if ($node and ref $node eq 'ARRAY') { } 1 0 1
elsif (defined $node and not ref $node) { } 0 0 0
Condition33%1/3
$node and ref $node eq "ARRAY"
!ll&&!rl&&r
001
MC/DC0%0/2
$node and ref $node eq "ARRAY"
$node ref $node eq "ARRAY"
Truth table33%1/3
A and B
A= $nodeB= ref $node eq "ARRAY"
ABresult
0X0
100
111
232 1 ▶ return exists($node->[0]) ? $node->[-1] : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $node->[0] ? : 1 0 1
233 ▶ } elsif (defined($node) && !ref($node)) { # Scalar
Condition0%0/3
defined $node and not ref $node
!ll&&!rl&&r
000
MC/DC0%0/2
defined $node and not ref $node
defined $node not ref $node
Truth table0%0/3
A and not(B)
A= defined $nodeB= not ref $node
ABresult
0X0
100
111
234 0 return $node;
235 }
236 0 return undef;
237 1 };
238 1 ▶ my $_conf_array = sub {
Subroutine __ANON__: called CC=6 SCAR=24.2
239 1 my $this = shift;
240 1 ▶ return undef unless defined($_[0]) && length($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] and length $_[0] 0 1 1
Condition33%1/3
defined $_[0] and length $_[0]
!ll&&!rl&&r
001
MC/DC0%0/2
defined $_[0] and length $_[0]
defined $_[0] length $_[0]
Truth table33%1/3
A and B
A= defined $_[0]B= length $_[0]
ABresult
0X0
100
111
241 1 my $node = $self->config_pointer->get($_[0]);
242 1 ▶ if ($node && ref($node) eq 'ARRAY') { # Array
Branch: 2 branches
BranchTrueFalseTotal
if ($node and ref $node eq 'ARRAY') { } 1 0 1
elsif (defined $node) { } 0 0 0
Condition33%1/3
$node and ref $node eq "ARRAY"
!ll&&!rl&&r
001
MC/DC0%0/2
$node and ref $node eq "ARRAY"
$node ref $node eq "ARRAY"
Truth table33%1/3
A and B
A= $nodeB= ref $node eq "ARRAY"
ABresult
0X0
100
111
243 1 return $node;
244 } elsif (defined($node)) {
245 0 return [$node];
246 }
247 0 return [];
248 1 };
249 1 ▶ my $_conf_hash = sub {
Subroutine __ANON__: called CC=5 SCAR=19.6
250 1 my $this = shift;
251 1 ▶ return undef unless defined($_[0]) && length($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] and length $_[0] 0 1 1
Condition33%1/3
defined $_[0] and length $_[0]
!ll&&!rl&&r
001
MC/DC0%0/2
defined $_[0] and length $_[0]
defined $_[0] length $_[0]
Truth table33%1/3
A and B
A= defined $_[0]B= length $_[0]
ABresult
0X0
100
111
252 1 my $node = $self->config_pointer->get($_[0]);
253 1 ▶ if ($node && ref($node) eq 'HASH') { # Hash
Branch: 1 branch
BranchTrueFalseTotal
if ($node and ref $node eq "HASH") 1 0 1
Condition33%1/3
$node and ref $node eq "HASH"
!ll&&!rl&&r
001
MC/DC0%0/2
$node and ref $node eq "HASH"
$node ref $node eq "HASH"
Truth table33%1/3
A and B
A= $nodeB= ref $node eq "HASH"
ABresult
0X0
100
111
254 1 return $node;
255 }
256 0 return {};
257 1 };
258  
259 # Set conf helpers
260 1 $app->helper('conf.get' => $_conf_get);
261 1 $app->helper('conf.first' => $_conf_fisrt);
262 1 $app->helper('conf.latest' => $_conf_latest);
263 1 $app->helper('conf.array' => $_conf_array);
264 1 $app->helper('conf.list' => $_conf_array);
265 1 $app->helper('conf.hash' => $_conf_hash);
266 1 $app->helper('conf.object' => $_conf_hash);
267  
268 # Return
269 1 ▶ return wantarray ? (%config) : \%config;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
270 }
271  
272 2 1;
273  
274 __END__
↑ Summary