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/libraries/Config.php

137 lines
3.5 KiB
PHP
Raw Normal View History

2015-04-01 15:38:04 +00:00
<?php
2016-02-03 22:22:56 +00:00
/**
* Holds the configuration manager.
*
* @package Sakura
*/
2015-04-01 15:38:04 +00:00
namespace Sakura;
/**
2016-02-02 21:04:15 +00:00
* Handles both the local and database stored configuration sides of Sakura.
*
* @package Sakura
2016-02-02 21:04:15 +00:00
* @author Julian van de Groep <me@flash.moe>
*/
class Config
{
2016-02-02 21:04:15 +00:00
/**
* Container for the parsed local configuration.
*
* @var array
*/
private static $local = [];
2016-02-02 21:04:15 +00:00
/**
* Container for the configuration stored in the database.
*
* @var array
*/
private static $database = [];
2015-04-01 15:38:04 +00:00
2016-02-02 21:04:15 +00:00
/**
* Initialiser, parses the local configuration.
*
* @param string $local Path to the configuration file.
*/
public static function init($local)
{
2015-04-01 15:38:04 +00:00
2015-05-29 19:27:45 +00:00
// Check if the configuration file exists
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);
// Check if $local is an array and then store it in $local
if (is_array($local)) {
self::$local = $local;
2015-06-27 19:29:37 +00:00
} else {
// Otherwise trigger an error
2015-09-14 21:41:43 +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-06-27 19:29:37 +00:00
}
2015-04-01 15:38:04 +00:00
}
2016-02-02 21:04:15 +00:00
/**
* Fetch configuration values from the database.
2015-04-01 15:38:04 +00:00
*/
public static function initDB()
{
2015-04-01 15:38:04 +00:00
// Get config table from the database
$_DATA = Database::fetch('config', true);
// Create variable to temporarily store values in
$_DBCN = [];
2015-04-01 15:38:04 +00:00
2015-06-27 19:29:37 +00:00
// Properly sort the values
foreach ($_DATA as $_CONF) {
2015-08-19 02:37:45 +00:00
$_DBCN[$_CONF['config_name']] = $_CONF['config_value'];
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::$database = $_DBCN;
2015-04-01 15:38:04 +00:00
}
2016-02-02 21:04:15 +00:00
/**
* Get a value from the local configuration file.
*
* @param string $key Configuration section.
* @param string $subkey Configuration key.
*
* @return string Configuration value.
*/
2015-12-04 14:19:10 +00:00
public static function local($key, $subkey = null)
{
2015-04-01 15:38:04 +00:00
// Check if the key that we're looking for exists
if (array_key_exists($key, self::$local)) {
if ($subkey) {
2015-06-27 19:29:37 +00:00
// If we also have a subkey return the proper data
return self::$local[$key][$subkey];
2015-06-27 19:29:37 +00:00
}
// else we just return the default value
return self::$local[$key];
2015-06-27 19:29:37 +00:00
}
// If it doesn't exist trigger an error to avoid explosions
2015-09-14 21:41:43 +00:00
trigger_error(
'Unable to get local configuration value "' . $key . '"',
E_USER_ERROR
);
2015-12-04 14:19:10 +00:00
return null;
2015-06-29 00:36:37 +00:00
}
2015-04-01 15:38:04 +00:00
2016-02-02 21:04:15 +00:00
/**
* Get a configuration value from the database.
*
* @param string $key Configuration key.
* @param bool $returnNull Unused value, only exists to prevent explosions.
*
* @return string Configuration value.
*/
2015-12-04 14:19:10 +00:00
public static function get($key, $returnNull = false)
{
2015-04-01 15:38:04 +00:00
// Check if the key that we're looking for exists
if (array_key_exists($key, self::$database)) {
2015-06-27 19:29:37 +00:00
// Then return the value
return self::$database[$key];
2015-06-27 19:29:37 +00:00
}
2015-04-01 15:38:04 +00:00
// Then return the value
2015-09-14 21:41:43 +00:00
trigger_error(
'Unable to get configuration value "' . $key . '"',
E_USER_ERROR
);
2015-12-04 14:19:10 +00:00
return null;
}
2015-04-01 15:38:04 +00:00
}