2015-04-01 15:38:04 +00:00
|
|
|
<?php
|
2016-02-03 22:22:56 +00:00
|
|
|
/**
|
|
|
|
* Holds the configuration manager.
|
2016-02-20 11:20:58 +00:00
|
|
|
*
|
2016-02-03 22:22:56 +00:00
|
|
|
* @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.
|
2016-02-20 11:20:58 +00:00
|
|
|
*
|
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.
|
2016-02-20 11:20:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @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-20 11:20:58 +00:00
|
|
|
*
|
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.
|
2016-02-20 11:20:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @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.
|
2016-02-20 11:20:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @param string $key Configuration section.
|
|
|
|
* @param string $subkey Configuration key.
|
2016-02-20 11:20:58 +00:00
|
|
|
*
|
2016-02-18 23:28:44 +00:00
|
|
|
* @return array|string Configuration value.
|
2016-02-02 21:04:15 +00:00
|
|
|
*/
|
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.
|
2016-02-20 11:20:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @param string $key Configuration key.
|
2016-02-20 11:20:58 +00:00
|
|
|
* @param string $default Value that gets used when the value doesn't exist.
|
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @return string Configuration value.
|
|
|
|
*/
|
2016-02-20 11:20:58 +00:00
|
|
|
public static function get($key, $default = 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::$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 {
|
2016-02-18 23:28:44 +00:00
|
|
|
$value = DB::prepare('SELECT * FROM `{prefix}config` WHERE `config_name` = :name');
|
|
|
|
$value->execute([
|
|
|
|
'name' => $key,
|
|
|
|
]);
|
|
|
|
$value = $value->fetch();
|
2016-02-15 21:20:46 +00:00
|
|
|
if ($value) {
|
2016-02-18 23:28:44 +00:00
|
|
|
self::$database[$key] = $value->config_value;
|
2016-02-15 21:20:46 +00:00
|
|
|
return self::$database[$key];
|
|
|
|
}
|
2015-06-27 19:29:37 +00:00
|
|
|
}
|
2015-04-01 15:38:04 +00:00
|
|
|
|
2016-02-20 11:20:58 +00:00
|
|
|
// If we fell all the way down here set the bundled default value
|
|
|
|
Config::set($key, $default);
|
|
|
|
|
|
|
|
// And then return default that value
|
|
|
|
return $default;
|
2015-09-14 20:51:23 +00:00
|
|
|
}
|
2016-02-18 23:28:44 +00:00
|
|
|
|
|
|
|
public static function set($key, $value)
|
|
|
|
{
|
|
|
|
// Unset the cached copy
|
|
|
|
if (array_key_exists($key, self::$database)) {
|
|
|
|
unset(self::$database[$key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the value already exists
|
|
|
|
$exists = DB::prepare('SELECT * FROM `{prefix}config` WHERE `config_name` = :name');
|
|
|
|
$exists->execute([
|
|
|
|
'name' => $key,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// If it exists run an update
|
|
|
|
if ($exists->rowCount()) {
|
|
|
|
$set = DB::prepare('UPDATE `{prefix}config` SET `config_value` = :value WHERE `config_name` = :name');
|
|
|
|
} else {
|
|
|
|
$set = DB::prepare('INSERT INTO `{prefix}config` (`config_name`, `config_value`) VALUES (:name, :value)');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the setter
|
|
|
|
$set->execute([
|
|
|
|
'name' => $key,
|
|
|
|
'value' => $value,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Return the value
|
|
|
|
return $value;
|
|
|
|
}
|
2015-04-01 15:38:04 +00:00
|
|
|
}
|