fixed setting manager namespace

This commit is contained in:
Steffen Schröder 2014-01-27 09:07:43 +01:00
parent dfe07ae8dc
commit e4fa085940

View File

@ -1,6 +1,8 @@
<?php <?php
namespace ManiaControl; namespace ManiaControl\Settings;
use ManiaControl\ManiaControl;
/** /**
* Class managing settings and configurations * Class managing settings and configurations
@ -12,12 +14,12 @@ class SettingManager {
* 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';
/** /**
* Private properties * Private properties
*/ */
@ -40,14 +42,14 @@ class SettingManager {
* @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; $typeSet = $defaultType;
$typeSet .= ",'" . self::TYPE_INT . "'"; $typeSet .= ",'" . self::TYPE_INT . "'";
$typeSet .= ",'" . self::TYPE_REAL . "'"; $typeSet .= ",'" . self::TYPE_REAL . "'";
$typeSet .= ",'" . self::TYPE_BOOL . "'"; $typeSet .= ",'" . self::TYPE_BOOL . "'";
$typeSet .= ",'" . self::TYPE_ARRAY . "'"; $typeSet .= ",'" . 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(50) COLLATE utf8_unicode_ci NOT NULL, `class` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`setting` varchar(50) COLLATE utf8_unicode_ci NOT NULL, `setting` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
@ -59,12 +61,12 @@ class SettingManager {
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;";
$settingTableStatement = $mysqli->prepare($settingTableQuery); $settingTableStatement = $mysqli->prepare($settingTableQuery);
if($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR); trigger_error($mysqli->error, E_USER_ERROR);
return false; return false;
} }
$settingTableStatement->execute(); $settingTableStatement->execute();
if($settingTableStatement->error) { if ($settingTableStatement->error) {
trigger_error($settingTableStatement->error, E_USER_ERROR); trigger_error($settingTableStatement->error, E_USER_ERROR);
return false; return false;
} }
@ -79,14 +81,14 @@ class SettingManager {
* @return string * @return string
*/ */
private function getClassName($object) { private function getClassName($object) {
if(is_object($object)) { if (is_object($object)) {
return get_class($object); return get_class($object);
} }
if(is_string($object)) { if (is_string($object)) {
return $object; return $object;
} }
trigger_error('Invalid class param. ' . $object); trigger_error('Invalid class param. ' . $object);
return (string)$object; return (string) $object;
} }
/** /**
@ -96,19 +98,19 @@ class SettingManager {
* @return string * @return string
*/ */
private function getType($param) { private function getType($param) {
if(is_int($param)) { if (is_int($param)) {
return self::TYPE_INT; return self::TYPE_INT;
} }
if(is_real($param)) { if (is_real($param)) {
return self::TYPE_REAL; return self::TYPE_REAL;
} }
if(is_bool($param)) { if (is_bool($param)) {
return self::TYPE_BOOL; return self::TYPE_BOOL;
} }
if(is_string($param)) { if (is_string($param)) {
return self::TYPE_STRING; return self::TYPE_STRING;
} }
if(is_array($param)) { if (is_array($param)) {
return self::TYPE_ARRAY; return self::TYPE_ARRAY;
} }
trigger_error('Unsupported setting type. ' . print_r($param, true)); trigger_error('Unsupported setting type. ' . print_r($param, true));
@ -119,23 +121,23 @@ class SettingManager {
* 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);
} }
trigger_error('Unsupported setting type. ' . print_r($type, true)); trigger_error('Unsupported setting type. ' . print_r($type, true));
@ -145,18 +147,18 @@ class SettingManager {
/** /**
* 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
*/ */
private function formatSetting($value, $type = null) { private function formatSetting($value, $type = null) {
if($type === null) { if ($type === null) {
$type = $this->getType($value); $type = $this->getType($value);
} }
if($type === self::TYPE_ARRAY) { if ($type === self::TYPE_ARRAY) {
return implode($this->arrayDelimiter, $value); return implode($this->arrayDelimiter, $value);
} }
if($type === self::TYPE_BOOL) { if ($type === self::TYPE_BOOL) {
return ($value ? 1 : 0); return ($value ? 1 : 0);
} }
return $value; return $value;
@ -167,18 +169,18 @@ class SettingManager {
* *
* @param object $object * @param object $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 ($default === null || 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`,
@ -193,13 +195,13 @@ class SettingManager {
`value` = IF(`default` = VALUES(`default`), `value`, VALUES(`default`)), `value` = IF(`default` = VALUES(`default`), `value`, VALUES(`default`)),
`default` = VALUES(`default`);"; `default` = VALUES(`default`);";
$settingStatement = $mysqli->prepare($settingQuery); $settingStatement = $mysqli->prepare($settingQuery);
if($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error); trigger_error($mysqli->error);
return false; return false;
} }
$settingStatement->bind_param('ssss', $className, $settingName, $type, $default); $settingStatement->bind_param('ssss', $className, $settingName, $type, $default);
$success = $settingStatement->execute(); $success = $settingStatement->execute();
if($settingStatement->error) { if ($settingStatement->error) {
trigger_error($settingStatement->error); trigger_error($settingStatement->error);
$settingStatement->close(); $settingStatement->close();
return false; return false;
@ -211,29 +213,28 @@ class SettingManager {
/** /**
* Get a Setting by its index * Get a Setting by its index
* *
* @param $settingIndex * @param $settingIndex
* @param bool $default * @param bool $default
* @internal param null $default * @internal param null $default
* @internal param $className * @internal param $className
* @internal param \ManiaControl\gIndex $settin * @internal param \ManiaControl\gIndex $settin
* @return mixed|null * @return mixed null
*/ */
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 false; return false;
} }
$row = $result->fetch_object(); $row = $result->fetch_object();
$result->close(); $result->close();
return $row; return $row;
} }
/** /**
* Gets a Setting Via it's class name * Gets a Setting Via it's class name
* *
@ -242,23 +243,23 @@ class SettingManager {
* @param $value * @param $value
*/ */
public function getSettingByClassName($className, $settingName, $default = null) { public function getSettingByClassName($className, $settingName, $default = null) {
$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);
if($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error); trigger_error($mysqli->error);
return null; return null;
} }
$settingStatement->bind_param('ss', $className, $settingName); $settingStatement->bind_param('ss', $className, $settingName);
$settingStatement->execute(); $settingStatement->execute();
if($settingStatement->error) { if ($settingStatement->error) {
trigger_error($settingStatement->error); trigger_error($settingStatement->error);
return null; return null;
} }
$settingStatement->store_result(); $settingStatement->store_result();
if($settingStatement->num_rows <= 0) { if ($settingStatement->num_rows <= 0) {
$this->updateSetting($className, $settingName, $default); $this->updateSetting($className, $settingName, $default);
return $default; return $default;
} }
@ -275,7 +276,7 @@ class SettingManager {
* *
* @param object $object * @param object $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) {
@ -292,20 +293,20 @@ class SettingManager {
* @return bool * @return bool
*/ */
public function updateSetting($className, $settingName, $value) { public function updateSetting($className, $settingName, $value) {
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "UPDATE `" . self::TABLE_SETTINGS . "` $settingQuery = "UPDATE `" . self::TABLE_SETTINGS . "`
SET `value` = ? SET `value` = ?
WHERE `class` = ? WHERE `class` = ?
AND `setting` = ?;"; AND `setting` = ?;";
$settingStatement = $mysqli->prepare($settingQuery); $settingStatement = $mysqli->prepare($settingQuery);
if($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error); trigger_error($mysqli->error);
return false; return false;
} }
$value = $this->formatSetting($value); $value = $this->formatSetting($value);
$settingStatement->bind_param('sss', $value, $className, $settingName); $settingStatement->bind_param('sss', $value, $className, $settingName);
$success = $settingStatement->execute(); $success = $settingStatement->execute();
if($settingStatement->error) { if ($settingStatement->error) {
trigger_error($settingStatement->error); trigger_error($settingStatement->error);
$settingStatement->close(); $settingStatement->close();
return false; return false;
@ -319,7 +320,7 @@ class SettingManager {
* *
* @param object $object * @param object $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) {
@ -335,20 +336,20 @@ class SettingManager {
* @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` = ?;";
$settingStatement = $mysqli->prepare($settingQuery); $settingStatement = $mysqli->prepare($settingQuery);
if($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error); trigger_error($mysqli->error);
return false; return false;
} }
$settingStatement->bind_param('ss', $className, $settingName); $settingStatement->bind_param('ss', $className, $settingName);
$success = $settingStatement->execute(); $success = $settingStatement->execute();
if($settingStatement->error) { if ($settingStatement->error) {
trigger_error($settingStatement->error); trigger_error($settingStatement->error);
$settingStatement->close(); $settingStatement->close();
return false; return false;
@ -365,19 +366,19 @@ class SettingManager {
* @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);
if($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error); trigger_error($mysqli->error);
return false; return false;
} }
$settingStatement->bind_param('ss', $className, $settingName); $settingStatement->bind_param('ss', $className, $settingName);
$success = $settingStatement->execute(); $success = $settingStatement->execute();
if($settingStatement->error) { if ($settingStatement->error) {
trigger_error($settingStatement->error); trigger_error($settingStatement->error);
$settingStatement->close(); $settingStatement->close();
return false; return false;
@ -394,17 +395,17 @@ class SettingManager {
*/ */
public function getSettingsByClass($className) { public function getSettingsByClass($className) {
$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;
//array_push($settings, $setting); // array_push($settings, $setting);
} }
$result->free(); $result->free();
return $settings; return $settings;
@ -417,17 +418,17 @@ class SettingManager {
*/ */
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) {
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;
//array_push($settings, $setting); // array_push($settings, $setting);
} }
$result->free(); $result->free();
return $settings; return $settings;