|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Database class |
|
5
|
|
|
*/ |
|
6
|
|
|
class Database { |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* array with database instances |
|
10
|
|
|
*/ |
|
11
|
|
|
protected static $instances = array(); |
|
12
|
|
|
|
|
13
|
|
|
protected static $db_settings = null; |
|
14
|
|
|
|
|
15
|
|
|
public static function &getInstance ($name = "") : DBDriver { |
|
16
|
|
|
if ($name == "") { |
|
17
|
|
|
$name = "default"; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
if (!isset(self::$instances[$name])) { |
|
21
|
|
|
//create new database connection |
|
22
|
|
|
self::createInstance($name); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
return self::$instances[$name]; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public static function createInstance ($name = "default") { |
|
29
|
|
|
if ($name == "default") { |
|
30
|
|
|
//get database configuration |
|
31
|
|
|
require(CONFIG_PATH . "database.php"); |
|
32
|
|
|
|
|
33
|
|
|
//save configuration |
|
34
|
|
|
self::$db_settings = $database; |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
//get database driver and config |
|
37
|
|
|
$db_class_name = self::$db_settings['driver']; |
|
38
|
|
|
$db_config_path = self::$db_settings['config']; |
|
39
|
|
|
|
|
40
|
|
|
//create new database class instance from string |
|
41
|
|
|
$instance1 = new $db_class_name(); |
|
42
|
|
|
|
|
43
|
|
|
//check, if instance is an database driver |
|
44
|
|
|
if (!$instance1 instanceof DBDriver) { |
|
45
|
|
|
throw new BadMethodCallException("Cannot initialize database driver " . $db_class_name . ", driver doesnt implements interface DBDriver."); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
//save instance |
|
49
|
|
|
self::$instances[$name] = $instance1; |
|
50
|
|
|
|
|
51
|
|
|
//connect to database |
|
52
|
|
|
self::$instances[$name]->connect($db_config_path); |
|
53
|
|
|
} else { |
|
54
|
|
|
//check, if configuration file exists |
|
55
|
|
|
if (file_exists(CONFIG_PATH . "db_" . $name . ".php")) { |
|
56
|
|
|
require(CONFIG_PATH . "db_" . $name . ".php"); |
|
57
|
|
|
|
|
58
|
|
|
//get database driver and config |
|
59
|
|
|
$db_class_name = $database['driver']; |
|
60
|
|
|
$db_config_path = $database['config']; |
|
61
|
|
|
|
|
62
|
|
|
//create new database class instance from string |
|
63
|
|
|
$db_instance = new $db_class_name(); |
|
64
|
|
|
|
|
65
|
|
|
//check, if instance is an database driver |
|
66
|
|
|
if (!$db_instance instanceof DBDriver) { |
|
67
|
|
|
throw new BadMethodCallException("Cannot initialize database driver " . $db_class_name . ", driver doesnt implements interface DBDriver."); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
//save instance |
|
71
|
|
|
self::$instances[$name] = $db_instance; |
|
72
|
|
|
|
|
73
|
|
|
//connect to database |
|
74
|
|
|
self::$instances[$name]->connect($db_config_path); |
|
75
|
|
|
} else { |
|
76
|
|
|
//Logger::error("Couldnt find database configuration file for database '" . $name . "'."); |
|
77
|
|
|
echo "Couldnt find database configuration file for database '" . $name . "'."; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|