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

136 lines
3.3 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;
2015-06-29 00:36:37 +00:00
// Initialise configuration, does not contain database initialisation because explained below
2015-04-01 15:38:04 +00:00
public static function init($local) {
2015-05-29 19:27:45 +00:00
// Check if the configuration file exists
2015-06-27 19:29:37 +00:00
if(!file_exists($local)) {
2015-05-29 19:27:45 +00:00
trigger_error('Local configuration file does not exist', E_USER_ERROR);
2015-06-27 19:29:37 +00:00
}
2015-05-29 19:27:45 +00:00
// Attempt to load the configuration file
$local = parse_ini_file($local, true);
2015-04-01 15:38:04 +00:00
// Check if $local is an array and then store it in $_LCNF
2015-06-27 19:29:37 +00:00
if(is_array($local)) {
2015-04-01 15:38:04 +00:00
self::$_LCNF = $local;
2015-06-27 19:29:37 +00:00
} else {
// Otherwise trigger an error
2015-05-29 19:27:45 +00:00
trigger_error('Failed to load local configuration file, check the structure of the file to see if you made mistake somewhere', E_USER_ERROR);
2015-04-01 15:38:04 +00:00
2015-06-27 19:29:37 +00:00
}
2015-04-01 15:38:04 +00:00
}
/*
* 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();
2015-06-27 19:29:37 +00:00
// Properly sort the values
foreach($_DATA as $_CONF) {
2015-04-01 15:38:04 +00:00
$_DBCN[$_CONF[0]] = $_CONF[1];
2015-06-27 19:29:37 +00:00
}
2015-04-01 15:38:04 +00:00
// Assign the temporary array to the static one
self::$_DCNF = $_DBCN;
}
2015-06-29 00:36:37 +00:00
// Get values from the configuration on the file system
public static function getLocalConfig($key, $subkey = null) {
2015-04-01 15:38:04 +00:00
// Check if the key that we're looking for exists
2015-06-29 00:36:37 +00:00
if(array_key_exists($key, self::$_LCNF)) {
2015-06-27 19:29:37 +00:00
2015-06-29 00:36:37 +00:00
if($subkey) {
2015-06-27 19:29:37 +00:00
// If we also have a subkey return the proper data
2015-06-29 00:36:37 +00:00
return self::$_LCNF[$key][$subkey];
2015-06-27 19:29:37 +00:00
2015-06-29 00:36:37 +00:00
} else {
2015-06-27 19:29:37 +00:00
// else we just return the default value
2015-06-29 00:36:37 +00:00
return self::$_LCNF[$key];
2015-06-27 19:29:37 +00:00
}
2015-06-29 00:36:37 +00:00
} else {// If it doesn't exist trigger an error to avoid explosions
2015-06-27 19:29:37 +00:00
2015-06-29 00:36:37 +00:00
trigger_error('Unable to get local configuration value!', E_USER_ERROR);
2015-04-01 15:38:04 +00:00
2015-06-27 19:29:37 +00:00
}
2015-06-29 00:36:37 +00:00
}
2015-05-11 22:20:19 +00:00
2015-06-29 00:36:37 +00:00
// Dynamically set local configuration values, does not update the configuration file
public static function setLocalConfig($key, $subkey, $value) {
2015-04-01 15:38:04 +00:00
// Check if we also do a subkey
2015-06-29 00:36:37 +00:00
if($subkey) {
2015-04-01 15:38:04 +00:00
// If we do we make sure that the parent key is an array
2015-06-29 00:36:37 +00:00
if(!isset(self::$_LCNF[$key])) {
2015-06-27 19:29:37 +00:00
2015-06-29 00:36:37 +00:00
self::$_LCNF[$key] = array();
2015-04-01 15:38:04 +00:00
2015-06-27 19:29:37 +00:00
}
2015-04-01 15:38:04 +00:00
// And then assign the value
2015-06-29 00:36:37 +00:00
self::$_LCNF[$key][$subkey] = $value;
2015-04-01 15:38:04 +00:00
2015-06-29 00:36:37 +00:00
} else {
2015-06-27 19:29:37 +00:00
// Otherwise we just straight up assign it
2015-06-29 00:36:37 +00:00
self::$_LCNF[$key] = $value;
2015-04-01 15:38:04 +00:00
2015-06-27 19:29:37 +00:00
}
2015-06-29 00:36:37 +00:00
}
2015-04-01 15:38:04 +00:00
2015-06-29 00:36:37 +00:00
// Get values from the configuration in the database
public static function getConfig($key) {
2015-04-01 15:38:04 +00:00
// Check if the key that we're looking for exists
2015-06-29 00:36:37 +00:00
if(array_key_exists($key, self::$_DCNF)) {
2015-06-27 19:29:37 +00:00
// Then return the value
return self::$_DCNF[$key];
} else {
// Then return the value
2015-06-29 00:36:37 +00:00
trigger_error('Unable to get configuration value', E_USER_ERROR);
2015-06-27 19:29:37 +00:00
}
2015-04-01 15:38:04 +00:00
2015-06-29 00:36:37 +00:00
}
2015-04-01 15:38:04 +00:00
}