Fix for issue #51. Tighter checks on input

This commit is contained in:
Abel Hoogeveen 2015-05-06 18:26:19 +02:00
parent dd93772704
commit 7d732790d2
2 changed files with 17 additions and 2 deletions

7
.gitignore vendored
View File

@ -10,6 +10,11 @@ FuzeWorks.esproj/*
*.DS_Store
*.swp
*.out
# Framework Files
Core/Cache/
Core/Logs/Error.log
build/
# Build Files
vendor/
build/

View File

@ -3,6 +3,7 @@
namespace Module\Database;
use \FuzeWorks\Module;
use \PDO;
use \FuzeWorks\DatabaseException;
class Main extends Module {
@ -22,6 +23,11 @@ class Main extends Module {
$this->config->dbActive = true;
}
/**
* Connect to a database
* @access public
* @param StdObject Config, like the database config in Application/Config
*/
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)) {
@ -29,6 +35,10 @@ class Main extends Module {
} else {
$db = $config;
}
if (empty($db->type) || empty($db->host)) {
throw (new DatabaseException('Database is not configured!'));
}
// Get the DSN for popular types of databases or a custom DSN
switch (strtolower($db->type)) {
@ -51,7 +61,7 @@ class Main extends Module {
// And set the prefix
$this->prefix = $db->prefix;
} catch (Exception $e) {
throw (new Exception('Could not connect to the database: "'. $e->getMessage() . '"'));
throw (new DatabaseException('Could not connect to the database: "'. $e->getMessage() . '"'));
}
}