| 1 |
lars |
1 |
<?php
|
|
|
2 |
/* SVN FILE: $Id: connection_manager.php 7945 2008-12-19 02:16:01Z gwoo $ */
|
|
|
3 |
/**
|
|
|
4 |
* Short description for file.
|
|
|
5 |
*
|
|
|
6 |
* Long description for file
|
|
|
7 |
*
|
|
|
8 |
* PHP versions 4 and 5
|
|
|
9 |
*
|
|
|
10 |
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
|
|
|
11 |
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
12 |
*
|
|
|
13 |
* Licensed under The MIT License
|
|
|
14 |
* Redistributions of files must retain the above copyright notice.
|
|
|
15 |
*
|
|
|
16 |
* @filesource
|
|
|
17 |
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
18 |
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
|
|
19 |
* @package cake
|
|
|
20 |
* @subpackage cake.cake.libs.model
|
|
|
21 |
* @since CakePHP(tm) v 0.10.x.1402
|
|
|
22 |
* @version $Revision: 7945 $
|
|
|
23 |
* @modifiedby $LastChangedBy: gwoo $
|
|
|
24 |
* @lastmodified $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
|
|
|
25 |
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
26 |
*/
|
|
|
27 |
uses ('model' . DS . 'datasources' . DS . 'datasource');
|
|
|
28 |
config('database');
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Manages loaded instances of DataSource objects
|
|
|
32 |
*
|
|
|
33 |
* Long description for file
|
|
|
34 |
*
|
|
|
35 |
* @package cake
|
|
|
36 |
* @subpackage cake.cake.libs.model
|
|
|
37 |
*/
|
|
|
38 |
class ConnectionManager extends Object {
|
|
|
39 |
/**
|
|
|
40 |
* Holds a loaded instance of the Connections object
|
|
|
41 |
*
|
|
|
42 |
* @var object
|
|
|
43 |
* @access public
|
|
|
44 |
*/
|
|
|
45 |
var $config = null;
|
|
|
46 |
/**
|
|
|
47 |
* Holds instances DataSource objects
|
|
|
48 |
*
|
|
|
49 |
* @var array
|
|
|
50 |
* @access protected
|
|
|
51 |
*/
|
|
|
52 |
var $_dataSources = array();
|
|
|
53 |
/**
|
|
|
54 |
* Contains a list of all file and class names used in Connection settings
|
|
|
55 |
*
|
|
|
56 |
* @var array
|
|
|
57 |
* @access protected
|
|
|
58 |
*/
|
|
|
59 |
var $_connectionsEnum = array();
|
|
|
60 |
/**
|
|
|
61 |
* Constructor.
|
|
|
62 |
*
|
|
|
63 |
*/
|
|
|
64 |
function __construct() {
|
|
|
65 |
if (class_exists('DATABASE_CONFIG')) {
|
|
|
66 |
$this->config =& new DATABASE_CONFIG();
|
|
|
67 |
}
|
|
|
68 |
}
|
|
|
69 |
/**
|
|
|
70 |
* Gets a reference to the ConnectionManger object instance
|
|
|
71 |
*
|
|
|
72 |
* @return object Instance
|
|
|
73 |
* @access public
|
|
|
74 |
* @static
|
|
|
75 |
*/
|
|
|
76 |
function &getInstance() {
|
|
|
77 |
static $instance = array();
|
|
|
78 |
|
|
|
79 |
if (!$instance) {
|
|
|
80 |
$instance[0] =& new ConnectionManager();
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
return $instance[0];
|
|
|
84 |
}
|
|
|
85 |
/**
|
|
|
86 |
* Gets a reference to a DataSource object
|
|
|
87 |
*
|
|
|
88 |
* @param string $name The name of the DataSource, as defined in app/config/connections
|
|
|
89 |
* @return object Instance
|
|
|
90 |
* @access public
|
|
|
91 |
* @static
|
|
|
92 |
*/
|
|
|
93 |
function &getDataSource($name) {
|
|
|
94 |
$_this =& ConnectionManager::getInstance();
|
|
|
95 |
|
|
|
96 |
if (!empty($_this->_dataSources[$name])) {
|
|
|
97 |
$return =& $_this->_dataSources[$name];
|
|
|
98 |
return $return;
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
$connections = $_this->enumConnectionObjects();
|
|
|
102 |
if (!empty($connections[$name])) {
|
|
|
103 |
$conn = $connections[$name];
|
|
|
104 |
$class = $conn['classname'];
|
|
|
105 |
$_this->loadDataSource($name);
|
|
|
106 |
$_this->_dataSources[$name] =& new $class($_this->config->{$name});
|
|
|
107 |
$_this->_dataSources[$name]->configKeyName = $name;
|
|
|
108 |
} else {
|
|
|
109 |
trigger_error(sprintf(__("ConnectionManager::getDataSource - Non-existent data source %s", true), $name), E_USER_ERROR);
|
|
|
110 |
return null;
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
$return =& $_this->_dataSources[$name];
|
|
|
114 |
return $return;
|
|
|
115 |
}
|
|
|
116 |
/**
|
|
|
117 |
* Gets the list of available DataSource connections
|
|
|
118 |
*
|
|
|
119 |
* @return array List of available connections
|
|
|
120 |
* @access public
|
|
|
121 |
* @static
|
|
|
122 |
*/
|
|
|
123 |
function sourceList() {
|
|
|
124 |
$_this =& ConnectionManager::getInstance();
|
|
|
125 |
return array_keys($_this->_dataSources);
|
|
|
126 |
}
|
|
|
127 |
/**
|
|
|
128 |
* Gets a DataSource name from an object reference
|
|
|
129 |
*
|
|
|
130 |
* @param object $source DataSource object
|
|
|
131 |
* @return string Datasource name
|
|
|
132 |
* @access public
|
|
|
133 |
* @static
|
|
|
134 |
*/
|
|
|
135 |
function getSourceName(&$source) {
|
|
|
136 |
$_this =& ConnectionManager::getInstance();
|
|
|
137 |
$names = array_keys($_this->_dataSources);
|
|
|
138 |
for ($i = 0; $i < count($names); $i++) {
|
|
|
139 |
if ($_this->_dataSources[$names[$i]] === $source) {
|
|
|
140 |
return $names[$i];
|
|
|
141 |
}
|
|
|
142 |
}
|
|
|
143 |
return null;
|
|
|
144 |
}
|
|
|
145 |
/**
|
|
|
146 |
* Loads the DataSource class for the given connection name
|
|
|
147 |
*
|
|
|
148 |
* @param mixed $connName A string name of the connection, as defined in Connections config,
|
|
|
149 |
* or an array containing the file and class name of the object.
|
|
|
150 |
* @return boolean True on success, null on failure or false if the class is already loaded
|
|
|
151 |
* @access public
|
|
|
152 |
* @static
|
|
|
153 |
*/
|
|
|
154 |
function loadDataSource($connName) {
|
|
|
155 |
$_this =& ConnectionManager::getInstance();
|
|
|
156 |
|
|
|
157 |
if (is_array($connName)) {
|
|
|
158 |
$conn = $connName;
|
|
|
159 |
} else {
|
|
|
160 |
$connections = $_this->enumConnectionObjects();
|
|
|
161 |
$conn = $connections[$connName];
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
if (!empty($conn['parent'])) {
|
|
|
165 |
$_this->loadDataSource($conn['parent']);
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
if (class_exists($conn['classname'])) {
|
|
|
169 |
return false;
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
if (file_exists(MODELS . 'datasources' . DS . $conn['filename'] . '.php')) {
|
|
|
173 |
require (MODELS . 'datasources' . DS . $conn['filename'] . '.php');
|
|
|
174 |
} elseif (fileExistsInPath(LIBS . 'model' . DS . 'datasources' . DS . $conn['filename'] . '.php')) {
|
|
|
175 |
require (LIBS . 'model' . DS . 'datasources' . DS . $conn['filename'] . '.php');
|
|
|
176 |
} else {
|
|
|
177 |
$error = __('Unable to load DataSource file %s.php', true);
|
|
|
178 |
trigger_error(sprintf($error, $conn['filename']), E_USER_ERROR);
|
|
|
179 |
return null;
|
|
|
180 |
}
|
|
|
181 |
}
|
|
|
182 |
/**
|
|
|
183 |
* Gets a list of class and file names associated with the user-defined DataSource connections
|
|
|
184 |
*
|
|
|
185 |
* @return array An associative array of elements where the key is the connection name
|
|
|
186 |
* (as defined in Connections), and the value is an array with keys 'filename' and 'classname'.
|
|
|
187 |
* @access public
|
|
|
188 |
* @static
|
|
|
189 |
*/
|
|
|
190 |
function enumConnectionObjects() {
|
|
|
191 |
$_this =& ConnectionManager::getInstance();
|
|
|
192 |
|
|
|
193 |
if (!empty($_this->_connectionsEnum)) {
|
|
|
194 |
return $_this->_connectionsEnum;
|
|
|
195 |
}
|
|
|
196 |
$connections = get_object_vars($_this->config);
|
|
|
197 |
|
|
|
198 |
if ($connections != null) {
|
|
|
199 |
foreach ($connections as $name => $config) {
|
|
|
200 |
$_this->_connectionsEnum[$name] = $_this->__getDriver($config);
|
|
|
201 |
}
|
|
|
202 |
return $_this->_connectionsEnum;
|
|
|
203 |
} else {
|
|
|
204 |
$_this->cakeError('missingConnection', array(array('className' => 'ConnectionManager')));
|
|
|
205 |
}
|
|
|
206 |
}
|
|
|
207 |
/**
|
|
|
208 |
* Dynamically creates a DataSource object at runtime, with the given name and settings
|
|
|
209 |
*
|
|
|
210 |
* @param string $name The DataSource name
|
|
|
211 |
* @param array $config The DataSource configuration settings
|
|
|
212 |
* @return object A reference to the DataSource object, or null if creation failed
|
|
|
213 |
* @access public
|
|
|
214 |
* @static
|
|
|
215 |
*/
|
|
|
216 |
function &create($name = '', $config = array()) {
|
|
|
217 |
$_this =& ConnectionManager::getInstance();
|
|
|
218 |
|
|
|
219 |
if (empty($name) || empty($config) || array_key_exists($name, $_this->_connectionsEnum)) {
|
|
|
220 |
$null = null;
|
|
|
221 |
return $null;
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
$_this->config->{$name} = $config;
|
|
|
225 |
$_this->_connectionsEnum[$name] = $_this->__getDriver($config);
|
|
|
226 |
$return =& $_this->getDataSource($name);
|
|
|
227 |
return $return;
|
|
|
228 |
}
|
|
|
229 |
/**
|
|
|
230 |
* Returns the file, class name, and parent for the given driver.
|
|
|
231 |
*
|
|
|
232 |
* @return array An indexed array with: filename, classname, and parent
|
|
|
233 |
* @access private
|
|
|
234 |
*/
|
|
|
235 |
function __getDriver($config) {
|
|
|
236 |
if (!isset($config['datasource'])) {
|
|
|
237 |
$config['datasource'] = 'dbo';
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
if (isset($config['driver']) && $config['driver'] != null && !empty($config['driver'])) {
|
|
|
241 |
$filename = $config['datasource'] . DS . $config['datasource'] . '_' . $config['driver'];
|
|
|
242 |
$classname = Inflector::camelize(strtolower($config['datasource'] . '_' . $config['driver']));
|
|
|
243 |
$parent = $this->__getDriver(array('datasource' => $config['datasource']));
|
|
|
244 |
} else {
|
|
|
245 |
$filename = $config['datasource'] . '_source';
|
|
|
246 |
$classname = Inflector::camelize(strtolower($config['datasource'] . '_source'));
|
|
|
247 |
$parent = null;
|
|
|
248 |
}
|
|
|
249 |
return array('filename' => $filename, 'classname' => $classname, 'parent' => $parent);
|
|
|
250 |
}
|
|
|
251 |
/**
|
|
|
252 |
* Destructor.
|
|
|
253 |
*
|
|
|
254 |
* @access private
|
|
|
255 |
*/
|
|
|
256 |
function __destruct() {
|
|
|
257 |
if (Configure::read('Session.save') == 'database' && function_exists('session_write_close')) {
|
|
|
258 |
session_write_close();
|
|
|
259 |
}
|
|
|
260 |
}
|
|
|
261 |
}
|
|
|
262 |
?>
|