this'll probably fix it
This commit is contained in:
parent
849d5d3ac9
commit
2fbb62b5b8
1 changed files with 120 additions and 120 deletions
|
@ -1,120 +1,120 @@
|
||||||
<?php
|
<?php
|
||||||
/*
|
/*
|
||||||
* Configuration Management
|
* Configuration Management
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Sakura;
|
namespace Sakura;
|
||||||
|
|
||||||
class Configuration {
|
class Configuration {
|
||||||
|
|
||||||
// Configuration data
|
// Configuration data
|
||||||
public static $_LCNF;
|
public static $_LCNF;
|
||||||
public static $_DCNF;
|
public static $_DCNF;
|
||||||
|
|
||||||
// Initialise configuration, does not contain database initialisation because explained below
|
// Initialise configuration, does not contain database initialisation because explained below
|
||||||
public static function init($local) {
|
public static function init($local) {
|
||||||
|
|
||||||
// Check if $local is an array and then store it in $_LCNF
|
// Check if $local is an array and then store it in $_LCNF
|
||||||
if(is_array($local))
|
if(is_array($local))
|
||||||
self::$_LCNF = $local;
|
self::$_LCNF = $local;
|
||||||
else // Otherwise trigger an error
|
else // Otherwise trigger an error
|
||||||
trigger_error('Failed to load local configuration!', E_USER_ERROR);
|
trigger_error('Failed to load local configuration!', E_USER_ERROR);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Initialise Database configuration values.
|
* Initialise Database configuration values.
|
||||||
* Different from init as that is called before the database connection is initially
|
* Different from init as that is called before the database connection is initially
|
||||||
* established.
|
* established.
|
||||||
*/
|
*/
|
||||||
public static function initDB() {
|
public static function initDB() {
|
||||||
|
|
||||||
// Get config table from the database
|
// Get config table from the database
|
||||||
$_DATA = Database::fetch('config', true);
|
$_DATA = Database::fetch('config', true);
|
||||||
|
|
||||||
// Create variable to temporarily store values in
|
// Create variable to temporarily store values in
|
||||||
$_DBCN = array();
|
$_DBCN = array();
|
||||||
|
|
||||||
foreach($_DATA as $_CONF) // Properly sort the values
|
foreach($_DATA as $_CONF) // Properly sort the values
|
||||||
$_DBCN[$_CONF[0]] = $_CONF[1];
|
$_DBCN[$_CONF[0]] = $_CONF[1];
|
||||||
|
|
||||||
// Assign the temporary array to the static one
|
// Assign the temporary array to the static one
|
||||||
self::$_DCNF = $_DBCN;
|
self::$_DCNF = $_DBCN;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get values from the configuration on the file system
|
// Get values from the configuration on the file system
|
||||||
public static function getLocalConfig($key, $subkey = null) {
|
public static function getLocalConfig($key, $subkey = null) {
|
||||||
|
|
||||||
// Check if the key that we're looking for exists
|
// Check if the key that we're looking for exists
|
||||||
if(array_key_exists($key, self::$_LCNF)) { // If we also have a subkey we check
|
if(array_key_exists($key, self::$_LCNF)) { // If we also have a subkey we check if that exists, else we just return the default value.
|
||||||
if($subkey && array_key_exists($subkey, $key)) // if that exists, else we just return
|
if($subkey && is_array($key) && array_key_exists($subkey, $key))
|
||||||
return self::$_LCNF[$key][$subkey]; // the default value.
|
return self::$_LCNF[$key][$subkey];
|
||||||
else
|
else
|
||||||
return self::$_LCNF[$key];
|
return self::$_LCNF[$key];
|
||||||
} else // If it doesn't exist trigger an error to avoid explosions
|
} else // If it doesn't exist trigger an error to avoid explosions
|
||||||
trigger_error('Unable to get local configuration value!', E_USER_ERROR);
|
trigger_error('Unable to get local configuration value!', E_USER_ERROR);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dynamically set local configuration values, does not update the configuration file
|
// Dynamically set local configuration values, does not update the configuration file
|
||||||
public static function setLocalConfig($key, $subkey, $value) {
|
public static function setLocalConfig($key, $subkey, $value) {
|
||||||
|
|
||||||
// Check if we also do a subkey
|
// Check if we also do a subkey
|
||||||
if($subkey) {
|
if($subkey) {
|
||||||
|
|
||||||
// If we do we make sure that the parent key is an array
|
// If we do we make sure that the parent key is an array
|
||||||
if(!isset(self::$_LCNF[$key]))
|
if(!isset(self::$_LCNF[$key]))
|
||||||
self::$_LCNF[$key] = array();
|
self::$_LCNF[$key] = array();
|
||||||
|
|
||||||
// And then assign the value
|
// And then assign the value
|
||||||
self::$_LCNF[$key][$subkey] = $value;
|
self::$_LCNF[$key][$subkey] = $value;
|
||||||
|
|
||||||
} else // Otherwise we just straight up assign it
|
} else // Otherwise we just straight up assign it
|
||||||
self::$_LCNF[$key] = $value;
|
self::$_LCNF[$key] = $value;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get values from the configuration in the database
|
// Get values from the configuration in the database
|
||||||
public static function getConfig($key) {
|
public static function getConfig($key) {
|
||||||
|
|
||||||
// Check if the key that we're looking for exists
|
// Check if the key that we're looking for exists
|
||||||
if(array_key_exists($key, self::$_DCNF))
|
if(array_key_exists($key, self::$_DCNF))
|
||||||
return self::$_DCNF[$key]; // Then return the value
|
return self::$_DCNF[$key]; // Then return the value
|
||||||
else // If it doesn't exist trigger an error to avoid explosions
|
else // If it doesn't exist trigger an error to avoid explosions
|
||||||
trigger_error('Unable to get configuration value!', E_USER_ERROR);
|
trigger_error('Unable to get configuration value!', E_USER_ERROR);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse .cfg files, mainly/only used for templates
|
// Parse .cfg files, mainly/only used for templates
|
||||||
public static function parseCfg($data) {
|
public static function parseCfg($data) {
|
||||||
|
|
||||||
// Create storage variable
|
// Create storage variable
|
||||||
$out = array();
|
$out = array();
|
||||||
|
|
||||||
// Remove comments and empty lines
|
// Remove comments and empty lines
|
||||||
$data = preg_replace('/#.*?\r\n/im', null, $data);
|
$data = preg_replace('/#.*?\r\n/im', null, $data);
|
||||||
$data = preg_replace('/^\r\n/im', null, $data);
|
$data = preg_replace('/^\r\n/im', null, $data);
|
||||||
|
|
||||||
// Break line breaks up into array values
|
// Break line breaks up into array values
|
||||||
$data = explode("\r\n", $data);
|
$data = explode("\r\n", $data);
|
||||||
|
|
||||||
foreach($data as $var) {
|
foreach($data as $var) {
|
||||||
|
|
||||||
// Remove whitespace between key, equals sign and value
|
// Remove whitespace between key, equals sign and value
|
||||||
$var = preg_replace('/[\s+]=[\s+]/i', '=', $var);
|
$var = preg_replace('/[\s+]=[\s+]/i', '=', $var);
|
||||||
|
|
||||||
// Then break this up
|
// Then break this up
|
||||||
$var = explode('=', $var);
|
$var = explode('=', $var);
|
||||||
|
|
||||||
// And assign the value with the key to the output variable
|
// And assign the value with the key to the output variable
|
||||||
$out[$var[0]] = $var[1];
|
$out[$var[0]] = $var[1];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the output variable
|
// Return the output variable
|
||||||
return $out;
|
return $out;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue