-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.php
97 lines (95 loc) · 2.76 KB
/
Database.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
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
87
88
89
90
91
92
93
94
95
96
97
<?php
namespace BlueSeed;
/**
*
* Administrate the DatabaseConnectionCollection
* @author ivonascimento <[email protected]>
* @license http://www.opensource.org/licenses/bsd-license.php BSD
*
*/
class Database {
/**
*
* the static instance of Database for the Singleton
* @var Database
* @access private
*/
private static $instance;
/**
*
* The DatabaseCollection where any DatabaseConnection are stored
* @var DatabaseCollection
* @access private
*/
private $databaseCollection;
/**
*
* the Constructor for Database Instance
* @access private
* @return void
*/
private function __construct() {
$this->databaseCollection = new DatabaseCollection();
}
/**
*
* The Singloton request method
* @return Database
* @access public
*/
public static function getInstance(){
if (is_null(self::$instance ) )
self::$instance = new Database();
return self::$instance;
}
/**
*
* The method to add DatabaseConnection items in DatabaseCollection
* @param String $name the label for request a especific connection
* @param DatabaseConnection $dbconn
* @access public
* @return void
*/
public function addDatabase($name, DatabaseConnection $dbconn){
$this->databaseCollection[ $name ] = $dbconn;
}
/**
*
* use this method to retrieve a DatabaseConnection by your label/name
* @param String $name
* @throws Exception
* @access public
* @return DatabaseConnection
*/
public function get($name="default"){
if ($name)
return $this->databaseCollection[ $name ];
else if ( count($this->databaseCollection)==1){
foreach ($this->databaseCollection as $item)
return $item;
}
else
throw New \Exception("Necessario indicar um nome de Item");
}
/**
*
* This method are used to load DatabaseConnections
* configurations from a ini file in Blue Seed sintax
* @param String $confpath
* @throws Exception
* @access public
* @return void
*/
public function loadfromConf($confpath){
$dbitens = parse_ini_file($confpath, true);
foreach ($dbitens as $idx => $each){
try{
$dsn = "{$each['driver']}:host={$each['host']};dbname={$each['database']};port={$each['port']}";
$this->addDatabase($idx, new DatabaseConnection( new \Pdo( trim($dsn), trim($each['user']), trim($each['password']))) );
}catch(\Exception $Exception){
throw new \Exception("Problemas na conexao com o DB {$idx} ({$dsn}) [ {$Exception->getMessage()} ]" );
}
}
}
}
?>