#!/usr/bin/perl
package RVL::Config;
##LICENSE##
=head1 NAME
RVL::Config - Config file parsing and handling, acts as a registry for config data.
=head1 SYNOPSIS
=head1 DESCRIPTION
=head1 METHODS
=cut
use strict;
use warnings;
use CGI;
use IO::Interactive qw(is_interactive interactive busy);
use RVL::ParamHandler;
use Class::Std::Utils;
{
# class attribute
sub new {
my ($class, $opt) = @_;
my %hash;
my ($this) = bless( \%hash, $class);
if ($this->isEmpty()) {
my ($configFile) = (defined $opt->{file}) ? $opt->{file} : $this->getDefauftConfig();
if ($configFile) {
RVL::debug("Load config $configFile", __CONSTANT__::RVL_LOG_DEBUG);
$this->{aProps} = $this->load($configFile);
} else {
RVL::raiseError('could not config file ' . $configFile,
__CONSTANT__::RVL_ERROR_NOFILE);
}
}
return $this;
}
=head2 singleton()
Returns a singleton Request instance.
=cut
sub singleton {
my ($class, $file) = __PACKAGE__;
if (!defined $RVL::INSTANCE{$class}) {
$RVL::INSTANCE{$class} = $class->new();
}
return $RVL::INSTANCE{$class}
}
sub load {
my ($this, $file) = @_;
if (-f $file) {
RVL::logMessage('Load config file: ' . $file, __CONSTANT__::RVL_LOG_DEBUG);
my ($fp) = RVL::ParamHandler::singleton($file);
my ($data) = $fp->read();
return $data;
} else {
return RVL::raiseError('Problem reading config file ' . $file, 0, __CONSTANT__::RVL_ERROR_INVALIDFILEPERMS);
}
}
sub get {
my ($this, $key);
$this = shift;
if (ref $this ne __PACKAGE__) {
$key = $this;
$this = RVL::Config::singleton();
} else {
$key = shift;
}
if (RVL::String::is_hash($key)) {
my ($group) = keys %{$key};
return defined($this->{aProps}->{$group}->{$key->{$group}}) ?
$this->{aProps}->{$group}->{$key->{$group}} : '';
} elsif(RVL::String::is_string($key)) {
my (@keys) = split(/\./, $key, 2);
if (!$keys[0] || !$keys[1]) {
return '';
} elsif (!defined($this->{aProps}) || !$this->{aProps}) {
return '';
} elsif(!defined($this->{aProps}->{$keys[0]})) {
return '';
} elsif (!defined($this->{aProps}->{$keys[0]}->{$keys[1]}) ) {
return '';
} else {
return $this->{aProps}->{$keys[0]}->{$keys[1]};
}
}
}
sub getAll {
my ($this) = @_;
return $this->{aProps};
}
sub getDefauftConfig {
my ($this) = @_;
return __CONSTANT__::RVL_VAR_DIR . '/' . $this->getRealHostname() . '.yaml';
}
sub getRealHostname {
my ($this) = @_;
$this = RVL::Config::singleton() if (ref $this ne __PACKAGE__);
my ($hostname);
if (!-f __CONSTANT__::RVL_VAR_DIR . '/customhostname.txt') {
$hostname = `hostname`;
} else {
open(FD, '<', __CONSTANT__::RVL_VAR_DIR . '/customhostname.txt');
$hostname = <FD>;
close(FD);
}
chomp($hostname);
return $hostname;
}
sub isEmpty {
my ($this, $force) = @_;
return (defined $this->{aProps}) ? 0 : 1;
}
sub ensureModuleConfigLoaded{
my ($this, $moduleName) = @_;
if ( !defined $this->{aProps}->{localConfig}->{moduleName}
|| $this->{aProps}->{localConfig}->{moduleName} ne $moduleName) {
my ($modConfigPath) = __CONSTANT__::RVL_MOD_DIR . '/' . $moduleName . '/conf.yaml';
if (-f $modConfigPath) {
my ($aModuleConfig) = $this->load($modConfigPath);
$this->merge($aModuleConfig);
} else {
my $ret = RVL::raiseError("Config file could not be found at '$modConfigPath'", 0, 1, __CONSTANT__::RVL_ERROR_NOFILE);
}
}
}
sub merge{
my ($this, $value) = @_;
$this->{aProps} = RVL::String::merge($this->{aProps}, $value);
}
}
1;
Copyright 2K16 - 2K18 Indonesian Hacker Rulez