MToken/Config.pm

filter: Statementstmt 29.1% 35 / 120 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 45.8% 11 / 24 Podpod 100% 7 / 7 totaltotal 22.7% 53 / 233 SCAR 63.0
CC
31
Cov
19%
_loadconfig
240.0
new
72.0
_saveconfig
20.0
CRAP 545.2
« MToken/Server.pm ↑ Summary MToken/Util.pm »
1 package MToken::Config; # $Id: Config.pm 57 2019-06-06 13:46:47Z minus $
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 utf-8
6  
7 =head1 NAME
8  
9 MToken::Config - MToken global and local configuration
10  
11 =head1 VERSION
12  
13 Version 1.01
14  
15 =head1 SYNOPSIS
16  
17 use MToken::Config;
18  
19 my $config = new MToken::Config(
20 global_file => '/foo/bar/global.conf',
21 local_file => '/foo/bar/local.conf',
22 name => 'foo', # Optional
23 );
24  
25 my $foo = $config->get('foo');
26  
27 my $foo = $config->set('foo' => 'bar');
28  
29 my $status = $config->save(); # Local file only
30  
31 =head1 DESCRIPTION
32  
33 The module works with the configuration data
34  
35 =head1 METHODS
36  
37 =over 8
38  
39 =item B<new>
40  
41 my $config = new WWW::MLite::Config(
42 global_file => '/foo/bar/global.conf',
43 local_file => '/foo/bar/local.conf',
44 name => 'foo', # Optional
45 );
46  
47 Returns configuration object
48  
49 =item B<get>
50  
51 my $value = $config->get( 'key' );
52  
53 Returns value by keyname
54  
55 =item B<getall, conf, config>
56  
57 my %config = $config->getall;
58  
59 Returns all allowed pairs - key and value
60  
61 =item B<set>
62  
63 $config->set( 'key', $value );
64  
65 Set new value for key. Returns status of the operation
66  
67 =item B<save>
68  
69 $config->save;
70  
71 Save current configuration to local_file and returns status of the operation
72  
73 =back
74  
75 =head1 HISTORY
76  
77 See C<Changes> file
78  
79 =head1 AUTHOR
80  
81 Serż Minus (Sergey Lepenkov) L<http://www.serzik.com> E<lt>abalama@cpan.orgE<gt>
82  
83 =head1 COPYRIGHT
84  
85 Copyright (C) 1998-2019 D&D Corporation. All Rights Reserved
86  
87 =head1 LICENSE
88  
89 This program is free software; you can redistribute it and/or
90 modify it under the same terms as Perl itself.
91  
92 See C<LICENSE> file and L<https://dev.perl.org/licenses/>
93  
94 =cut
95  
96 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
97 1 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
98 1 ▶use Config::General;
Subroutine BEGIN: called CC=1 SCAR=0.0
99 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
100 1 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
101 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
102 1 ▶use MToken::Const qw/ :GENERAL :MATH /;
Subroutine BEGIN: called CC=1 SCAR=0.0
103  
104 1 ▶use vars qw/$VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
105 1 $VERSION = '1.01';
106  
107 use constant {
108 1 ALLOWED_KEYS => [qw/
109 name distname project
110 server_url
111 gpgbin opensslbin
112 /],
113 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
114  
115 sub new {
116 0 ▶ my $class = shift;
Subroutine new: not called CC=8 SCAR=42.8
Pod documented
117 0 my %args = (@_);
118  
119 0 my $global_file = $args{global_file};
120 0 my $local_file = $args{local_file};
121 0 my $project = $args{project};
122  
123 # global_file
124 0 ▶ unless (defined $global_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $global_file) 0 0 0
125 0 $global_file = File::Spec->catfile(cwd(), DIR_ETC, GLOBAL_CONF_FILE);
126 }
127  
128 # local_file
129 0 ▶ my $locex = (defined($project) && $project ne PROJECT) ? TRUE : FALSE;
Branch: 1 branch
BranchTrueFalseTotal
defined $project && $project ne 'mtoken' ? : 0 0 0
Condition0%0/3
defined $project and $project ne "mtoken"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $project and $project ne "mtoken"
defined $project $project ne "mtoken"
Truth table0%0/3
A and B
A= defined $projectB= $project ne "mtoken"
ABresult
0X0
100
111
130 0 ▶ if (defined $local_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $local_file) { } 0 0 0
131 0 $locex = TRUE;
132 } else {
133 0 ▶ my $lcf = $locex
Branch: 1 branch
BranchTrueFalseTotal
$locex ? : 0 0 0
134 ? sprintf("%s_%s", LOCAL_CONF_FILE, $project)
135 : LOCAL_CONF_FILE;
136 0 $local_file = File::Spec->catfile(home(), $lcf);
137 }
138  
139 0 ▶ unless ($locex) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($locex) 0 0 0
140 0 my %tmp = _loadconfig($global_file);
141 0 my $project = $tmp{project};
142 0 ▶ croak("Can't get PROJECT param from $global_file file. Please reinitialize this project") unless $project;
Branch: 1 branch
BranchTrueFalseTotal
unless $project 0 0 0
143 0 $local_file = File::Spec->catfile(home(), sprintf("%s_%s", LOCAL_CONF_FILE, $project));
144 }
145  
146 0 my %cfg = (
147 global_conf_file => $global_file,
148 local_conf_file => $local_file,
149 _loadconfig($global_file, $local_file)
150 );
151  
152 0 my $self = bless { %cfg }, $class;
153 0 return $self;
154 }
155 sub save {
156 0 ▶ my $self = shift;
Subroutine save: not called CC=1 SCAR=6.9
Pod documented
157 0 return _saveconfig($self->get("local_conf_file"), {($self->getall)});
158 }
159 sub get {
160 0 ▶ my $self = shift;
Subroutine get: not called CC=3 SCAR=24.8
Pod documented
161 0 my $key = shift;
162 0 ▶ unless ($self) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self) 0 0 0
163 0 carp("Object not specified");
164 0 return undef;
165 }
166 0 ▶ return undef unless $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 0 0
167 0 return $self->{$key};
168 }
169 sub getall {
170 0 ▶ my $self = shift;
Subroutine getall: not called CC=3 SCAR=24.8
Pod documented
171  
172 0 my %svh = ();
173 0 foreach my $k (@{(ALLOWED_KEYS)}) {
174 0 my $v = $self->get($k);
175 0 ▶ $svh{$k} = $v if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 0 0 0
176 }
177 0 return (%svh);
178 }
179 0 ▶sub conf { goto &getall };
Subroutine conf: not called CC=1 SCAR=6.9
Pod documented
180 0 ▶sub config { goto &getall };
Subroutine config: not called CC=1 SCAR=6.9
Pod documented
181 sub set {
182 0 ▶ my $self = shift;
Subroutine set: not called CC=3 SCAR=24.8
Pod documented
183 0 my $key = shift;
184 0 my $val = shift;
185 0 ▶ unless ($self) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self) 0 0 0
186 0 carp("Object not specified");
187 0 return FALSE;
188 }
189 0 ▶ unless ($key) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key) 0 0 0
190 0 carp("Key not specified");
191 0 return FALSE;
192 }
193 0 $self->{$key} = $val;
194 0 return TRUE;
195 }
196  
197 sub _loadconfig {
198 0 ▶ my $gfile = shift;
Subroutine _loadconfig: not called CC=15 SCAR=54.8
199 0 my $lfile = shift;
200  
201 0 my %config = (
202 loadstatus_global => FALSE,
203 loadstatus_local => FALSE,
204 configfiles => [],
205 );
206  
207 # Global
208 0 ▶ if ($gfile && -e $gfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gfile and -e $gfile) 0 0 0
Condition0%0/3
$gfile and -e $gfile
!ll&&!rl&&r
000
MC/DC0%0/2
$gfile and -e $gfile
$gfile -e $gfile
Truth table0%0/3
A and B
A= $gfileB= -e $gfile
ABresult
0X0
100
111
209 0 my $gconf;
210 try {
211 0 ▶ $gconf = new Config::General(
Subroutine __ANON__: not called CC=1 SCAR=6.9
212 -ConfigFile => $gfile,
213 #-ConfigPath => $cdirs,
214 -ApacheCompatible => TRUE,
215 -LowerCaseNames => TRUE,
216 -AutoTrue => TRUE,
217 );
218 } catch {
219 0 ▶ carp($_);
Subroutine __ANON__: not called CC=1 SCAR=6.9
220 0 };
221 0 ▶ if ($gconf && $gconf->can('getall')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gconf and $gconf->can("getall")) 0 0 0
Condition0%0/3
$gconf and $gconf->can("getall")
!ll&&!rl&&r
000
MC/DC0%0/2
$gconf and $gconf->can("getall")
$gconf $gconf->can("getall")
Truth table0%0/3
A and B
A= $gconfB= $gconf->can("getall")
ABresult
0X0
100
111
222 0 %config = (%config, $gconf->getall);
223 0 $config{loadstatus_global} = 1;
224 }
225 0 ▶ $config{configfiles} = [$gconf->files] if $gconf && $gconf->can('files');
Branch: 1 branch
BranchTrueFalseTotal
if $gconf and $gconf->can("files") 0 0 0
Condition0%0/3
$gconf and $gconf->can("files")
!ll&&!rl&&r
000
MC/DC0%0/2
$gconf and $gconf->can("files")
$gconf $gconf->can("files")
Truth table0%0/3
A and B
A= $gconfB= $gconf->can("files")
ABresult
0X0
100
111
226 }
227  
228 # Local
229 0 ▶ if ($lfile && -e $lfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lfile and -e $lfile) 0 0 0
Condition0%0/3
$lfile and -e $lfile
!ll&&!rl&&r
000
MC/DC0%0/2
$lfile and -e $lfile
$lfile -e $lfile
Truth table0%0/3
A and B
A= $lfileB= -e $lfile
ABresult
0X0
100
111
230 0 my $lconf;
231 try {
232 0 ▶ $lconf = new Config::General(
Subroutine __ANON__: not called CC=1 SCAR=6.9
233 -ConfigFile => $lfile,
234 -ApacheCompatible => TRUE,
235 -LowerCaseNames => TRUE,
236 -AutoTrue => TRUE,
237 );
238 } catch {
239 0 ▶ carp($_);
Subroutine __ANON__: not called CC=1 SCAR=6.9
240 0 };
241 0 ▶ if ($lconf && $lconf->can('getall')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lconf and $lconf->can("getall")) 0 0 0
Condition0%0/3
$lconf and $lconf->can("getall")
!ll&&!rl&&r
000
MC/DC0%0/2
$lconf and $lconf->can("getall")
$lconf $lconf->can("getall")
Truth table0%0/3
A and B
A= $lconfB= $lconf->can("getall")
ABresult
0X0
100
111
242 0 my %rplc = $lconf->getall;
243 0 ▶ while (my ($k,$v) = each %rplc) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %rplc) 0 0 0
244 0 ▶ $config{$k} = $v if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 0 0 0
245 }
246 0 $config{loadstatus_local} = 1;
247 }
248 0 my $cfs = $config{configfiles};
249 0 ▶ push(@$cfs, ($lconf->files)) if $lconf && $lconf->can('files');
Branch: 1 branch
BranchTrueFalseTotal
if $lconf and $lconf->can("files") 0 0 0
Condition0%0/3
$lconf and $lconf->can("files")
!ll&&!rl&&r
000
MC/DC0%0/2
$lconf and $lconf->can("files")
$lconf $lconf->can("files")
Truth table0%0/3
A and B
A= $lconfB= $lconf->can("files")
ABresult
0X0
100
111
250 }
251  
252 0 return %config;
253 }
254 sub _saveconfig {
255 0 ▶ my $file = shift;
Subroutine _saveconfig: not called CC=4 SCAR=30.0
256 0 my $config = shift;
257 0 ▶ unless ($file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 0 0 0
258 0 carp("Filename not specified!");
259 0 return 0;
260 }
261 0 ▶ unless ($config && ref($config) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config and ref $config eq "HASH") 0 0 0
Condition0%0/3
$config and ref $config eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$config and ref $config eq "HASH"
$config ref $config eq "HASH"
Truth table0%0/3
A and B
A= $configB= ref $config eq "HASH"
ABresult
0X0
100
111
262 0 carp("No configuration data for saving");
263 0 return 0;
264 }
265  
266 0 my $conf = new Config::General(
267 -ConfigHash => $config,
268 -ApacheCompatible => TRUE,
269 -LowerCaseNames => TRUE,
270 -AutoTrue => TRUE,
271 );
272 0 $conf->save_file($file);
273 0 return 1;
274 }
275  
276 1 1;
« MToken/Server.pm ↑ Summary MToken/Util.pm »