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.
This commit is contained in:
Abel Hoogeveen 2015-04-30 21:19:07 +02:00
parent 584148e16d
commit b10561df6d
7 changed files with 86 additions and 122 deletions

View File

@ -1,5 +1,7 @@
<?php <?php
namespace FuzeWorks;
class Interpret extends Model { class Interpret extends Model {
public function __construct(&$core){ public function __construct(&$core){
@ -21,17 +23,6 @@ class Interpret extends Model {
// Append to model // Append to model
$this->fields = $table_fields; $this->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;
}
} }
?> ?>

View File

@ -19,4 +19,6 @@ class Event {
} }
} }
class NotifierEvent extends Event {}
?> ?>

View File

@ -48,7 +48,7 @@ class Config extends Bus{
$DECODED = (object) require($file); $DECODED = (object) require($file);
return $DECODED; return $DECODED;
} else { } else {
$this->core->loadMod('database'); $this->core->loadMod('techfuze/database');
if ($this->dbActive) { if ($this->dbActive) {
// Fetch me a query of 5 // Fetch me a query of 5
$prefix = $this->mods->database->getPrefix(); $prefix = $this->mods->database->getPrefix();

View File

@ -82,7 +82,7 @@ class Events extends Bus{
## EVENTS ## EVENTS
public function fireEvent($input) { public function fireEvent($input) {
if (is_string($input)) { if (is_string($input)) {
// STRING // If the input is a string
$eventClass = $input; $eventClass = $input;
$eventName = $input; $eventName = $input;
if(!class_exists($eventClass)){ if(!class_exists($eventClass)){
@ -92,7 +92,7 @@ class Events extends Bus{
// Load the file // Load the file
require_once($file); require_once($file);
}else{ }else{
// No event arguments? Looks like an notify-event // No event arguments? Looks like a notify-event
if(func_num_args() == 1){ if(func_num_args() == 1){
// Load notify-event-class // Load notify-event-class
$eventClass = '\FuzeWorks\NotifierEvent'; $eventClass = '\FuzeWorks\NotifierEvent';
@ -184,7 +184,5 @@ class Events extends Bus{
} }
} }
class NotifierEvent extends Event {}
?> ?>

View File

@ -1,11 +1,16 @@
<?php <?php
namespace Module\Database;
use \FuzeWorks\Module; use \FuzeWorks\Module;
use \Exception;
use \PDO; use \PDO;
class Database extends Module { class Main extends Module {
/**
* The default database connection
* @access private
* @var PDO Class
*/
private $DBH; private $DBH;
public $prefix; public $prefix;
@ -14,105 +19,44 @@ class Database extends Module {
} }
public function onLoad() { public function onLoad() {
$this->connect($this->getSystemDbSettings());
$this->config->dbActive = true; $this->config->dbActive = true;
} }
public function connect($params = array()) { public function connect($config = null) {
if (isset($params['type'])) { // If nothing is given, connect to database from the main config, otherwise use the served configuration
$type = $params['type']; if (is_null($config)) {
$db = $this->mods->config->database;
} else { } else {
throw (new Exception("No database type given")); $db = $config;
} }
if (isset($params['datb'])) { // Get the DSN for popular types of databases or a custom DSN
$database = $params['datb']; switch (strtolower($db->type)) {
} else { case 'mysql':
throw (new Exception("No database given. Can not connect without database.")); $dsn = "mysql:host=".$db->host.";";
} $dsn .= (!empty($db->database) ? "dbname=".$db->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;
break; break;
case 'sqlite': case 'custom':
$DSN = 'sqlite:' . $host . ($database != '' ? ";dbname=" .$database : ""); $dsn = $db->dsn;
$DSN_FINAL = $DSN;
break;
default:
throw (new Exception("Unknown database type given: '" . $type . "'"));
break; break;
} }
// Try and connect try {
try{ $this->mods->logger->logInfo("Connecting to '".$dsn."'", "Database");
$this->mods->logger->logInfo("Connecting to '" . $DSN_FINAL. "'", "Database", __FILE__, __LINE__); // And create the connection
$this->DBH = new \PDO($DSN_FINAL, $username, $password, $options); $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->DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->mods->logger->logInfo("Connected to database", "Database");
$this->mods->logger->logInfo("Connected!", "Database", __FILE__, __LINE__); // And set the prefix
}catch(\PDOException $e){ $this->prefix = $db->prefix;
throw (new Exception('Could not connect to the database: "'. $e->getMessage() . '"')); } catch (Exception $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);
} }
} }
public function __get($name) { public function getPrefix() {
if ($this->is_active()) { return $this->prefix;
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 is_active() { public function is_active() {
@ -123,25 +67,31 @@ class Database extends Module {
} }
} }
public function getPrefix() { public function __call($name, $params) {
return $this->prefix; 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);
}
} }
/** public function __get($name) {
* Retrieve an array of the system DB settings. This is the configuration in the config file of FuzeWorks if ($this->is_active()) {
* @access public return $this->DBH->$name;
* @return DBSettings array } else {
*/ $this->connect();
public function getSystemDbSettings() { return $this->DBH->$name;
$dbsettings = array( }
'type' => $this->mods->config->database->type, }
'host' => $this->mods->config->database->host,
'user' => $this->mods->config->database->username, public function __set($name, $value) {
'pass' => $this->mods->config->database->password, if ($this->is_active()) {
'datb' => $this->mods->config->database->database, $this->DBH->$name = $value;
'prefix' => $this->mods->config->database->prefix, } else {
); $this->connect();
return $dbsettings; $this->DBH->$name = $value;
}
} }
} }

View File

@ -0,0 +1,23 @@
<?php
return array(
'module_class' => '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,
);

View File

@ -6,7 +6,7 @@ return array(
'module_name' => 'databasemodel', 'module_name' => 'databasemodel',
'abstract' => false, 'abstract' => false,
'dependencies' => array('database'), 'dependencies' => array('techfuze/database'),
'name' => 'DatabaseModel', 'name' => 'DatabaseModel',
'description' => 'Abstract type for easy database queries', 'description' => 'Abstract type for easy database queries',