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;
|
|
|
|
|
2015-10-18 19:06:30 +00:00
|
|
|
/**
|
2016-02-02 21:04:15 +00:00
|
|
|
* Handles both the local and database stored configuration sides of Sakura.
|
|
|
|
*
|
2015-10-18 19:06:30 +00:00
|
|
|
* @package Sakura
|
2016-02-02 21:04:15 +00:00
|
|
|
* @author Julian van de Groep <me@flash.moe>
|
2015-10-18 19:06:30 +00:00
|
|
|
*/
|
2015-11-06 22:30:37 +00:00
|
|
|
class Config
|
2015-09-14 20:51:23 +00:00
|
|
|
{
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Container for the parsed local configuration.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2015-09-14 20:51:23 +00:00
|
|
|
private static $local = [];
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
2016-02-15 21:20:46 +00:00
|
|
|
* Cache for the configuration stored in the database.
|
2016-02-02 21:04:15 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2015-09-14 20:51:23 +00:00
|
|
|
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.
|
|
|
|
*/
|
2015-09-14 20:51:23 +00:00
|
|
|
public static function init($local)
|
|
|
|
{
|
2015-05-29 19:27:45 +00:00
|
|
|
// Check if the configuration file exists
|
2015-09-14 20:51:23 +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-09-14 20:51:23 +00:00
|
|
|
// 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
|
|
|
/**
|
|
|
|
* 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-09-14 20:51:23 +00:00
|
|
|
{
|
2015-04-01 15:38:04 +00:00
|
|
|
// Check if the key that we're looking for exists
|
2015-09-14 20:51:23 +00:00
|
|
|
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
|
2015-09-14 20:51:23 +00:00
|
|
|
return self::$local[$key][$subkey];
|
2015-06-27 19:29:37 +00:00
|
|
|
}
|
|
|
|
|
2015-09-14 20:51:23 +00:00
|
|
|
// else we just return the default value
|
|
|
|
return self::$local[$key];
|
2015-06-27 19:29:37 +00:00
|
|
|
}
|
|
|
|
|
2015-09-14 20:51:23 +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-09-14 20:51:23 +00:00
|
|
|
{
|
2015-04-01 15:38:04 +00:00
|
|
|
// Check if the key that we're looking for exists
|
2015-09-14 20:51:23 +00:00
|
|
|
if (array_key_exists($key, self::$database)) {
|
2015-06-27 19:29:37 +00:00
|
|
|
// Then return the value
|
2015-09-14 20:51:23 +00:00
|
|
|
return self::$database[$key];
|
2016-02-15 21:20:46 +00:00
|
|
|
} else {
|
|
|
|
$value = Database::fetch('config', false, ['config_name' => [$key, '=']]);
|
|
|
|
if ($value) {
|
|
|
|
self::$database[$key] = $value['config_value'];
|
|
|
|
return self::$database[$key];
|
|
|
|
}
|
2015-06-27 19:29:37 +00:00
|
|
|
}
|
2015-04-01 15:38:04 +00:00
|
|
|
|
2015-09-14 20:51:23 +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-09-14 20:51:23 +00:00
|
|
|
}
|
2015-04-01 15:38:04 +00:00
|
|
|
}
|