| 1 | package Service::Engine::Config; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use File::Slurp; | ||
| 7 | use Carp; | ||
| 8 | use Data::Dumper; | ||
| 9 | use File::stat; | ||
| 10 | use Time::localtime; | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | |||
| 14 | my ($class,$options) = @_; | ||
| 15 | |||
| 16 | # set some defaults | ||
| 17 | my $attributes = { | ||
| 18 | 'config'=> { | ||
| 19 | 'logging' => { | ||
| 20 | 'log_level' => 0 | ||
| 21 | } | ||
| 22 | } | ||
| 23 | }; | ||
| 24 | |||
| 25 | # load options | ||
| 26 | if (ref($options) eq 'HASH') { | ||
| 27 | foreach my $option (keys %{$options}) { | ||
| 28 | $attributes->{$option} = $options->{$option}; | ||
| 29 | } | ||
| 30 | } | ||
| 31 | |||
| 32 | # load configuration file | ||
| 33 | my $config; | ||
| 34 | if ($attributes->{config_file}) { | ||
| 35 | if (-e $attributes->{config_file}) { | ||
| 36 | my $text = read_file($attributes->{config_file}, { err_mode => 'carp' }); | ||
| 37 | $config = eval($text); | ||
| 38 | if (ref($config) ne 'HASH') { | ||
| 39 | croak("config file is not a valid Perl file: " . $attributes->{config_file}); | ||
| 40 | } | ||
| 41 | } else { | ||
| 42 | croak("Can't open config file " . $attributes->{config_file}); | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | $attributes->{config}->{mtime} = ctime(stat($attributes->{config_file})->mtime); | ||
| 47 | |||
| 48 | if (ref($config) eq 'HASH') { | ||
| 49 | foreach my $option (keys %{$config}) { | ||
| 50 | $attributes->{'config'}->{$option} = $config->{$option}; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | my $self = bless $attributes, $class; | ||
| 55 | |||
| 56 | return $self; | ||
| 57 | |||
| 58 | } | ||
| 59 | |||
| 60 | sub get_config { | ||
| 61 | |||
| 62 | my ($self, $key) = @_; | ||
| 63 | |||
| 64 | if ($key) { | ||
| 65 | return $self->{'config'}->{$key}; | ||
| 66 | } | ||
| 67 | |||
| 68 | # with no $key return the entire config object | ||
| 69 | return $self->{'config'}; | ||
| 70 | |||
| 71 | } | ||
| 72 | |||
| 73 | sub reload { | ||
| 74 | |||
| 75 | # THIS IS NOT IMPLEMENTED | ||
| 76 | |||
| 77 | my ($self) = @_; | ||
| 78 | |||
| 79 | if ($self->{'config'}->{'mtime'} ne ctime(stat($self->{'config_file'})->mtime)) { | ||
| 80 | |||
| 81 | # load configuration file | ||
| 82 | my $config; | ||
| 83 | |||
| 84 | if (-e $self->{config_file}) { | ||
| 85 | my $text = read_file($self->{config_file}, { err_mode => 'carp' }); | ||
| 86 | $config = eval($text); | ||
| 87 | if (ref($config) ne 'HASH') { | ||
| 88 | croak("config file is not a valid Perl file: " . $self->{config_file}); | ||
| 89 | } | ||
| 90 | } else { | ||
| 91 | croak("Can't open config file " . $self->{config_file}); | ||
| 92 | } | ||
| 93 | |||
| 94 | $self->{'config'}->{'mtime'} = ctime(stat($self->{config_file})->mtime); | ||
| 95 | |||
| 96 | if (ref($config) eq 'HASH') { | ||
| 97 | foreach my $option (keys %{$config}) { | ||
| 98 | $self->{'config'}->{$option} = $config->{$option}; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | } | ||
| 103 | |||
| 104 | } | ||
| 105 | |||
| 106 | 1; |