/
Db.php
60 lines (51 loc) · 1.3 KB
/
Db.php
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
<?php
namespace Li;
/**
* db factor
*/
class Db
{
public static $_instance;
private static $_db = array();
public static function db()
{
if(!(self::$_instance instanceof self))
{
self::$_instance = new self();
}
return self::$_instance;
}
function __get($dbName)
{
if(isset(App::app()->config['database'][$dbName]))
{
$driver = 'Li\\'.ucfirst(App::app()->config['database'][$dbName]['driver']);
if(!(isset($this->$dbName) && $this->$dbName instanceof $driver))
{
$this->$dbName = new $driver(App::app()->config['database'][$dbName]);
}
return $this->$dbName;
}
else
{
throw new Exception("config no db ".$dbName);
}
}
/**
* init db instance by config
* @param $key string the db index
* @param $config mixed the config
*/
public function initByConfig($key, $config)
{
if(empty($config))
{
throw new Exception('db config is empty');
}
$driver = 'Li\\'.ucfirst($config['driver']);
if(!(isset($this->$key) && $this->$key instanceof $driver))
{
$this->$key = new $driver($config);
}
}
}