improved setting manager + setting caching
This commit is contained in:
parent
ba41cb3456
commit
471fc1dd79
@ -9,20 +9,20 @@ use ManiaControl\ManiaControl;
|
|||||||
/**
|
/**
|
||||||
* Class managing Settings and Configurations
|
* Class managing Settings and Configurations
|
||||||
*
|
*
|
||||||
* @author steeffeen & kremsy
|
* @author steeffeen & kremsy
|
||||||
* @copyright ManiaControl Copyright © 2014 ManiaControl Team
|
* @copyright ManiaControl Copyright © 2014 ManiaControl Team
|
||||||
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
||||||
*/
|
*/
|
||||||
class SettingManager implements CallbackListener {
|
class SettingManager implements CallbackListener {
|
||||||
/*
|
/*
|
||||||
* Constants
|
* Constants
|
||||||
*/
|
*/
|
||||||
const TABLE_SETTINGS = 'mc_settings';
|
const TABLE_SETTINGS = 'mc_settings';
|
||||||
const TYPE_STRING = 'string';
|
const TYPE_STRING = 'string';
|
||||||
const TYPE_INT = 'int';
|
const TYPE_INT = 'int';
|
||||||
const TYPE_REAL = 'real';
|
const TYPE_REAL = 'real';
|
||||||
const TYPE_BOOL = 'bool';
|
const TYPE_BOOL = 'bool';
|
||||||
const TYPE_ARRAY = 'array';
|
const TYPE_ARRAY = 'array';
|
||||||
const CB_SETTINGS_CHANGED = 'SettingManager.SettingsChanged';
|
const CB_SETTINGS_CHANGED = 'SettingManager.SettingsChanged';
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -57,9 +57,9 @@ class SettingManager implements CallbackListener {
|
|||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
private function initTables() {
|
private function initTables() {
|
||||||
$mysqli = $this->maniaControl->database->mysqli;
|
$mysqli = $this->maniaControl->database->mysqli;
|
||||||
$defaultType = "'" . self::TYPE_STRING . "'";
|
$defaultType = "'" . self::TYPE_STRING . "'";
|
||||||
$typeSet = $defaultType . ",'" . self::TYPE_INT . "','" . self::TYPE_REAL . "','" . self::TYPE_BOOL . "','" . self::TYPE_ARRAY . "'";
|
$typeSet = $defaultType . ",'" . self::TYPE_INT . "','" . self::TYPE_REAL . "','" . self::TYPE_BOOL . "','" . self::TYPE_ARRAY . "'";
|
||||||
$settingTableQuery = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_SETTINGS . "` (
|
$settingTableQuery = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_SETTINGS . "` (
|
||||||
`index` int(11) NOT NULL AUTO_INCREMENT,
|
`index` int(11) NOT NULL AUTO_INCREMENT,
|
||||||
`class` varchar(100) NOT NULL,
|
`class` varchar(100) NOT NULL,
|
||||||
@ -71,7 +71,7 @@ class SettingManager implements CallbackListener {
|
|||||||
PRIMARY KEY (`index`),
|
PRIMARY KEY (`index`),
|
||||||
UNIQUE KEY `settingId` (`class`,`setting`)
|
UNIQUE KEY `settingId` (`class`,`setting`)
|
||||||
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Settings and Configurations' AUTO_INCREMENT=1;";
|
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Settings and Configurations' AUTO_INCREMENT=1;";
|
||||||
$result = $mysqli->query($settingTableQuery);
|
$result = $mysqli->query($settingTableQuery);
|
||||||
if ($mysqli->error) {
|
if ($mysqli->error) {
|
||||||
trigger_error($mysqli->error, E_USER_ERROR);
|
trigger_error($mysqli->error, E_USER_ERROR);
|
||||||
}
|
}
|
||||||
@ -93,7 +93,7 @@ class SettingManager implements CallbackListener {
|
|||||||
return $param;
|
return $param;
|
||||||
}
|
}
|
||||||
trigger_error('Invalid class param. ' . $param);
|
trigger_error('Invalid class param. ' . $param);
|
||||||
return (string)$param;
|
return (string) $param;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -126,21 +126,21 @@ class SettingManager implements CallbackListener {
|
|||||||
* Cast a Setting to the given Type
|
* Cast a Setting to the given Type
|
||||||
*
|
*
|
||||||
* @param string $type
|
* @param string $type
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
private function castSetting($type, $value) {
|
private function castSetting($type, $value) {
|
||||||
if ($type === self::TYPE_INT) {
|
if ($type === self::TYPE_INT) {
|
||||||
return (int)$value;
|
return (int) $value;
|
||||||
}
|
}
|
||||||
if ($type === self::TYPE_REAL) {
|
if ($type === self::TYPE_REAL) {
|
||||||
return (float)$value;
|
return (float) $value;
|
||||||
}
|
}
|
||||||
if ($type === self::TYPE_BOOL) {
|
if ($type === self::TYPE_BOOL) {
|
||||||
return (bool)$value;
|
return (bool) $value;
|
||||||
}
|
}
|
||||||
if ($type === self::TYPE_STRING) {
|
if ($type === self::TYPE_STRING) {
|
||||||
return (string)$value;
|
return (string) $value;
|
||||||
}
|
}
|
||||||
if ($type === self::TYPE_ARRAY) {
|
if ($type === self::TYPE_ARRAY) {
|
||||||
return explode($this->arrayDelimiter, $value);
|
return explode($this->arrayDelimiter, $value);
|
||||||
@ -152,7 +152,7 @@ class SettingManager implements CallbackListener {
|
|||||||
/**
|
/**
|
||||||
* Format a Setting for saving it to the Database
|
* Format a Setting for saving it to the Database
|
||||||
*
|
*
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param string $type
|
* @param string $type
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
@ -172,20 +172,20 @@ class SettingManager implements CallbackListener {
|
|||||||
/**
|
/**
|
||||||
* Initialize a Setting for the given Object
|
* Initialize a Setting for the given Object
|
||||||
*
|
*
|
||||||
* @param mixed $object
|
* @param mixed $object
|
||||||
* @param string $settingName
|
* @param string $settingName
|
||||||
* @param mixed $default
|
* @param mixed $default
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function initSetting($object, $settingName, $default) {
|
public function initSetting($object, $settingName, $default) {
|
||||||
if ($default === null || is_object($default)) {
|
if (is_null($default) || is_object($default)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$className = $this->getClassName($object);
|
$className = $this->getClassName($object);
|
||||||
$type = $this->getType($default);
|
$type = $this->getType($default);
|
||||||
$default = $this->formatSetting($default, $type);
|
$default = $this->formatSetting($default, $type);
|
||||||
$mysqli = $this->maniaControl->database->mysqli;
|
$mysqli = $this->maniaControl->database->mysqli;
|
||||||
$settingQuery = "INSERT INTO `" . self::TABLE_SETTINGS . "` (
|
$settingQuery = "INSERT INTO `" . self::TABLE_SETTINGS . "` (
|
||||||
`class`,
|
`class`,
|
||||||
`setting`,
|
`setting`,
|
||||||
`type`,
|
`type`,
|
||||||
@ -219,15 +219,15 @@ class SettingManager implements CallbackListener {
|
|||||||
/**
|
/**
|
||||||
* Get a Setting by its Index
|
* Get a Setting by its Index
|
||||||
*
|
*
|
||||||
* @param int $settingIndex
|
* @param int $settingIndex
|
||||||
* @param mixed $default
|
* @param mixed $default
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function getSettingByIndex($settingIndex, $default = false) {
|
public function getSettingByIndex($settingIndex, $default = false) {
|
||||||
$mysqli = $this->maniaControl->database->mysqli;
|
$mysqli = $this->maniaControl->database->mysqli;
|
||||||
$settingQuery = "SELECT * FROM `" . self::TABLE_SETTINGS . "`
|
$settingQuery = "SELECT * FROM `" . self::TABLE_SETTINGS . "`
|
||||||
WHERE `index` = {$settingIndex};";
|
WHERE `index` = {$settingIndex};";
|
||||||
$result = $mysqli->query($settingQuery);
|
$result = $mysqli->query($settingQuery);
|
||||||
if (!$result) {
|
if (!$result) {
|
||||||
trigger_error($mysqli->error);
|
trigger_error($mysqli->error);
|
||||||
return null;
|
return null;
|
||||||
@ -244,21 +244,21 @@ class SettingManager implements CallbackListener {
|
|||||||
/**
|
/**
|
||||||
* Get Setting by Name for the given Object
|
* Get Setting by Name for the given Object
|
||||||
*
|
*
|
||||||
* @param mixed $object
|
* @param mixed $object
|
||||||
* @param string $settingName
|
* @param string $settingName
|
||||||
* @param mixed $default
|
* @param mixed $default
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function getSetting($object, $settingName, $default = null) {
|
public function getSetting($object, $settingName, $default = null) {
|
||||||
$className = $this->getClassName($object);
|
$className = $this->getClassName($object);
|
||||||
|
|
||||||
//Check if setting is already in the ram
|
// Check if setting is already in the ram
|
||||||
if (isset($this->storedSettings[$className . $settingName])) {
|
if (isset($this->storedSettings[$className . $settingName])) {
|
||||||
return $this->storedSettings[$className . $settingName];
|
return $this->storedSettings[$className . $settingName];
|
||||||
}
|
}
|
||||||
|
|
||||||
$mysqli = $this->maniaControl->database->mysqli;
|
$mysqli = $this->maniaControl->database->mysqli;
|
||||||
$settingQuery = "SELECT `type`, `value` FROM `" . self::TABLE_SETTINGS . "`
|
$settingQuery = "SELECT `type`, `value` FROM `" . self::TABLE_SETTINGS . "`
|
||||||
WHERE `class` = ?
|
WHERE `class` = ?
|
||||||
AND `setting` = ?;";
|
AND `setting` = ?;";
|
||||||
$settingStatement = $mysqli->prepare($settingQuery);
|
$settingStatement = $mysqli->prepare($settingQuery);
|
||||||
@ -283,7 +283,7 @@ class SettingManager implements CallbackListener {
|
|||||||
$settingStatement->close();
|
$settingStatement->close();
|
||||||
$setting = $this->castSetting($type, $value);
|
$setting = $this->castSetting($type, $value);
|
||||||
|
|
||||||
//Store setting in the ram
|
// Store setting in the ram
|
||||||
$this->storedSettings[$className . $settingName] = $setting;
|
$this->storedSettings[$className . $settingName] = $setting;
|
||||||
return $setting;
|
return $setting;
|
||||||
}
|
}
|
||||||
@ -291,15 +291,18 @@ class SettingManager implements CallbackListener {
|
|||||||
/**
|
/**
|
||||||
* Set a Setting for the given Object
|
* Set a Setting for the given Object
|
||||||
*
|
*
|
||||||
* @param mixed $object
|
* @param mixed $object
|
||||||
* @param string $settingName
|
* @param string $settingName
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function setSetting($object, $settingName, $value) {
|
public function setSetting($object, $settingName, $value) {
|
||||||
$className = $this->getClassName($object);
|
$className = $this->getClassName($object);
|
||||||
$mysqli = $this->maniaControl->database->mysqli;
|
|
||||||
$settingQuery = "UPDATE `" . self::TABLE_SETTINGS . "`
|
var_dump($className, $settingName, $value);
|
||||||
|
|
||||||
|
$mysqli = $this->maniaControl->database->mysqli;
|
||||||
|
$settingQuery = "UPDATE `" . self::TABLE_SETTINGS . "`
|
||||||
SET `value` = ?
|
SET `value` = ?
|
||||||
WHERE `class` = ?
|
WHERE `class` = ?
|
||||||
AND `setting` = ?;";
|
AND `setting` = ?;";
|
||||||
@ -308,8 +311,8 @@ class SettingManager implements CallbackListener {
|
|||||||
trigger_error($mysqli->error);
|
trigger_error($mysqli->error);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$value = $this->formatSetting($value);
|
$setting = $this->formatSetting($value);
|
||||||
$settingStatement->bind_param('sss', $value, $className, $settingName);
|
$settingStatement->bind_param('sss', $setting, $className, $settingName);
|
||||||
$success = $settingStatement->execute();
|
$success = $settingStatement->execute();
|
||||||
if ($settingStatement->error) {
|
if ($settingStatement->error) {
|
||||||
trigger_error($settingStatement->error);
|
trigger_error($settingStatement->error);
|
||||||
@ -318,12 +321,9 @@ class SettingManager implements CallbackListener {
|
|||||||
}
|
}
|
||||||
$settingStatement->close();
|
$settingStatement->close();
|
||||||
|
|
||||||
//FIXME store changed value
|
$this->storedSettings[$className . $settingName] = $value;
|
||||||
if (isset($this->storedSettings[$className . $settingName])) {
|
|
||||||
unset($this->storedSettings[$className . $settingName]);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Trigger settings changed Callback
|
// Trigger settings changed Callback
|
||||||
$this->maniaControl->callbackManager->triggerCallback(self::CB_SETTINGS_CHANGED, $className, $settingName, $value);
|
$this->maniaControl->callbackManager->triggerCallback(self::CB_SETTINGS_CHANGED, $className, $settingName, $value);
|
||||||
return $success;
|
return $success;
|
||||||
}
|
}
|
||||||
@ -331,14 +331,14 @@ class SettingManager implements CallbackListener {
|
|||||||
/**
|
/**
|
||||||
* Reset a Setting to its default Value
|
* Reset a Setting to its default Value
|
||||||
*
|
*
|
||||||
* @param mixed $object
|
* @param mixed $object
|
||||||
* @param string $settingName
|
* @param string $settingName
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function resetSetting($object, $settingName) {
|
public function resetSetting($object, $settingName) {
|
||||||
$className = $this->getClassName($object);
|
$className = $this->getClassName($object);
|
||||||
$mysqli = $this->maniaControl->database->mysqli;
|
$mysqli = $this->maniaControl->database->mysqli;
|
||||||
$settingQuery = "UPDATE `" . self::TABLE_SETTINGS . "`
|
$settingQuery = "UPDATE `" . self::TABLE_SETTINGS . "`
|
||||||
SET `value` = `default`
|
SET `value` = `default`
|
||||||
WHERE `class` = ?
|
WHERE `class` = ?
|
||||||
AND `setting` = ?;";
|
AND `setting` = ?;";
|
||||||
@ -355,20 +355,23 @@ class SettingManager implements CallbackListener {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$settingStatement->close();
|
$settingStatement->close();
|
||||||
|
if (isset($this->storedSettings[$className . $settingName])) {
|
||||||
|
unset($this->storedSettings[$className . $settingName]);
|
||||||
|
}
|
||||||
return $success;
|
return $success;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete a Setting
|
* Delete a Setting
|
||||||
*
|
*
|
||||||
* @param mixed $object
|
* @param mixed $object
|
||||||
* @param string $settingName
|
* @param string $settingName
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function deleteSetting($object, $settingName) {
|
public function deleteSetting($object, $settingName) {
|
||||||
$className = $this->getClassName($object);
|
$className = $this->getClassName($object);
|
||||||
$mysqli = $this->maniaControl->database->mysqli;
|
$mysqli = $this->maniaControl->database->mysqli;
|
||||||
$settingQuery = "DELETE FROM `" . self::TABLE_SETTINGS . "`
|
$settingQuery = "DELETE FROM `" . self::TABLE_SETTINGS . "`
|
||||||
WHERE `class` = ?
|
WHERE `class` = ?
|
||||||
AND `setting` = ?;";
|
AND `setting` = ?;";
|
||||||
$settingStatement = $mysqli->prepare($settingQuery);
|
$settingStatement = $mysqli->prepare($settingQuery);
|
||||||
@ -384,6 +387,9 @@ class SettingManager implements CallbackListener {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$settingStatement->close();
|
$settingStatement->close();
|
||||||
|
if (isset($this->storedSettings[$className . $settingName])) {
|
||||||
|
unset($this->storedSettings[$className . $settingName]);
|
||||||
|
}
|
||||||
return $success;
|
return $success;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -395,16 +401,16 @@ class SettingManager implements CallbackListener {
|
|||||||
*/
|
*/
|
||||||
public function getSettingsByClass($object) {
|
public function getSettingsByClass($object) {
|
||||||
$className = $this->getClassName($object);
|
$className = $this->getClassName($object);
|
||||||
$mysqli = $this->maniaControl->database->mysqli;
|
$mysqli = $this->maniaControl->database->mysqli;
|
||||||
$query = "SELECT * FROM `" . self::TABLE_SETTINGS . "` WHERE `class`= '" . $mysqli->escape_string($className) . "'
|
$query = "SELECT * FROM `" . self::TABLE_SETTINGS . "` WHERE `class`= '" . $mysqli->escape_string($className) . "'
|
||||||
ORDER BY `setting` ASC;";
|
ORDER BY `setting` ASC;";
|
||||||
$result = $mysqli->query($query);
|
$result = $mysqli->query($query);
|
||||||
if ($mysqli->error) {
|
if ($mysqli->error) {
|
||||||
trigger_error($mysqli->error);
|
trigger_error($mysqli->error);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
$settings = array();
|
$settings = array();
|
||||||
while($setting = $result->fetch_object()) {
|
while ($setting = $result->fetch_object()) {
|
||||||
$settings[$setting->index] = $setting;
|
$settings[$setting->index] = $setting;
|
||||||
}
|
}
|
||||||
$result->free();
|
$result->free();
|
||||||
@ -418,7 +424,7 @@ class SettingManager implements CallbackListener {
|
|||||||
*/
|
*/
|
||||||
public function getSettings() {
|
public function getSettings() {
|
||||||
$mysqli = $this->maniaControl->database->mysqli;
|
$mysqli = $this->maniaControl->database->mysqli;
|
||||||
$query = "SELECT * FROM `" . self::TABLE_SETTINGS . "`
|
$query = "SELECT * FROM `" . self::TABLE_SETTINGS . "`
|
||||||
ORDER BY `class` ASC, `setting` ASC;";
|
ORDER BY `class` ASC, `setting` ASC;";
|
||||||
$result = $mysqli->query($query);
|
$result = $mysqli->query($query);
|
||||||
if ($mysqli->error) {
|
if ($mysqli->error) {
|
||||||
@ -426,7 +432,7 @@ class SettingManager implements CallbackListener {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
$settings = array();
|
$settings = array();
|
||||||
while($setting = $result->fetch_object()) {
|
while ($setting = $result->fetch_object()) {
|
||||||
$settings[$setting->index] = $setting;
|
$settings[$setting->index] = $setting;
|
||||||
}
|
}
|
||||||
$result->free();
|
$result->free();
|
||||||
@ -439,8 +445,8 @@ class SettingManager implements CallbackListener {
|
|||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
private function deleteUnusedSettings() {
|
private function deleteUnusedSettings() {
|
||||||
$mysqli = $this->maniaControl->database->mysqli;
|
$mysqli = $this->maniaControl->database->mysqli;
|
||||||
$settingQuery = "DELETE FROM `" . self::TABLE_SETTINGS . "`
|
$settingQuery = "DELETE FROM `" . self::TABLE_SETTINGS . "`
|
||||||
WHERE `changed` < NOW() - INTERVAL 1 HOUR;";
|
WHERE `changed` < NOW() - INTERVAL 1 HOUR;";
|
||||||
$settingStatement = $mysqli->prepare($settingQuery);
|
$settingStatement = $mysqli->prepare($settingQuery);
|
||||||
if ($mysqli->error) {
|
if ($mysqli->error) {
|
||||||
@ -454,6 +460,7 @@ class SettingManager implements CallbackListener {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$settingStatement->close();
|
$settingStatement->close();
|
||||||
|
$this->storedSettings = array();
|
||||||
return $success;
|
return $success;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user