line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package OTRS::OPM::Analyzer::Utils::Config; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: class to parse a yaml config |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
27313
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
6
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
23
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
4
|
use Carp; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
96
|
|
9
|
1
|
|
|
1
|
|
914
|
use YAML::Tiny; |
|
1
|
|
|
|
|
6432
|
|
|
1
|
|
|
|
|
63
|
|
10
|
1
|
|
|
1
|
|
7
|
use File::Spec; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
17
|
|
11
|
1
|
|
|
1
|
|
5
|
use File::Basename; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
510
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new{ |
14
|
2
|
|
|
2
|
1
|
934
|
my ($class,$file) = @_; |
15
|
|
|
|
|
|
|
|
16
|
2
|
|
|
|
|
7
|
my $self = {}; |
17
|
2
|
|
|
|
|
6
|
bless $self,$class; |
18
|
|
|
|
|
|
|
|
19
|
2
|
|
|
|
|
218
|
my $default_dir = File::Spec->rel2abs( File::Basename::dirname( $0 ) ); |
20
|
2
|
|
|
|
|
24
|
my $default_file = File::Spec->catfile( $default_dir, 'conf', 'base.yml' ); |
21
|
2
|
|
|
|
|
22
|
$default_file = File::Spec->rel2abs( $default_file ); |
22
|
|
|
|
|
|
|
|
23
|
2
|
50
|
0
|
|
|
65
|
$file ||= $default_file if -e $default_file; |
24
|
2
|
50
|
|
|
|
12
|
$self->load( $file ) if defined $file; |
25
|
2
|
|
|
|
|
8
|
return $self; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub load{ |
29
|
3
|
|
|
3
|
1
|
43
|
my ($self,$file) = @_; |
30
|
3
|
100
|
|
|
|
34
|
croak "no config file given" unless defined $file; |
31
|
|
|
|
|
|
|
|
32
|
2
|
|
|
|
|
4
|
my $result = eval{ |
33
|
2
|
|
|
|
|
17
|
YAML::Tiny->read( $file ); |
34
|
|
|
|
|
|
|
}; |
35
|
|
|
|
|
|
|
|
36
|
2
|
|
100
|
|
|
25566
|
$result ||= []; |
37
|
|
|
|
|
|
|
|
38
|
2
|
|
100
|
|
|
21
|
$self->{_config} = $result->[0] || {}; |
39
|
|
|
|
|
|
|
|
40
|
2
|
|
|
|
|
69
|
return $self->{_config}; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub get { |
44
|
13
|
|
|
13
|
1
|
37
|
my ($self,$key) = @_; |
45
|
|
|
|
|
|
|
|
46
|
13
|
|
|
|
|
16
|
my $return; |
47
|
|
|
|
|
|
|
|
48
|
13
|
100
|
|
|
|
31
|
if( defined $key ){ |
49
|
12
|
|
50
|
|
|
35
|
my $config = $self->{_config} || {}; |
50
|
|
|
|
|
|
|
|
51
|
12
|
|
|
|
|
53
|
my @keys = split /(?<!\\)\./, $key; |
52
|
12
|
|
|
|
|
19
|
for my $subkey ( @keys ){ |
53
|
19
|
|
|
|
|
28
|
$subkey =~ s/\\\././g; |
54
|
|
|
|
|
|
|
|
55
|
19
|
100
|
33
|
|
|
135
|
return if $config and $subkey and ref $config ne 'HASH'; |
|
|
|
66
|
|
|
|
|
56
|
|
|
|
|
|
|
|
57
|
18
|
100
|
|
|
|
59
|
return if not exists $config->{$subkey}; |
58
|
14
|
|
|
|
|
32
|
$config = $config->{$subkey}; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
7
|
|
|
|
|
17
|
$return = $config; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
8
|
|
|
|
|
49
|
$return; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub set { |
68
|
4
|
|
|
4
|
1
|
9
|
my ($self,$key,$value) = @_; |
69
|
|
|
|
|
|
|
|
70
|
4
|
|
50
|
|
|
14
|
$self->{_config} ||= {}; |
71
|
|
|
|
|
|
|
|
72
|
4
|
50
|
|
|
|
12
|
if( defined $key ){ |
73
|
4
|
|
50
|
|
|
14
|
my $config = $self->{_config} || {}; |
74
|
|
|
|
|
|
|
|
75
|
4
|
|
|
|
|
22
|
my @keys = split /(?<!\\)\./, $key; |
76
|
4
|
|
|
|
|
16
|
while ( my $subkey = shift @keys ){ |
77
|
10
|
|
|
|
|
15
|
$subkey =~ s/\\\././g; |
78
|
|
|
|
|
|
|
|
79
|
10
|
100
|
33
|
|
|
76
|
last if $subkey and $config and ref $config ne 'HASH'; |
|
|
|
66
|
|
|
|
|
80
|
|
|
|
|
|
|
|
81
|
9
|
100
|
|
|
|
28
|
$config->{$subkey} = {} if not exists $config->{$subkey}; |
82
|
|
|
|
|
|
|
|
83
|
9
|
100
|
|
|
|
21
|
if ( !@keys ) { |
84
|
3
|
|
|
|
|
18
|
$config->{$subkey} = $value; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
else { |
87
|
6
|
|
|
|
|
24
|
$config = $config->{$subkey}; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
1; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
__END__ |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=pod |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head1 NAME |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
OTRS::OPM::Analyzer::Utils::Config - class to parse a yaml config |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head1 VERSION |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
version 0.03 |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head1 SYNOPSIS |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
use OTRS::OPM::Analyzer::Utils::Config; |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
my $config = '/path/to/config.yml'; |
112
|
|
|
|
|
|
|
my $obj = OTRS::OPM::Analyzer::Utils::Config->new( $config ); |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
print $obj->get( 'app.path' ); # /opt/otrs/ |
115
|
|
|
|
|
|
|
print $obj->get( 'app' )->{path}; # /opt/otrs/ |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
$obj->set( 'app.version', '3.3.3' ); |
118
|
|
|
|
|
|
|
print $obj->get( 'app.version' ); # 3.3.3 |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
config.yml |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
--- |
123
|
|
|
|
|
|
|
app: |
124
|
|
|
|
|
|
|
path: /opt/otrs/ |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head1 METHODS |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head2 new |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Creates a new object of the config parser |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
use OTRS::OPM::Analyzer::Utils::Config; |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
my $config = '/path/to/config.yml'; |
135
|
|
|
|
|
|
|
my $obj = OTRS::OPM::Analyzer::Utils::Config->new( $config ); |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head2 load |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
Loads a new config |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
my $config = '/path/to/config.yml'; |
142
|
|
|
|
|
|
|
my $obj = OTRS::OPM::Analyzer::Utils::Config->new; |
143
|
|
|
|
|
|
|
$obj->load( $config ); |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head2 get |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
Returns the value of a given config key. Multilevel hashes can be separated with a '.' |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
print $obj->get( 'app.path' ); # /opt/otrs/ |
150
|
|
|
|
|
|
|
print $obj->get( 'app' )->{path}; # /opt/otrs/ |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head2 set |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Sets a config option |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
$obj->set( 'app.version', '3.3.3' ); |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head1 AUTHOR |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
Renee Baecker <reneeb@cpan.org> |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
This software is Copyright (c) 2014 by Renee Baecker. |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
This is free software, licensed under: |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
The Artistic License 2.0 (GPL Compatible) |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=cut |