forked from JorgenEvens/WebLab_Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.php
More file actions
86 lines (74 loc) · 2.14 KB
/
Database.php
File metadata and controls
86 lines (74 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
/**
* This class manages database connections and generation of their adapters.
* Lazy loading of the adapters is used.
*
* @author jorgen
* @package WebLab
*
*/
class WebLab_Database {
/**
* Stores the already loaded database adapters.
*
* @var WebLab_Data_Adapter[]
*/
protected static $_databases = array();
/**
* Stores the database configuration read from WebLab_Config
*
* @see WebLab_Config
* @var mixed[]
*/
protected static $_config;
/**
* Lazy loads the configuration.
*
* @return mixed[]
*/
protected static function _getConfig() {
if( empty( self::$_config ) )
self::$_config = WebLab_Config::getApplicationConfig()->get( 'Application.Data.DB', WebLab_Config::RAW, false );
return self::$_config;
}
/**
* Retrieves the database with $name from the database list, or generates it's adapter and adds it to the list.
*
* @param string $name
* @throws WebLab_Exception_Data If database with that name was defined.
* @return WebLab_Data_Adapter
*/
public static function getDb( $name ) {
if( !isset( self::$_databases[$name] ) ) {
$config = self::_getConfig();
if( empty( $config ) || !isset( $config[$name] ) ) {
throw new WebLab_Exception_Data( 'No such database found.' );
}
$db_config = (object)$config[$name];
$adapterClass = 'WebLab_Data_' . $db_config->type . '_Adapter';
self::$_databases[$name] = new $adapterClass( $db_config );
}
return self::$_databases[$name];
}
/**
* Returns all of the database adapters defined in the configuration file.
* All adapters that had not been loaded yet will now be loaded before returning the list.
*
* @return WebLab_Data_Adapter[]
*/
public static function getAll() {
$config = self::$_getConfig();
if( empty( $config ) ) {
return array();
}
if( count( $config ) != count( self::$_databases ) ) {
$databases = array_keys( $config );
foreach( $databases as $name ) {
if( !isset( self::$_databases[$name] ) ) {
self::getDb( $name );
}
}
}
return self::$_databases;
}
}