This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/_sakura/components/Configuration.php

90 lines
2.6 KiB
PHP
Raw Normal View History

2015-04-01 15:38:04 +00:00
<?php
/*
* Configuration Management
*/
namespace Sakura;
class Configuration {
// Configuration data
public static $_LCNF;
public static $_DCNF;
// Initialise configuration, does not contain database initialisation because explained below
public static function init($local) {
// Check if $local is an array and then store it in $_LCNF
if(is_array($local))
self::$_LCNF = $local;
else // Otherwise trigger an error
trigger_error('Failed to load local configuration!', E_USER_ERROR);
}
/*
* Initialise Database configuration values.
* Different from init as that is called before the database connection is initially
* established.
*/
public static function initDB() {
// Get config table from the database
$_DATA = Database::fetch('config', true);
// Create variable to temporarily store values in
$_DBCN = array();
foreach($_DATA as $_CONF) // Properly sort the values
$_DBCN[$_CONF[0]] = $_CONF[1];
// Assign the temporary array to the static one
self::$_DCNF = $_DBCN;
}
// Get values from the configuration on the file system
public static function getLocalConfig($key, $subkey = null) {
// Check if the key that we're looking for exists
2015-04-01 15:41:06 +00:00
if(array_key_exists($key, self::$_LCNF)) {
if($subkey) // If we also have a subkey return the proper shit
2015-04-01 15:38:04 +00:00
return self::$_LCNF[$key][$subkey];
2015-04-01 15:41:06 +00:00
else // else we just return the default value
2015-04-01 15:38:04 +00:00
return self::$_LCNF[$key];
} else // If it doesn't exist trigger an error to avoid explosions
trigger_error('Unable to get local configuration value!', E_USER_ERROR);
}
2015-05-11 22:20:19 +00:00
2015-04-01 15:38:04 +00:00
// Dynamically set local configuration values, does not update the configuration file
public static function setLocalConfig($key, $subkey, $value) {
// Check if we also do a subkey
if($subkey) {
// If we do we make sure that the parent key is an array
if(!isset(self::$_LCNF[$key]))
self::$_LCNF[$key] = array();
// And then assign the value
self::$_LCNF[$key][$subkey] = $value;
} else // Otherwise we just straight up assign it
self::$_LCNF[$key] = $value;
}
// Get values from the configuration in the database
public static function getConfig($key) {
// Check if the key that we're looking for exists
if(array_key_exists($key, self::$_DCNF))
return self::$_DCNF[$key]; // Then return the value
else // If it doesn't exist trigger an error to avoid explosions
trigger_error('Unable to get configuration value!', E_USER_ERROR);
}
}