1
|
|
|
<?php |
2
|
|
|
namespace Agavi\Database; |
3
|
|
|
|
4
|
|
|
// +---------------------------------------------------------------------------+ |
5
|
|
|
// | This file is part of the Agavi package. | |
6
|
|
|
// | Copyright (c) 2005-2011 the Agavi Project. | |
7
|
|
|
// | Based on the Mojavi3 MVC Framework, Copyright (c) 2003-2005 Sean Kerr. | |
8
|
|
|
// | | |
9
|
|
|
// | For the full copyright and license information, please view the LICENSE | |
10
|
|
|
// | file that was distributed with this source code. You can also view the | |
11
|
|
|
// | LICENSE file online at http://www.agavi.org/LICENSE.txt | |
12
|
|
|
// | vi: set noexpandtab: | |
13
|
|
|
// | Local Variables: | |
14
|
|
|
// | indent-tabs-mode: t | |
15
|
|
|
// | End: | |
16
|
|
|
// +---------------------------------------------------------------------------+ |
17
|
|
|
use Agavi\Config\Config; |
18
|
|
|
use Agavi\Config\ConfigCache; |
19
|
|
|
use Agavi\Core\Context; |
20
|
|
|
use Agavi\Exception\DatabaseException; |
21
|
|
|
use Agavi\Exception\InitializationException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* AgaviDatabaseManager allows you to setup your database connectivity before |
25
|
|
|
* the request is handled. This eliminates the need for a filter to manage |
26
|
|
|
* database connections. |
27
|
|
|
* |
28
|
|
|
* @package agavi |
29
|
|
|
* @subpackage database |
30
|
|
|
* |
31
|
|
|
* @author David Zülke <[email protected]> |
32
|
|
|
* @author Sean Kerr <[email protected]> |
33
|
|
|
* @copyright Authors |
34
|
|
|
* @copyright The Agavi Project |
35
|
|
|
* |
36
|
|
|
* @since 0.9.0 |
37
|
|
|
* |
38
|
|
|
* @version $Id$ |
39
|
|
|
*/ |
40
|
|
|
class DatabaseManager |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* @var string The name of the default database. |
44
|
|
|
*/ |
45
|
|
|
protected $defaultDatabaseName = null; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array An array of AgaviDatabases. |
49
|
|
|
*/ |
50
|
|
|
protected $databases = array(); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var Context A Context instance. |
54
|
|
|
*/ |
55
|
|
|
protected $context = null; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Retrieve the current application context. |
59
|
|
|
* |
60
|
|
|
* @return Context The current Context instance. |
61
|
|
|
* |
62
|
|
|
* @author David Zülke <[email protected]> |
63
|
|
|
* @since 0.11.0 |
64
|
|
|
*/ |
65
|
|
|
final public function getContext() |
66
|
|
|
{ |
67
|
|
|
return $this->context; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Retrieve the database connection associated with this Database |
72
|
|
|
* implementation. |
73
|
|
|
* |
74
|
|
|
* @param string $nme A database name. |
|
|
|
|
75
|
|
|
* |
76
|
|
|
* @return mixed A Database instance. |
77
|
|
|
* |
78
|
|
|
* @throws DatabaseException If the requested database name does not exist. |
79
|
|
|
* |
80
|
|
|
* @author David Zülke <[email protected]> |
81
|
|
|
* @author Sean Kerr <[email protected]> |
82
|
|
|
* @since 0.9.0 |
83
|
|
|
*/ |
84
|
|
|
public function getDatabase($name = null) |
85
|
|
|
{ |
86
|
|
|
if ($name === null) { |
87
|
|
|
$name = $this->defaultDatabaseName; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (isset($this->databases[$name])) { |
91
|
|
|
return $this->databases[$name]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// nonexistent database name |
95
|
|
|
$error = 'Database "%s" does not exist'; |
96
|
|
|
$error = sprintf($error, $name); |
97
|
|
|
throw new DatabaseException($error); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Retrieve the name of the given database instance. |
102
|
|
|
* |
103
|
|
|
* @param Database $database The database to fetch the name of. |
104
|
|
|
* |
105
|
|
|
* @return string The name of the database, or false if it was not found. |
106
|
|
|
* |
107
|
|
|
* @author David Zülke <[email protected]> |
108
|
|
|
* @since 0.11.0 |
109
|
|
|
*/ |
110
|
|
|
public function getDatabaseName(Database $database) |
111
|
|
|
{ |
112
|
|
|
return array_search($database, $this->databases, true); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Returns the name of the default database. |
117
|
|
|
* |
118
|
|
|
* @return string The name of the default database. |
119
|
|
|
* |
120
|
|
|
* @author David Zülke <[email protected]> |
121
|
|
|
* @since 0.11.0 |
122
|
|
|
*/ |
123
|
|
|
public function getDefaultDatabaseName() |
124
|
|
|
{ |
125
|
|
|
return $this->defaultDatabaseName; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Initialize this DatabaseManager. |
130
|
|
|
* |
131
|
|
|
* @param Context $context A Context instance. |
132
|
|
|
* @param array $parameters An array of initialization parameters. |
133
|
|
|
* |
134
|
|
|
* @throws InitializationException If an error occurs while initializing this DatabaseManager. |
135
|
|
|
* |
136
|
|
|
* @author David Zülke <[email protected]> |
137
|
|
|
* @author Sean Kerr <[email protected]> |
138
|
|
|
* @since 0.9.0 |
139
|
|
|
*/ |
140
|
|
|
public function initialize(Context $context, array $parameters = array()) |
141
|
|
|
{ |
142
|
|
|
$this->context = $context; |
143
|
|
|
|
144
|
|
|
// load database configuration |
145
|
|
|
require(ConfigCache::checkConfig(Config::get('core.config_dir') . '/databases.xml')); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Do any necessary startup work after initialization. |
150
|
|
|
* |
151
|
|
|
* This method is not called directly after initialize(). |
152
|
|
|
* |
153
|
|
|
* @author David Zülke <[email protected]> |
154
|
|
|
* @since 0.11.0 |
155
|
|
|
*/ |
156
|
|
|
public function startup() |
157
|
|
|
{ |
158
|
|
|
/** @var Database $database */ |
159
|
|
|
foreach ($this->databases as $database) { |
160
|
|
|
$database->startup(); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Execute the shutdown procedure. |
166
|
|
|
* |
167
|
|
|
* @throws <b>AgaviDatabaseException</b> If an error occurs while shutting |
168
|
|
|
* down this DatabaseManager. |
169
|
|
|
* |
170
|
|
|
* @author Sean Kerr <[email protected]> |
171
|
|
|
* @since 0.9.0 |
172
|
|
|
*/ |
173
|
|
|
public function shutdown() |
174
|
|
|
{ |
175
|
|
|
// loop through databases and shutdown connections |
176
|
|
|
foreach ($this->databases as $database) { |
177
|
|
|
$database->shutdown(); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.