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
namespace ManiaControl;
namespace ManiaControl\Settings;
use ManiaControl\ManiaControl;
/**
* Class managing settings and configurations
@ -59,12 +61,12 @@ class SettingManager {
UNIQUE KEY `settingId` (`class`,`setting`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Settings and Configurations' AUTO_INCREMENT=1;";
$settingTableStatement = $mysqli->prepare($settingTableQuery);
if($mysqli->error) {
if ($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR);
return false;
}
$settingTableStatement->execute();
if($settingTableStatement->error) {
if ($settingTableStatement->error) {
trigger_error($settingTableStatement->error, E_USER_ERROR);
return false;
}
@ -79,14 +81,14 @@ class SettingManager {
* @return string
*/
private function getClassName($object) {
if(is_object($object)) {
if (is_object($object)) {
return get_class($object);
}
if(is_string($object)) {
if (is_string($object)) {
return $object;
}
trigger_error('Invalid class param. ' . $object);
return (string)$object;
return (string) $object;
}
/**
@ -96,19 +98,19 @@ class SettingManager {
* @return string
*/
private function getType($param) {
if(is_int($param)) {
if (is_int($param)) {
return self::TYPE_INT;
}
if(is_real($param)) {
if (is_real($param)) {
return self::TYPE_REAL;
}
if(is_bool($param)) {
if (is_bool($param)) {
return self::TYPE_BOOL;
}
if(is_string($param)) {
if (is_string($param)) {
return self::TYPE_STRING;
}
if(is_array($param)) {
if (is_array($param)) {
return self::TYPE_ARRAY;
}
trigger_error('Unsupported setting type. ' . print_r($param, true));
@ -123,19 +125,19 @@ class SettingManager {
* @return mixed
*/
private function castSetting($type, $value) {
if($type === self::TYPE_INT) {
return (int)$value;
if ($type === self::TYPE_INT) {
return (int) $value;
}
if($type === self::TYPE_REAL) {
return (float)$value;
if ($type === self::TYPE_REAL) {
return (float) $value;
}
if($type === self::TYPE_BOOL) {
return (bool)$value;
if ($type === self::TYPE_BOOL) {
return (bool) $value;
}
if($type === self::TYPE_STRING) {
return (string)$value;
if ($type === self::TYPE_STRING) {
return (string) $value;
}
if($type === self::TYPE_ARRAY) {
if ($type === self::TYPE_ARRAY) {
return explode($this->arrayDelimiter, $value);
}
trigger_error('Unsupported setting type. ' . print_r($type, true));
@ -150,13 +152,13 @@ class SettingManager {
* @return mixed
*/
private function formatSetting($value, $type = null) {
if($type === null) {
if ($type === null) {
$type = $this->getType($value);
}
if($type === self::TYPE_ARRAY) {
if ($type === self::TYPE_ARRAY) {
return implode($this->arrayDelimiter, $value);
}
if($type === self::TYPE_BOOL) {
if ($type === self::TYPE_BOOL) {
return ($value ? 1 : 0);
}
return $value;
@ -171,7 +173,7 @@ class SettingManager {
* @return bool
*/
public function initSetting($object, $settingName, $default) {
if($default === null || is_object($default)) {
if ($default === null || is_object($default)) {
return false;
}
$className = $this->getClassName($object);
@ -193,13 +195,13 @@ class SettingManager {
`value` = IF(`default` = VALUES(`default`), `value`, VALUES(`default`)),
`default` = VALUES(`default`);";
$settingStatement = $mysqli->prepare($settingQuery);
if($mysqli->error) {
if ($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
$settingStatement->bind_param('ssss', $className, $settingName, $type, $default);
$success = $settingStatement->execute();
if($settingStatement->error) {
if ($settingStatement->error) {
trigger_error($settingStatement->error);
$settingStatement->close();
return false;
@ -216,14 +218,14 @@ class SettingManager {
* @internal param null $default
* @internal param $className
* @internal param \ManiaControl\gIndex $settin
* @return mixed|null
* @return mixed null
*/
public function getSettingByIndex($settingIndex, $default = false) {
$mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "SELECT * FROM `" . self::TABLE_SETTINGS . "`
WHERE `index` = {$settingIndex};";
$result = $mysqli->query($settingQuery);
if(!$result) {
if (!$result) {
trigger_error($mysqli->error);
return false;
}
@ -233,7 +235,6 @@ class SettingManager {
return $row;
}
/**
* Gets a Setting Via it's class name
*
@ -247,18 +248,18 @@ class SettingManager {
WHERE `class` = ?
AND `setting` = ?;";
$settingStatement = $mysqli->prepare($settingQuery);
if($mysqli->error) {
if ($mysqli->error) {
trigger_error($mysqli->error);
return null;
}
$settingStatement->bind_param('ss', $className, $settingName);
$settingStatement->execute();
if($settingStatement->error) {
if ($settingStatement->error) {
trigger_error($settingStatement->error);
return null;
}
$settingStatement->store_result();
if($settingStatement->num_rows <= 0) {
if ($settingStatement->num_rows <= 0) {
$this->updateSetting($className, $settingName, $default);
return $default;
}
@ -298,14 +299,14 @@ class SettingManager {
WHERE `class` = ?
AND `setting` = ?;";
$settingStatement = $mysqli->prepare($settingQuery);
if($mysqli->error) {
if ($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
$value = $this->formatSetting($value);
$settingStatement->bind_param('sss', $value, $className, $settingName);
$success = $settingStatement->execute();
if($settingStatement->error) {
if ($settingStatement->error) {
trigger_error($settingStatement->error);
$settingStatement->close();
return false;
@ -342,13 +343,13 @@ class SettingManager {
WHERE `class` = ?
AND `setting` = ?;";
$settingStatement = $mysqli->prepare($settingQuery);
if($mysqli->error) {
if ($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
$settingStatement->bind_param('ss', $className, $settingName);
$success = $settingStatement->execute();
if($settingStatement->error) {
if ($settingStatement->error) {
trigger_error($settingStatement->error);
$settingStatement->close();
return false;
@ -371,13 +372,13 @@ class SettingManager {
WHERE `class` = ?
AND `setting` = ?;";
$settingStatement = $mysqli->prepare($settingQuery);
if($mysqli->error) {
if ($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
$settingStatement->bind_param('ss', $className, $settingName);
$success = $settingStatement->execute();
if($settingStatement->error) {
if ($settingStatement->error) {
trigger_error($settingStatement->error);
$settingStatement->close();
return false;
@ -397,14 +398,14 @@ class SettingManager {
$query = "SELECT * FROM `" . self::TABLE_SETTINGS . "` WHERE `class`= '" . $mysqli->escape_string($className) . "'
ORDER BY `setting` ASC;";
$result = $mysqli->query($query);
if($mysqli->error) {
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;
//array_push($settings, $setting);
// array_push($settings, $setting);
}
$result->free();
return $settings;
@ -420,14 +421,14 @@ class SettingManager {
$query = "SELECT * FROM `" . self::TABLE_SETTINGS . "`
ORDER BY `class` ASC, `setting` ASC;";
$result = $mysqli->query($query);
if($mysqli->error) {
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;
//array_push($settings, $setting);
// array_push($settings, $setting);
}
$result->free();
return $settings;