Merge branch 'master' of lx-office.linet-services.de:lx-office-erp
[kivitendo-erp.git] / SL / LxOfficeConf.pm
1 package SL::LxOfficeConf;
2
3 use strict;
4
5 use Config::Std;
6 use Encode;
7
8 sub read {
9   my ($class, $file_name) = @_;
10
11   read_config 'config/lx_office.conf.default' => %::lx_office_conf;
12   _decode_recursively(\%::lx_office_conf);
13
14   $file_name ||= 'config/lx_office.conf';
15
16   if (-f $file_name) {
17     read_config $file_name => my %local_conf;
18     _decode_recursively(\%local_conf);
19     _flat_merge(\%::lx_office_conf, \%local_conf);
20   }
21 }
22
23 sub _decode_recursively {
24   my ($obj) = @_;
25
26   while (my ($key, $value) = each %{ $obj }) {
27     if (ref($value) eq 'HASH') {
28       _decode_recursively($value);
29     } else {
30       $obj->{$key} = decode('UTF-8', $value);
31     }
32   }
33 }
34
35 sub _flat_merge {
36   my ($dst, $src) = @_;
37
38   while (my ($key, $value) = each %{ $src }) {
39     if (!exists $dst->{$key}) {
40       $dst->{$key} = $value;
41
42     } else {
43       map { $dst->{$key}->{$_} = $value->{$_} } keys %{ $value };
44     }
45   }
46 }
47
48 1;