Quiq/Config.pm

filter: Statementstmt 87.5% 70 / 80 Branchbran 64.2% 18 / 28 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 84.5% 109 / 129 CC 21 SCAR 24.2
CC
21
Cov
82%
new
23.2
try
7.0
get
3.0
CRAP 11.2
« Quiq/TimeLapse/Filename.pm ↑ Summary Quiq/Url.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Config - Konfigurationsdatei in "Perl Object Notation"
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 SYNOPSIS
14  
15 use Quiq::Config;
16
17 my $cfg = Quiq::Config->new('/etc/myapp/test.conf');
18 my $database = $cgf->get('database');
19  
20 =head1 DESCRIPTION
21  
22 Ein Objekt der Klasse Quiq::Config repräsentiert eine Menge von
23 Attribut/Wert-Paaren, die in einer Perl-Datei definiert sind.
24  
25 Beispiel für den Inhalt einer Konfigurationsdatei:
26  
27 host => 'localhost',
28 datenbank => 'entw1',
29 benutzer => ['sys','system']
30  
31 =head2 Platzhalterersetzung
32  
33 Im Wert einer Konfigurationsvariable können Platzhalter
34 eingebettet sein. Ein solcher Platzhalter wird mit Prozentzeichen
35 (%) begrenzt und beim Lese-Zugriff durch den Wert der betreffenden
36 Konfigurationsvariable ersetzt. Beispiel:
37  
38 Konfigurationsdatei:
39
40 VarDir => '/var/opt/myapp',
41 SpoolDir => '%VarDir%/spool',
42
43 Code:
44
45 $val = $cfg->get('SpoolDir');
46 =>
47 '/var/opt/myapp/spool'
48  
49 =head2 Besondere Platzhalter
50  
51 =over 4
52  
53 =item %CWD%
54  
55 Wird durch den Pfad des aktuellen Verzeichnisses ersetzt.
56 Anwendungsfall: Testkonfiguration für Zugriff auf aktuelles
57 Verzeichnis über einen Dienst wie FTP:
58  
59 test.conf
60 ---------
61 FtpUrl => 'user:passw@localhost:%CWD%'
62  
63 =back
64  
65 =cut
66  
67 # -----------------------------------------------------------------------------
68  
69 package Quiq::Config;
70 10 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 10 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76 10 our $VERSION = '1.240';
77  
78 10 ▶use Quiq::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 10 ▶use Quiq::Reference;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 10 ▶use Quiq::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 10 ▶use Quiq::Unindent;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 10 ▶use Quiq::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 10 ▶use Quiq::Process;
Subroutine BEGIN: called CC=1 SCAR=0.0
84  
85 # -----------------------------------------------------------------------------
86  
87 =head1 METHODS
88  
89 =head2 Konstruktor
90  
91 =head3 new() - Instantiiere Konfigurationsobjekt
92  
93 =head4 Synopsis
94  
95 [1] $cfg = $class->new(@files,@opt);
96 [3] $cfg = $class->new($str);
97 [4] $cfg = $class->new(\%keyVal);
98  
99 =head4 Options
100  
101 =over 4
102  
103 =item -create => $text
104  
105 Falls die Konfigurationsdatei nicht existert, erzeuge sie mit
106 dem Inhalt $text.
107  
108 =item -secure => $bool (Default: 0)
109  
110 Prüfe die Sicherheit der Datei. Wenn gesetzt, wird geprüft,
111 ob die Datei nur für den aufrufenden Benutzer lesbar/schreibbar ist.
112  
113 =back
114  
115 =head4 Description
116  
117 [1] Instantiiere Konfigurationsobjekt aus einer der Dateien @files
118 und liefere eine Referenz auf dieses Objekt zurück. Beginnt $file
119 mit einer Tilde (~), wird sie zum Homedir des rufenden Users
120 expandiert. Die erste gefundene Datei wird geöffnet.
121  
122 [2] Als Parameter ist der Konfigurationscode als Zeichenkette
123 der Form "$key => $val, ..." angegeben.
124  
125 [3] Die Konfiguration ist inline durch Hash %keyVal angegeben.
126  
127 =cut
128  
129 # -----------------------------------------------------------------------------
130  
131 5 ▶sub new {
Subroutine new: called CC=13 SCAR=31.4
Pod documented
132 5 my $class = shift;
133 # @_: @files -or- $str -or- \%keyVal
134  
135 # Optionen
136  
137 5 my $create = undef;
138 5 my $secure = 0;
139  
140 5 Quiq::Option->extract(\@_,
141 -create => \$create,
142 -secure => \$secure,
143 );
144  
145 # Operation ausführen
146  
147 5 my %cfg;
148 5 ▶ if ($_[0] =~ /=>/) { # "$key => $val, ..."
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0] =~ /=>/) { } 1 4 5
elsif ('Quiq::Reference'->isHashRef($_[0])) { } 1 3 4
149 1 %cfg = eval shift;
150 }
151 elsif (Quiq::Reference->isHashRef($_[0])) { # \%keyVal
152 1 %cfg = %{shift()};
153 }
154 else {
155 # @files
156  
157 3 my $cfgFile;
158 3 my $p = Quiq::Path->new;
159  
160 # Datei suchen
161  
162 3 for my $file (@_) {
163 3 ▶ if ($p->exists($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->exists($file)) 3 0 3
164 3 $cfgFile = $p->expandTilde($file);
165 }
166 }
167 3 ▶ if (!defined $cfgFile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cfgFile) 0 3 3
168 0 ▶ if (defined $create) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $create) { } 0 0 0
169 # Wir speichern die Datei unter dem ersten Dateinamen,
170 # dessen Verzeichnis existiert.
171  
172 0 for my $file (@_) {
173 0 my ($dir) = $p->split($file);
174 0 ▶ if ($p->exists($dir)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->exists($dir)) 0 0 0
175 0 $cfgFile = $p->expandTilde($file);
176 }
177 }
178 0 $create = Quiq::Unindent->trimNl($create);
179 0 $p->write($cfgFile,$create,-recursive=>1);
180 0 ▶ if ($secure) {
Branch: 1 branch
BranchTrueFalseTotal
if ($secure) 0 0 0
181 0 ▶ $p->chmod($cfgFile,$secure? 0600: 0644);
Branch: 1 branch
BranchTrueFalseTotal
$secure ? : 0 0 0
182 }
183 }
184 else {
185 0 $class->throw(
186 'CFG-00002: Config file not found',
187 ConfigFile => "@_",
188 );
189 }
190 }
191  
192 3 ▶ if ($secure) {
Branch: 1 branch
BranchTrueFalseTotal
if ($secure) 1 2 3
193 1 $p->checkFileSecurity($cfgFile);
194 }
195  
196 3 ▶ if (substr($cfgFile,0,1) ne '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($cfgFile, 0, 1) ne "/") 2 1 3
197 # Wenn der Dateiname kein absoluter Pfad ist,
198 # müssen wir ./ voranstellen, weil perlDoFile()
199 # sonst @INC nach der Datei absucht (und sie
200 # dann nicht findet)
201
202 2 $cfgFile = "./$cfgFile";
203 }
204
205 3 %cfg = Quiq::Perl->perlDoFile($cfgFile);
206 }
207  
208 5 return bless \%cfg,$class;
209 }
210  
211 # -----------------------------------------------------------------------------
212  
213 =head2 Werte abfragen
214  
215 =head3 get() - Liefere Konfigurationswerte
216  
217 =head4 Synopsis
218  
219 $val = $cfg->get($key);
220 @vals = $cfg->get(@keys);
221  
222 =head4 Description
223  
224 Liefere den Wert des Konfigurationsattributs $key bzw. die Werte
225 der Konfigurationsattribute @keys.
226  
227 Existiert ein Konfigurationsattribut nicht, wirft die Methode eine
228 Exception.
229  
230 =cut
231  
232 # -----------------------------------------------------------------------------
233  
234 13 ▶sub get {
Subroutine get: called CC=3 SCAR=11.0
Pod documented
235 13 my $self = shift;
236 # @_: @keys
237  
238 # Existenz der Attribute überprüfen
239  
240 13 for my $key (@_) {
241 14 ▶ if (!exists $self->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{$key}) 3 11 14
242 3 $self->throw(
243 'CFG-00001: Config variable does not exist',
244 Variable => $key,
245 );
246 }
247 }
248  
249 # Aufruf an try() delegieren
250 10 return $self->try(@_);
251 }
252  
253 # -----------------------------------------------------------------------------
254  
255 =head3 try() - Liefere Konfigurationswerte ohne Exception
256  
257 =head4 Synopsis
258  
259 $val = $cfg->try($key);
260 @vals = $cfg->try(@keys);
261  
262 =head4 Description
263  
264 Liefere den Wert des Konfigurationsattributs $key bzw. die Werte
265 der Konfigurationsattribute @keys. Existiert ein
266 Konfigurationsattribut nicht, liefere undef.
267  
268 =cut
269  
270 # -----------------------------------------------------------------------------
271  
272 14 ▶sub try {
Subroutine try: called CC=7 SCAR=19.5
Pod documented
273 14 my $self = shift;
274 # @_: @keys
275  
276 14 my @arr;
277 14 for my $key (@_) {
278 15 my $val = $self->{$key};
279 15 ▶ if (!ref $val && defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $val and defined $val) 10 5 15
Condition100%3/3
not ref $val and defined $val
!ll&&!rl&&r
3210
MC/DC100%2/2
not ref $val and defined $val
not ref $val defined $val
Truth table100%3/3
not(A) and B
A= not ref $valB= defined $val
ABresult
0X0
100
111
280 # Platzhalter suchen und ersetzen
281 10 for my $key ($val =~ /%(\w+)%/g) {
282 2 ▶ if ($key eq 'CWD') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq 'CWD') { } 1 1 2
283 1 $val =~ s/%CWD%/Quiq::Process->cwd/e;
284 }
285 else {
286 1 $val =~ s/%$key%/$self->get($key)/e;
287 }
288 }
289 }
290 15 push @arr,$val;
291 }
292  
293 14 ▶ return wantarray? @arr: $arr[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 13 14
294 }
295  
296 # -----------------------------------------------------------------------------
297  
298 =head1 VERSION
299  
300 1.240
301  
302 =head1 AUTHOR
303  
304 Frank Seitz, L<http://fseitz.de/>
305  
306 =head1 COPYRIGHT
307  
308 Copyright (C) 2026 Frank Seitz
309  
310 =head1 LICENSE
311  
312 This code is free software; you can redistribute it and/or modify
313 it under the same terms as Perl itself.
314  
315 =cut
316  
317 # -----------------------------------------------------------------------------
318  
319 10 1;
320  
321 # eof
« Quiq/TimeLapse/Filename.pm ↑ Summary Quiq/Url.pm »