Acrux/Config.pm

filter: Statementstmt 77.6% 104 / 134 Branchbran 38.8% 28 / 72 Conditioncond 38.4% 15 / 39 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 80.7% 21 / 26 Podpod 100% 16 / 16 totaltotal 59.7% 184 / 308 CC 54 SCAR 34.1
CC
54
Cov
60%
new
83.7
first
10.5
latest
10.5
CRAP 30.2
« Acrux/Log.pm ↑ Summary Acrux/Digest.pm »
1 package Acrux::Config;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =encoding utf8
6  
7 =head1 NAME
8  
9 Acrux::Config - Config::General Configuration of Acrux
10  
11 =head1 SYNOPSIS
12  
13 use Acrux::Config;
14  
15 my $config = Acrux::Config->new(
16 file => '/etc/myapp.conf',
17 );
18 say $config->get('foo');
19  
20 =head1 DESCRIPTION
21  
22 The module works with the configuration using L<Config::General>
23  
24 All getters of this class are allows get access to configuration parameters by path-pointers.
25 See L<Acrux::Pointer> and L<RFC 6901|https://tools.ietf.org/html/rfc6901>
26  
27 =head2 new
28  
29 my $config = Acrux::Config->new(
30 file => '/etc/myapp.conf',
31 default => {foo => 'bar'},
32 );
33  
34 =head1 ATTRIBUTES
35  
36 This plugin supports the following attributes
37  
38 =head2 default
39  
40 default => {foo => 'bar'}
41  
42 Default configuration data
43  
44 =head2 dirs
45  
46 dirs => ['/etc/foo', '/etc/bar']
47  
48 Paths to additional directories of config files
49  
50 =head2 file
51  
52 file => '/etc/foo.stuff'
53  
54 Path to configuration file, absolute or relative, defaults to the value of the
55 C<$0.conf> in the current directory
56  
57 =head2 noload
58  
59 noload => 1
60  
61 This attribute disables loading config file
62  
63 =head2 options
64  
65 options => {'-AutoTrue' => 0}
66  
67 Sets the L<Config::General> options directly
68  
69 =head2 root
70  
71 root => '/etc/myapp'
72  
73 Sets the root directory to configuration files and directories location
74  
75 =head1 METHODS
76  
77 This plugin implements the following methods
78  
79 =head2 array, list
80  
81 dumper $config->array('/foo'); # ['first', 'second', 'third']
82 # ['first', 'second', 'third']
83 dumper $config->array('/foo'); # 'value'
84 # ['value']
85  
86 Returns an array of found values from configuration
87  
88 =head2 config, conf
89  
90 my $config_hash = $config->config; # { ... }
91  
92 This method returns config structure directly as hash ref
93  
94 =head2 error
95  
96 my $error = $config->error;
97  
98 Returns error string if occurred any errors while creating the object or reading the configuration file
99  
100 =head2 first
101  
102 say $config->first('/foo'); # ['first', 'second', 'third']
103 # first
104  
105 Returns an first value of found values from configuration
106  
107 =head2 get
108  
109 say $config->get('/datadir');
110  
111 Returns configuration value by path
112  
113 =head2 hash, object
114  
115 dumper $config->hash('/foo'); # { foo => 'first', bar => 'second' }
116 # { foo => 'first', bar => 'second' }
117  
118 Returns an hash of found values from configuration
119  
120 =head2 latest
121  
122 say $config->latest('/foo'); # ['first', 'second', 'third']
123 # third
124  
125 Returns an latest value of found values from configuration
126  
127 =head2 load
128  
129 my $config = $config->load;
130  
131 Loading config files
132  
133 =head2 pointer
134  
135 my $pointer = $config->pointer;
136  
137 Returns current L<Acrux::Pointer> object
138  
139 =head1 HISTORY
140  
141 See C<Changes> file
142  
143 =head1 TO DO
144  
145 See C<TODO> file
146  
147 =head1 SEE ALSO
148  
149 L<Config::General>, L<Acrux::Pointer>
150  
151 =head1 AUTHOR
152  
153 Serż Minus (Sergey Lepenkov) L<https://www.serzik.com> E<lt>abalama@cpan.orgE<gt>
154  
155 =head1 COPYRIGHT
156  
157 Copyright (C) 1998-2026 D&D Corporation
158  
159 =head1 LICENSE
160  
161 This program is distributed under the terms of the Artistic License Version 2.0
162  
163 See the C<LICENSE> file or L<https://opensource.org/license/artistic-2-0> for details
164  
165 =cut
166  
167 1 ▶use Config::General qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
168 1 ▶use Cwd qw/getcwd/;
Subroutine BEGIN: called CC=1 SCAR=0.0
169 1 ▶use File::Spec qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
170 1 ▶use File::Basename qw/basename/;
Subroutine BEGIN: called CC=1 SCAR=0.0
171 1 ▶use Acrux::Pointer;
Subroutine BEGIN: called CC=1 SCAR=0.0
172 1 ▶use Acrux::RefUtil qw/as_array is_array_ref is_hash_ref is_value/;
Subroutine BEGIN: called CC=1 SCAR=0.0
173 1 ▶use Acrux::Util qw/clone/;
Subroutine BEGIN: called CC=1 SCAR=0.0
174  
175 1 ▶use constant DEFAULT_CG_OPTS => {
Subroutine BEGIN: called CC=1 SCAR=0.0
176 '-ApacheCompatible' => 1, # Makes possible to tweak all options in a way that Apache configs can be parsed
177 '-LowerCaseNames' => 1, # All options found in the config will be converted to lowercase
178 '-UTF8' => 1, # All files will be opened in utf8 mode
179 '-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
180 1 };
181  
182 1 ▶sub new {
Subroutine new: called CC=25 SCAR=44.3
Pod documented
183 1 my $class = shift;
184 0 ▶ my $args = @_ ? @_ > 1 ? {@_} : {%{$_[0]}} : {};
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 1 0 1
@_ > 1 ? : 1 0 1
185 my $self = bless {
186 default => $args->{defaults} || $args->{default} || {},
187 file => $args->{file} // '',
188 root => $args->{root} // '', # base path to default files/directories
189 dirs => $args->{dirs} || [],
190 noload => $args->{noload} || 0,
191 options => {},
192 error => '',
193 config => {},
194 pointer => Acrux::Pointer->new,
195 files => [],
196 orig => $args->{options} || $args->{opts} || {},
197 1 ▶ }, $class;
Condition44%8/18
$args->{'defaults'} || $args->{'default'} || {}
l!l
01
$args->{"defaults"} or $args->{"default"}
l!l&&r!l&&!r
001
$args->{"file"} // ""
l!l
10
$args->{"root"} // ""
l!l
01
$args->{'dirs'} || []
l!l
01
$args->{'noload'} || 0
l!l
01
$args->{'options'} || $args->{'opts'} || {}
l!l
01
$args->{"options"} or $args->{"opts"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$args->{'defaults'} || $args->{'default'} || {}
$args->{"defaults"} $args->{"default"}
MC/DC0%0/1
$args->{"file"} // ""
$args->{"file"}
MC/DC0%0/1
$args->{"root"} // ""
$args->{"root"}
MC/DC0%0/1
$args->{'dirs'} || []
$args->{'dirs'}
MC/DC0%0/1
$args->{'noload'} || 0
$args->{'noload'}
MC/DC0%0/2
$args->{'options'} || $args->{'opts'} || {}
$args->{"options"} $args->{"opts"}
Truth table33%1/3
A or B
A= $args->{"defaults"}B= $args->{"default"}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $args->{"file"}
Aresult
11
00
Truth table50%1/2
A
A= $args->{"root"}
Aresult
11
00
Truth table50%1/2
A
A= $args->{'dirs'}
Aresult
11
00
Truth table50%1/2
A
A= $args->{'noload'}
Aresult
11
00
Truth table33%1/3
A or B
A= $args->{"options"}B= $args->{"opts"}
ABresult
1X1
011
000
198 1 ▶ my $myroot = length($self->{root}) ? $self->{root} : getcwd();
Branch: 1 branch
BranchTrueFalseTotal
length $self->{'root'} ? : 0 1 1
199  
200 # Set dirs
201 1 my @dirs = ();
202 1 foreach my $dir (as_array($self->{dirs})) {
203 0 ▶ unless (File::Spec->file_name_is_absolute($dir)) { # rel
Branch: 1 branch
BranchTrueFalseTotal
unless ("File::Spec"->file_name_is_absolute($dir)) 0 0 0
204 0 ▶ $dir = length($myroot)
Branch: 1 branch
BranchTrueFalseTotal
length $myroot ? : 0 0 0
205 ? File::Spec->rel2abs($dir, $myroot)
206 : File::Spec->rel2abs($dir);
207 }
208 0 ▶ push @dirs, $dir if -e $dir;
Branch: 1 branch
BranchTrueFalseTotal
if -e $dir 0 0 0
209 }
210 1 $self->{dirs} = [@dirs];
211  
212 # Set config file
213 1 my $file = $self->{file};
214 1 ▶ $file = sprintf("%s.conf", basename($0)) unless length $file;
Branch: 1 branch
BranchTrueFalseTotal
unless length $file 0 1 1
215 1 ▶ unless (File::Spec->file_name_is_absolute($file)) { # rel
Branch: 1 branch
BranchTrueFalseTotal
unless ("File::Spec"->file_name_is_absolute($file)) 1 0 1
216 1 ▶ $file = length($myroot)
Branch: 1 branch
BranchTrueFalseTotal
length $myroot ? : 1 0 1
217 ? File::Spec->rel2abs($file, $myroot)
218 : File::Spec->rel2abs($file);
219 }
220 1 $self->{file} = $file;
221 1 ▶ unless ($self->{noload}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"noload"}) 1 0 1
222 1 ▶ unless (-r $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $file) 0 1 1
223 0 $self->{error} = sprintf("Configuration file \"%s\" not found or unreadable", $file);
224 0 return $self;
225 }
226 }
227  
228 # Config::General Options
229 1 my $orig = $self->{orig};
230 1 ▶ $orig = {} unless is_hash_ref($orig);
Branch: 1 branch
BranchTrueFalseTotal
unless is_hash_ref($orig) 0 1 1
231 1 my %options = (%{DEFAULT_CG_OPTS()}, %$orig); # Merge everything
232 1 $options{'-ConfigFile'} = $file;
233 1 ▶ $options{"-ConfigPath"} ||= [@dirs] if scalar(@dirs);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @dirs 0 1 1
Condition0%0/2
$options{"-ConfigPath"} ||= [@dirs]
l!l
00
MC/DC0%0/1
$options{"-ConfigPath"} ||= [@dirs]
$options{"-ConfigPath"}
Truth table0%0/2
A
A= $options{"-ConfigPath"}
Aresult
11
00
234 1 $self->{options} = {%options};
235  
236 # Load
237 1 ▶ return $self if $self->{noload};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"noload"} 0 1 1
238 1 return $self->load;
239 }
240 1 ▶sub default {
Subroutine default: called CC=2 SCAR=8.4
Pod documented
241 1 my $self = shift;
242 1 ▶ if (scalar(@_) >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ >= 1) 0 1 1
243 0 $self->{default} = shift;
244 0 return $self;
245 }
246 1 return $self->{default};
247 }
248 1 ▶sub error {
Subroutine error: called CC=2 SCAR=8.4
Pod documented
249 1 my $self = shift;
250 1 ▶ if (scalar(@_) >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ >= 1) 0 1 1
251 0 $self->{error} = shift;
252 0 return $self;
253 }
254 1 return $self->{error};
255 }
256 0 ▶sub file {
Subroutine file: not called CC=2 SCAR=17.9
Pod documented
257 0 my $self = shift;
258 0 ▶ if (scalar(@_) >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ >= 1) 0 0 0
259 0 $self->{file} = shift;
260 0 return $self;
261 }
262 0 return $self->{file};
263 }
264 0 ▶sub dirs {
Subroutine dirs: not called CC=2 SCAR=17.9
Pod documented
265 0 my $self = shift;
266 0 ▶ if (scalar(@_) >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ >= 1) 0 0 0
267 0 $self->{dirs} = shift;
268 0 return $self;
269 }
270 0 return $self->{dirs};
271 }
272 10 ▶sub pointer { shift->{pointer} }
Subroutine pointer: called CC=1 SCAR=0.0
Pod documented
273 1 ▶sub load {
Subroutine load: called CC=6 SCAR=18.3
Pod documented
274 1 my $self = shift;
275 1 my $opts = $self->{options};
276 1 $self->{error} = "";
277  
278 # Load
279 1 my $cfg = eval { Config::General->new(%$opts) };
280 1 ▶ return $self->error(sprintf("Can't load configuration from file \"%s\": %s", $self->file, $@)) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
281 1 ▶ return $self->error(sprintf("Configuration file \"%s\" did not return a Config::General object", $self->file))
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cfg eq "Config::General" 0 1 1
282 unless ref $cfg eq 'Config::General';
283 1 my %config = $cfg->getall;
284 1 my @files = $cfg->files;
285  
286 # Merge defaults
287 1 ▶ my $defaults = $self->default || {};
Condition50%1/2
$self->default || {}
l!l
10
MC/DC0%0/1
$self->default || {}
$self->default
Truth table50%1/2
A
A= $self->default
Aresult
11
00
288 1 ▶ %config = (%$defaults, %config) if is_hash_ref($defaults) && scalar keys %$defaults;
Branch: 1 branch
BranchTrueFalseTotal
if is_hash_ref($defaults) and scalar keys %$defaults 0 1 1
Condition50%1/2
is_hash_ref($defaults) and scalar keys %$defaults
!ll
01
MC/DC0%0/1
is_hash_ref($defaults) and scalar keys %$defaults
is_hash_ref($defaults)
Truth table50%1/2
A
A= is_hash_ref($defaults)
Aresult
00
11
289  
290 # Add system values
291 1 $config{'_config_files'} = [@files];
292 1 $config{'_config_loaded'} = scalar @files;
293  
294 # Set config data
295 1 $self->{config} = {%config}; # hash data
296 1 $self->pointer->data(clone($self->{config}));
297  
298 1 return $self;
299 }
300 2 ▶sub config {
Subroutine config: called CC=4 SCAR=15.2
Pod documented
301 2 my $self = shift;
302 2 my $key = shift;
303 2 ▶ return undef unless $self->{config};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"config"} 0 2 2
304 2 ▶ return $self->{config} unless defined $key and length $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key and length $key 0 2 2
Condition33%1/3
defined $key and length $key
!ll&&!rl&&r
002
MC/DC0%0/2
defined $key and length $key
defined $key length $key
Truth table33%1/3
A and B
A= defined $keyB= length $key
ABresult
0X0
100
111
305 2 return $self->{config}->{$key};
306 }
307 0 ▶sub conf { goto &config }
Subroutine conf: not called CC=1 SCAR=6.9
Pod documented
308 5 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod documented
309 5 my $self = shift;
310 5 my $key = shift;
311 5 return $self->pointer->get($key);
312 }
313 1 ▶sub first {
Subroutine first: called CC=6 SCAR=23.5
Pod documented
314 1 my $self = shift;
315 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
316 1 my $node = $self->pointer->get($_[0]);
317 1 ▶ if (is_array_ref($node)) { # Array ref
Branch: 2 branches
BranchTrueFalseTotal
if (is_array_ref($node)) { } 1 0 1
elsif (is_value($node)) { } 0 0 0
318 1 ▶ return exists($node->[0]) ? $node->[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $node->[0] ? : 1 0 1
319 } elsif (is_value($node)) { # Scalar value
320 0 return $node;
321 }
322 0 return undef;
323 }
324 1 ▶sub latest {
Subroutine latest: called CC=6 SCAR=23.5
Pod documented
325 1 my $self = shift;
326 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
327 1 my $node = $self->pointer->get($_[0]);
328 1 ▶ if (is_array_ref($node)) { # Array ref
Branch: 2 branches
BranchTrueFalseTotal
if (is_array_ref($node)) { } 1 0 1
elsif (is_value($node)) { } 0 0 0
329 1 ▶ return exists($node->[0]) ? $node->[-1] : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $node->[0] ? : 1 0 1
330 } elsif (is_value($node)) { # Scalar value
331 0 return $node;
332 }
333 0 return undef;
334 }
335 1 ▶sub array {
Subroutine array: called CC=5 SCAR=20.9
Pod documented
336 1 my $self = shift;
337 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
338 1 my $node = $self->pointer->get($_[0]);
339 1 ▶ if (is_array_ref($node)) { # Array ref
Branch: 2 branches
BranchTrueFalseTotal
if (is_array_ref($node)) { } 1 0 1
elsif (defined $node) { } 0 0 0
340 1 return $node;
341 } elsif (defined($node)) {
342 0 return [$node];
343 }
344 0 return [];
345 }
346 0 ▶sub list { goto &array }
Subroutine list: not called CC=1 SCAR=6.9
Pod documented
347 1 ▶sub hash {
Subroutine hash: called CC=4 SCAR=16.4
Pod documented
348 1 my $self = shift;
349 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
350 1 my $node = $self->pointer->get($_[0]);
351 1 ▶ return $node if is_hash_ref($node);
Branch: 1 branch
BranchTrueFalseTotal
if is_hash_ref($node) 1 0 1
352 0 return {};
353 }
354 0 ▶sub object { goto &hash }
Subroutine object: not called CC=1 SCAR=6.9
Pod documented
355  
356 1 1;
357  
358 __END__
« Acrux/Log.pm ↑ Summary Acrux/Digest.pm »