From b10561df6d1a0d218a8661f0f932e0fac62d5f67 Mon Sep 17 00:00:00 2001 From: Abel Hoogeveen Date: Thu, 30 Apr 2015 21:19:07 +0200 Subject: [PATCH] Updated the database to a higher standard according to issue #33 and #32 Also moved the NotifierEvent into another file, the event abstract and gave the interpret model a namespace. --- Core/System/Models/model.interpret.php | 13 +- Core/System/class.abstract.event.php | 2 + Core/System/class.config.php | 2 +- Core/System/class.events.php | 6 +- Modules/database/class.database.php | 160 +++++++++---------------- Modules/database/moduleInfo.php | 23 ++++ Modules/databasemodel/moduleInfo.php | 2 +- 7 files changed, 86 insertions(+), 122 deletions(-) create mode 100644 Modules/database/moduleInfo.php diff --git a/Core/System/Models/model.interpret.php b/Core/System/Models/model.interpret.php index 3dc6cbc..dfa41da 100644 --- a/Core/System/Models/model.interpret.php +++ b/Core/System/Models/model.interpret.php @@ -1,5 +1,7 @@ fields = $table_fields; } - - public function toPHP() { - $values = array(); - foreach ($this->fields as $key => $value) { - $values[] = '"'.$value.'"'; - } - $values = implode(', ', $values); - $text = 'VALUES: array('.$values.') - TABLE: '.$this->table; - echo $text; - } } ?> \ No newline at end of file diff --git a/Core/System/class.abstract.event.php b/Core/System/class.abstract.event.php index e119043..1cd8210 100644 --- a/Core/System/class.abstract.event.php +++ b/Core/System/class.abstract.event.php @@ -19,4 +19,6 @@ class Event { } } +class NotifierEvent extends Event {} + ?> \ No newline at end of file diff --git a/Core/System/class.config.php b/Core/System/class.config.php index f16d69b..7e35fcf 100644 --- a/Core/System/class.config.php +++ b/Core/System/class.config.php @@ -48,7 +48,7 @@ class Config extends Bus{ $DECODED = (object) require($file); return $DECODED; } else { - $this->core->loadMod('database'); + $this->core->loadMod('techfuze/database'); if ($this->dbActive) { // Fetch me a query of 5 $prefix = $this->mods->database->getPrefix(); diff --git a/Core/System/class.events.php b/Core/System/class.events.php index 18e3212..12c5226 100644 --- a/Core/System/class.events.php +++ b/Core/System/class.events.php @@ -82,7 +82,7 @@ class Events extends Bus{ ## EVENTS public function fireEvent($input) { if (is_string($input)) { - // STRING + // If the input is a string $eventClass = $input; $eventName = $input; if(!class_exists($eventClass)){ @@ -92,7 +92,7 @@ class Events extends Bus{ // Load the file require_once($file); }else{ - // No event arguments? Looks like an notify-event + // No event arguments? Looks like a notify-event if(func_num_args() == 1){ // Load notify-event-class $eventClass = '\FuzeWorks\NotifierEvent'; @@ -184,7 +184,5 @@ class Events extends Bus{ } } -class NotifierEvent extends Event {} - ?> \ No newline at end of file diff --git a/Modules/database/class.database.php b/Modules/database/class.database.php index 5e077a3..bdc5c9a 100644 --- a/Modules/database/class.database.php +++ b/Modules/database/class.database.php @@ -1,11 +1,16 @@ connect($this->getSystemDbSettings()); $this->config->dbActive = true; } - public function connect($params = array()) { - if (isset($params['type'])) { - $type = $params['type']; + public function connect($config = null) { + // If nothing is given, connect to database from the main config, otherwise use the served configuration + if (is_null($config)) { + $db = $this->mods->config->database; } else { - throw (new Exception("No database type given")); + $db = $config; } - - if (isset($params['datb'])) { - $database = $params['datb']; - } else { - throw (new Exception("No database given. Can not connect without database.")); - } - - if (isset($params['host'])) { - $host = $params['host']; - } else { - throw (new Exception("No database host given. Can not connect without hostname.")); - } - - $username = $params['user']; - $password = $params['pass']; - $this->prefix = $params['prefix']; - - if (isset($params['options'])) { - $options = $params['options']; - } else { - $options = null; - } - - - $DSN_FINAL = ""; - - switch ($type) { - case 'MYSQL': - $DSN = "mysql:host="; - $DSN2 = ";dbname="; - - // Check if charset is required - if (isset($extraOptions)) { - if (isset($extraOptions->charset)) { - $DSN3 = ";charset=" . $extraOptions->charset; - } else { - $DSN3 = ";"; - } - } else { - $DSN3 = ";"; - } - $DSN_FINAL = $DSN . $host . $DSN2 . $database . $DSN3; + + // Get the DSN for popular types of databases or a custom DSN + switch (strtolower($db->type)) { + case 'mysql': + $dsn = "mysql:host=".$db->host.";"; + $dsn .= (!empty($db->database) ? "dbname=".$db->database.";" : ""); break; - case 'sqlite': - $DSN = 'sqlite:' . $host . ($database != '' ? ";dbname=" .$database : ""); - $DSN_FINAL = $DSN; - break; - default: - throw (new Exception("Unknown database type given: '" . $type . "'")); + case 'custom': + $dsn = $db->dsn; break; } - // Try and connect - try{ - $this->mods->logger->logInfo("Connecting to '" . $DSN_FINAL. "'", "Database", __FILE__, __LINE__); - $this->DBH = new \PDO($DSN_FINAL, $username, $password, $options); - $this->DBH->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); + try { + $this->mods->logger->logInfo("Connecting to '".$dsn."'", "Database"); + // And create the connection + $this->DBH = new PDO($dsn, $db->username, $db->password, (isset($db->options) ? $db->options : null)); + $this->DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $this->mods->logger->logInfo("Connected to database", "Database"); - $this->mods->logger->logInfo("Connected!", "Database", __FILE__, __LINE__); - }catch(\PDOException $e){ - throw (new Exception('Could not connect to the database: "'. $e->getMessage() . '"')); - } - } - - public function __call($name, $params) { - if ($this->is_active()) { - return call_user_func_array(array($this->DBH, $name), $params); - } else { - $this->connect($this->getSystemDbSettings()); - return call_user_func_array(array($this->DBH, $name), $params); + // And set the prefix + $this->prefix = $db->prefix; + } catch (Exception $e) { + throw (new Exception('Could not connect to the database: "'. $e->getMessage() . '"')); } } - public function __get($name) { - if ($this->is_active()) { - return $this->DBH->$name; - } else { - $this->connect($this->getSystemDbSettings()); - return $this->DBH->$name; - } - } - - public function __set($name, $value) { - if ($this->is_active()) { - $this->DBH->$name = $value; - } else { - $this->connect($this->getSystemDbSettings()); - $this->DBH->$name = $value; - } + public function getPrefix() { + return $this->prefix; } public function is_active() { @@ -123,25 +67,31 @@ class Database extends Module { } } - public function getPrefix() { - return $this->prefix; + public function __call($name, $params) { + if ($this->is_active()) { + return call_user_func_array(array($this->DBH, $name), $params); + } else { + $this->connect(); + return call_user_func_array(array($this->DBH, $name), $params); + } } - /** - * Retrieve an array of the system DB settings. This is the configuration in the config file of FuzeWorks - * @access public - * @return DBSettings array - */ - public function getSystemDbSettings() { - $dbsettings = array( - 'type' => $this->mods->config->database->type, - 'host' => $this->mods->config->database->host, - 'user' => $this->mods->config->database->username, - 'pass' => $this->mods->config->database->password, - 'datb' => $this->mods->config->database->database, - 'prefix' => $this->mods->config->database->prefix, - ); - return $dbsettings; + public function __get($name) { + if ($this->is_active()) { + return $this->DBH->$name; + } else { + $this->connect(); + return $this->DBH->$name; + } + } + + public function __set($name, $value) { + if ($this->is_active()) { + $this->DBH->$name = $value; + } else { + $this->connect(); + $this->DBH->$name = $value; + } } } diff --git a/Modules/database/moduleInfo.php b/Modules/database/moduleInfo.php new file mode 100644 index 0000000..705ba7c --- /dev/null +++ b/Modules/database/moduleInfo.php @@ -0,0 +1,23 @@ + 'Module\Database\Main', + 'module_file' => 'class.database.php', + 'module_name' => 'Database', + + 'abstract' => false, + 'dependencies' => array(), + 'events' => array(), + 'sections' => array(), + + 'name' => 'FuzeWorks Database Module', + 'description' => 'PDO Wrapper class for FuzeWorks', + 'author' => 'TechFuze', + 'version' => '1.0.0', + 'website' => 'http://fuzeworks.techfuze.net/', + + 'date_created' => '30-04-2015', + 'date_updated' => '30-04-2015', + + 'enabled' => true, +); diff --git a/Modules/databasemodel/moduleInfo.php b/Modules/databasemodel/moduleInfo.php index f4c85d6..81b6c58 100644 --- a/Modules/databasemodel/moduleInfo.php +++ b/Modules/databasemodel/moduleInfo.php @@ -6,7 +6,7 @@ return array( 'module_name' => 'databasemodel', 'abstract' => false, - 'dependencies' => array('database'), + 'dependencies' => array('techfuze/database'), 'name' => 'DatabaseModel', 'description' => 'Abstract type for easy database queries',