cache settings

This commit is contained in:
kremsy 2014-04-20 19:25:06 +02:00 committed by Steffen Schröder
parent e40df45cd9
commit fa1f8663f3

View File

@ -9,9 +9,9 @@ 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 {
/* /*
@ -30,6 +30,7 @@ class SettingManager implements CallbackListener {
*/ */
private $maniaControl = null; private $maniaControl = null;
private $arrayDelimiter = ';;'; private $arrayDelimiter = ';;';
private $storedSettings = array();
/** /**
* Construct a new Setting Manager * Construct a new Setting Manager
@ -56,10 +57,10 @@ 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,
`setting` varchar(150) NOT NULL, `setting` varchar(150) NOT NULL,
@ -70,21 +71,12 @@ 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);
} }
// TODO: remove before release return $result;
$settingTableChangesQuery = "ALTER TABLE `".self::TABLE_SETTINGS."`
MODIFY `class` VARCHAR(100) NOT NULL,
MODIFY `setting` VARCHAR(150) NOT NULL;";
$result2 = $mysqli->query($settingTableChangesQuery);
if ($mysqli->error) {
trigger_error($mysqli->error);
}
return $result && $result2;
} }
/** /**
@ -258,7 +250,13 @@ class SettingManager implements CallbackListener {
* @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
if (isset($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` = ?
@ -284,6 +282,9 @@ 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
$this->storedSettings[$className . $settingName] = $setting;
return $setting; return $setting;
} }
@ -317,6 +318,11 @@ class SettingManager implements CallbackListener {
} }
$settingStatement->close(); $settingStatement->close();
//FIXME store changed 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;