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

@ -8,23 +8,23 @@ 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';
/* /*
* Private Properties * Private Properties
*/ */
@ -34,13 +34,13 @@ class SettingManager implements CallbackListener {
/** /**
* Construct a new Setting Manager * Construct a new Setting Manager
* *
* @param ManiaControl $maniaControl * @param ManiaControl $maniaControl
*/ */
public function __construct(ManiaControl $maniaControl) { public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl; $this->maniaControl = $maniaControl;
$this->initTables(); $this->initTables();
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_AFTERINIT, $this, 'handleAfterInit'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_AFTERINIT, $this, 'handleAfterInit');
} }
@ -53,13 +53,13 @@ class SettingManager implements CallbackListener {
/** /**
* Initialize necessary Database Tables * Initialize necessary Database Tables
* *
* @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,17 +71,17 @@ 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);
} }
return $result; return $result;
} }
/** /**
* Get Class Name of a Parameter * Get Class Name of a Parameter
* *
* @param mixed $param * @param mixed $param
* @return string * @return string
*/ */
@ -93,12 +93,12 @@ 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;
} }
/** /**
* Get Type of a Parameter * Get Type of a Parameter
* *
* @param mixed $param * @param mixed $param
* @return string * @return string
*/ */
@ -124,23 +124,23 @@ 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);
@ -151,8 +151,8 @@ 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
*/ */
@ -171,21 +171,21 @@ 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`,
@ -218,16 +218,16 @@ 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;
@ -243,22 +243,22 @@ 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);
@ -282,24 +282,27 @@ class SettingManager implements CallbackListener {
$settingStatement->free_result(); $settingStatement->free_result();
$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;
} }
/** /**
* 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);
@ -317,28 +320,25 @@ class SettingManager implements CallbackListener {
return false; return false;
} }
$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;
} }
/** /**
* 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,27 +387,30 @@ 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;
} }
/** /**
* Get all Settings for the given Class * Get all Settings for the given Class
* *
* @param mixed $object * @param mixed $object
* @return array * @return array
*/ */
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();
@ -413,12 +419,12 @@ class SettingManager implements CallbackListener {
/** /**
* Get all settings * Get all settings
* *
* @return array * @return array
*/ */
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();
@ -435,12 +441,12 @@ class SettingManager implements CallbackListener {
/** /**
* Delete all unused Settings that haven't been initialized during the current Startup * Delete all unused Settings that haven't been initialized during the current Startup
* *
* @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;
} }
} }