|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Magister\Services\Database; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class DatabaseManager. |
|
7
|
|
|
*/ |
|
8
|
|
|
class DatabaseManager implements ConnectionResolverInterface |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* The application instance. |
|
12
|
|
|
* |
|
13
|
|
|
* @var \Magister\Magister |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $app; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The active connection instances. |
|
19
|
|
|
* |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $connections = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Create a new database manager instance. |
|
26
|
|
|
* |
|
27
|
|
|
* @param \Magister\Magister $app |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct($app) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->app = $app; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Get a database connection instance. |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $name |
|
38
|
|
|
* |
|
39
|
|
|
* @return \Magister\Services\Database\Connection |
|
40
|
|
|
*/ |
|
41
|
|
|
public function connection($name = null) |
|
42
|
|
|
{ |
|
43
|
|
|
$name = $name ?: $this->getDefaultConnection(); |
|
44
|
|
|
|
|
45
|
|
|
if (!isset($this->connections[$name])) { |
|
46
|
|
|
$connection = $this->makeConnection(); |
|
47
|
|
|
|
|
48
|
|
|
$this->connections[$name] = $connection; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $this->connections[$name]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Make the database connection instance. |
|
56
|
|
|
* |
|
57
|
|
|
* @return \Magister\Services\Database\Connection |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function makeConnection() |
|
60
|
|
|
{ |
|
61
|
|
|
return new Connection($this->app['http']); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Add a connection to the resolver. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $name |
|
68
|
|
|
* @param \Magister\Services\Database\ConnectionInterface $connection |
|
69
|
|
|
* |
|
70
|
|
|
* @return void |
|
71
|
|
|
*/ |
|
72
|
|
|
public function addConnection($name, ConnectionInterface $connection) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->connections[$name] = $connection; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Set the default connection name. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $name |
|
81
|
|
|
* |
|
82
|
|
|
* @return void |
|
83
|
|
|
*/ |
|
84
|
|
|
public function setDefaultConnection($name) |
|
85
|
|
|
{ |
|
86
|
|
|
$this->app['config']['database.default'] = $name; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get the default connection name. |
|
91
|
|
|
* |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getDefaultConnection() |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->app['config']['database.default']; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Return all of the created connections. |
|
101
|
|
|
* |
|
102
|
|
|
* @return array |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getConnections() |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->connections; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Dynamically pass methods to the default connection. |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $method |
|
113
|
|
|
* @param array $parameters |
|
114
|
|
|
* |
|
115
|
|
|
* @return mixed |
|
116
|
|
|
*/ |
|
117
|
|
|
public function __call($method, $parameters) |
|
118
|
|
|
{ |
|
119
|
|
|
return call_user_func_array([$this->connection(), $method], $parameters); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|