2015-03-08 04:57:30 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* Configuration Management
|
|
|
|
*/
|
|
|
|
|
2015-03-25 09:59:10 +00:00
|
|
|
namespace Sakura;
|
2015-03-08 04:57:30 +00:00
|
|
|
|
|
|
|
class Configuration {
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:57:30 +00:00
|
|
|
public static $_LCNF;
|
|
|
|
public static $_DCNF;
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:57:30 +00:00
|
|
|
// Constructor
|
|
|
|
public static function init($local) {
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:57:30 +00:00
|
|
|
// Store $local in $_LCNF
|
|
|
|
if(is_array($local))
|
|
|
|
self::$_LCNF = $local;
|
|
|
|
else
|
|
|
|
die('<h1>Failed to initialise configuration.</h1>');
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:57:30 +00:00
|
|
|
}
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:57:30 +00:00
|
|
|
// Initialise Database configuration values.
|
2015-03-21 14:12:51 +00:00
|
|
|
// Different from init as that is called before the database connection is initially
|
2015-03-08 04:57:30 +00:00
|
|
|
// established
|
|
|
|
public static function initDB() {
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:57:30 +00:00
|
|
|
$_DATA = Database::fetch('config', true);
|
|
|
|
$_DBCN = array();
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:57:30 +00:00
|
|
|
foreach($_DATA as $_CONF)
|
|
|
|
$_DBCN[$_CONF[0]] = $_CONF[1];
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:57:30 +00:00
|
|
|
self::$_DCNF = $_DBCN;
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:57:30 +00:00
|
|
|
}
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:57:30 +00:00
|
|
|
// Get values from the configuration on the file system
|
|
|
|
public static function getLocalConfig($key, $subkey = null) {
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:57:30 +00:00
|
|
|
if(array_key_exists($key, self::$_LCNF)) {
|
|
|
|
if($subkey)
|
|
|
|
return self::$_LCNF[$key][$subkey];
|
|
|
|
else
|
|
|
|
return self::$_LCNF[$key];
|
|
|
|
} else
|
2015-03-29 16:25:18 +00:00
|
|
|
trigger_error('Unable to get local configuration value!', E_USER_ERROR);
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:57:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Dynamically set local configuration values, does not update the configuration file
|
|
|
|
public static function setLocalConfig($key, $subkey, $value) {
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:57:30 +00:00
|
|
|
if($subkey) {
|
2015-03-21 14:12:51 +00:00
|
|
|
if(!isset(self::$_LCNF[$key]))
|
2015-03-08 04:57:30 +00:00
|
|
|
self::$_LCNF[$key] = array();
|
|
|
|
self::$_LCNF[$key][$subkey] = $value;
|
|
|
|
} else {
|
|
|
|
self::$_LCNF[$key] = $value;
|
|
|
|
}
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:57:30 +00:00
|
|
|
}
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:57:30 +00:00
|
|
|
// Get values from the configuration in the database
|
|
|
|
public static function getConfig($key) {
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:57:30 +00:00
|
|
|
if(array_key_exists($key, self::$_DCNF))
|
2015-03-08 06:11:57 +00:00
|
|
|
return self::$_DCNF[$key];
|
2015-03-08 04:57:30 +00:00
|
|
|
else
|
2015-03-29 16:25:18 +00:00
|
|
|
trigger_error('Unable to get configuration value!', E_USER_ERROR);
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:57:30 +00:00
|
|
|
}
|
2015-03-21 14:12:51 +00:00
|
|
|
|
2015-03-08 04:57:30 +00:00
|
|
|
}
|