Merge branch 'master' of git@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   read_config 'config/lx_office.conf.default' => %::lx_office_conf;
10   _decode_recursively(\%::lx_office_conf);
11
12   if (-f 'config/lx_office.conf') {
13     read_config 'config/lx_office.conf' => my %local_conf;
14     _decode_recursively(\%local_conf);
15     _flat_merge(\%::lx_office_conf, \%local_conf);
16   }
17 }
18
19 sub _decode_recursively {
20   my ($obj) = @_;
21
22   while (my ($key, $value) = each %{ $obj }) {
23     if (ref($value) eq 'HASH') {
24       _decode_recursively($value);
25     } else {
26       $obj->{$key} = decode('UTF-8', $value);
27     }
28   }
29 }
30
31 sub _flat_merge {
32   my ($dst, $src) = @_;
33
34   while (my ($key, $value) = each %{ $src }) {
35     if (!exists $dst->{$key}) {
36       $dst->{$key} = $value;
37
38     } else {
39       map { $dst->{$key}->{$_} = $value->{$_} } keys %{ $value };
40     }
41   }
42 }
43
44 1;