improved setting manager + setting caching

This commit is contained in:
Steffen Schröder 2014-04-30 02:27:17 +02:00
parent ba41cb3456
commit 471fc1dd79

View File

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