3
* Datasource connection manager
5
* Provides an interface for loading and enumerating connections defined in app/config/database.php
9
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
10
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
12
* Licensed under The MIT License
13
* Redistributions of files must retain the above copyright notice.
15
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
16
* @link http://cakephp.org CakePHP(tm) Project
18
* @subpackage cake.cake.libs.model
19
* @since CakePHP(tm) v 0.10.x.1402
20
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
22
require LIBS . 'model' . DS . 'datasources' . DS . 'datasource.php';
23
include_once CONFIGS . 'database.php';
26
* Manages loaded instances of DataSource objects
29
* @subpackage cake.cake.libs.model
31
class ConnectionManager extends Object {
34
* Holds a loaded instance of the Connections object
36
* @var DATABASE_CONFIG
42
* Holds instances DataSource objects
47
var $_dataSources = array();
50
* Contains a list of all file and class names used in Connection settings
55
var $_connectionsEnum = array();
61
function __construct() {
62
if (class_exists('DATABASE_CONFIG')) {
63
$this->config =& new DATABASE_CONFIG();
64
$this->_getConnectionObjects();
69
* Gets a reference to the ConnectionManger object instance
71
* @return object Instance
75
function &getInstance() {
76
static $instance = array();
79
$instance[0] =& new ConnectionManager();
86
* Gets a reference to a DataSource object
88
* @param string $name The name of the DataSource, as defined in app/config/database.php
89
* @return object Instance
93
function &getDataSource($name) {
94
$_this =& ConnectionManager::getInstance();
96
if (!empty($_this->_dataSources[$name])) {
97
$return =& $_this->_dataSources[$name];
101
if (empty($_this->_connectionsEnum[$name])) {
102
trigger_error(sprintf(__("ConnectionManager::getDataSource - Non-existent data source %s", true), $name), E_USER_ERROR);
106
$conn = $_this->_connectionsEnum[$name];
107
$class = $conn['classname'];
109
if ($_this->loadDataSource($name) === null) {
110
trigger_error(sprintf(__("ConnectionManager::getDataSource - Could not load class %s", true), $class), E_USER_ERROR);
114
$_this->_dataSources[$name] =& new $class($_this->config->{$name});
115
$_this->_dataSources[$name]->configKeyName = $name;
117
$return =& $_this->_dataSources[$name];
122
* Gets the list of available DataSource connections
124
* @return array List of available connections
128
function sourceList() {
129
$_this =& ConnectionManager::getInstance();
130
return array_keys($_this->_dataSources);
134
* Gets a DataSource name from an object reference.
136
* **Warning** this method may cause fatal errors in PHP4.
138
* @param object $source DataSource object
139
* @return string Datasource name, or null if source is not present
140
* in the ConnectionManager.
144
function getSourceName(&$source) {
145
$_this =& ConnectionManager::getInstance();
146
foreach ($_this->_dataSources as $name => $ds) {
147
if ($ds == $source) {
155
* Loads the DataSource class for the given connection name
157
* @param mixed $connName A string name of the connection, as defined in app/config/database.php,
158
* or an array containing the filename (without extension) and class name of the object,
159
* to be found in app/models/datasources/ or cake/libs/model/datasources/.
160
* @return boolean True on success, null on failure or false if the class is already loaded
164
function loadDataSource($connName) {
165
$_this =& ConnectionManager::getInstance();
167
if (is_array($connName)) {
170
$conn = $_this->_connectionsEnum[$connName];
173
if (class_exists($conn['classname'])) {
177
if (!empty($conn['parent'])) {
178
$_this->loadDataSource($conn['parent']);
181
$conn = array_merge(array('plugin' => null, 'classname' => null, 'parent' => null), $conn);
182
$class = "{$conn['plugin']}.{$conn['classname']}";
184
if (!App::import('Datasource', $class, !is_null($conn['plugin']))) {
185
trigger_error(sprintf(__('ConnectionManager::loadDataSource - Unable to import DataSource class %s', true), $class), E_USER_ERROR);
192
* Return a list of connections
194
* @return array An associative array of elements where the key is the connection name
195
* (as defined in Connections), and the value is an array with keys 'filename' and 'classname'.
199
function enumConnectionObjects() {
200
$_this =& ConnectionManager::getInstance();
202
return $_this->_connectionsEnum;
206
* Dynamically creates a DataSource object at runtime, with the given name and settings
208
* @param string $name The DataSource name
209
* @param array $config The DataSource configuration settings
210
* @return object A reference to the DataSource object, or null if creation failed
214
function &create($name = '', $config = array()) {
215
$_this =& ConnectionManager::getInstance();
217
if (empty($name) || empty($config) || array_key_exists($name, $_this->_connectionsEnum)) {
221
$_this->config->{$name} = $config;
222
$_this->_connectionsEnum[$name] = $_this->__connectionData($config);
223
$return =& $_this->getDataSource($name);
228
* Gets a list of class and file names associated with the user-defined DataSource connections
234
function _getConnectionObjects() {
235
$connections = get_object_vars($this->config);
237
if ($connections != null) {
238
foreach ($connections as $name => $config) {
239
$this->_connectionsEnum[$name] = $this->__connectionData($config);
242
$this->cakeError('missingConnection', array(array('code' => 500, 'className' => 'ConnectionManager')));
247
* Returns the file, class name, and parent for the given driver.
249
* @return array An indexed array with: filename, classname, plugin and parent
252
function __connectionData($config) {
253
if (!isset($config['datasource'])) {
254
$config['datasource'] = 'dbo';
256
$filename = $classname = $parent = $plugin = null;
258
if (!empty($config['driver'])) {
259
$parent = $this->__connectionData(array('datasource' => $config['datasource']));
260
$parentSource = preg_replace('/_source$/', '', $parent['filename']);
262
list($plugin, $classname) = pluginSplit($config['driver']);
264
$source = Inflector::underscore($classname);
266
$source = $parentSource . '_' . $config['driver'];
267
$classname = Inflector::camelize(strtolower($source));
269
$filename = $parentSource . DS . $source;
271
list($plugin, $classname) = pluginSplit($config['datasource']);
273
$filename = Inflector::underscore($classname);
275
$filename = Inflector::underscore($config['datasource']);
277
if (substr($filename, -7) != '_source') {
278
$filename .= '_source';
280
$classname = Inflector::camelize(strtolower($filename));
282
return compact('filename', 'classname', 'parent', 'plugin');
290
function __destruct() {
291
if (Configure::read('Session.save') == 'database' && function_exists('session_write_close')) {
292
session_write_close();