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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
<?php
/* SVN FILE: $Id: connection_manager.php 6311 2008-01-02 06:33:52Z phpnut $ */
/**
* Short description for file.
*
* Long description for file
*
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
* Copyright 2005-2008, Cake Software Foundation, Inc.
* 1785 E. Sahara Avenue, Suite 490-204
* Las Vegas, Nevada 89104
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc.
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
* @package cake
* @subpackage cake.cake.libs.model
* @since CakePHP(tm) v 0.10.x.1402
* @version $Revision: 6311 $
* @modifiedby $LastChangedBy: phpnut $
* @lastmodified $Date: 2008-01-01 22:33:52 -0800 (Tue, 01 Jan 2008) $
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
/**
* Manages loaded instances of DataSource objects
*
* Long description for file
*
* @package cake
* @subpackage cake.cake.libs.model
*/
uses ('model' . DS . 'datasources' . DS . 'datasource');
config('database');
class ConnectionManager extends Object {
/**
* Holds a loaded instance of the Connections object
*
* @var object
* @access public
*/
var $config = null;
/**
* Holds instances DataSource objects
*
* @var array
* @access protected
*/
var $_dataSources = array();
/**
* Contains a list of all file and class names used in Connection settings
*
* @var array
* @access protected
*/
var $_connectionsEnum = array();
/**
* Constructor.
*
*/
function __construct() {
if (class_exists('DATABASE_CONFIG')) {
$this->config =& new DATABASE_CONFIG();
}
}
/**
* Gets a reference to the ConnectionManger object instance
*
* @return object Instance
* @access public
* @static
*/
function &getInstance() {
static $instance = array();
if (!isset($instance[0]) || !$instance[0]) {
$instance[0] =& new ConnectionManager();
}
return $instance[0];
}
/**
* Gets a reference to a DataSource object
*
* @param string $name The name of the DataSource, as defined in app/config/connections
* @return object Instance
* @access public
* @static
*/
function &getDataSource($name) {
$_this =& ConnectionManager::getInstance();
if (in_array($name, array_keys($_this->_dataSources))) {
return $_this->_dataSources[$name];
}
$connections = $_this->enumConnectionObjects();
if (in_array($name, array_keys($connections))) {
$conn = $connections[$name];
$class = $conn['classname'];
$_this->loadDataSource($name);
$_this->_dataSources[$name] =& new $class($_this->config->{$name});
$_this->_dataSources[$name]->configKeyName = $name;
} else {
trigger_error(sprintf(__("ConnectionManager::getDataSource - Non-existent data source %s", true), $name), E_USER_ERROR);
return null;
}
return $_this->_dataSources[$name];
}
/**
* Gets the list of available DataSource connections
*
* @return array List of available connections
* @access public
* @static
*/
function sourceList() {
$_this =& ConnectionManager::getInstance();
return array_keys($_this->_dataSources);
}
/**
* Gets a DataSource name from an object reference
*
* @param object $source DataSource object
* @return string Datasource name
* @access public
* @static
*/
function getSourceName(&$source) {
$_this =& ConnectionManager::getInstance();
$names = array_keys($_this->_dataSources);
for ($i = 0; $i < count($names); $i++) {
if ($_this->_dataSources[$names[$i]] === $source) {
return $names[$i];
}
}
return null;
}
/**
* Loads the DataSource class for the given connection name
*
* @param mixed $connName A string name of the connection, as defined in Connections config,
* or an array containing the file and class name of the object.
* @return boolean True on success, null on failure or false if the class is already loaded
* @access public
* @static
*/
function loadDataSource($connName) {
$_this =& ConnectionManager::getInstance();
if (is_array($connName)) {
$conn = $connName;
} else {
$connections = $_this->enumConnectionObjects();
$conn = $connections[$connName];
}
if (isset($conn['parent']) && !empty($conn['parent'])) {
$_this->loadDataSource($conn['parent']);
}
if (class_exists($conn['classname'])) {
return false;
}
if (file_exists(MODELS . 'datasources' . DS . $conn['filename'] . '.php')) {
require (MODELS . 'datasources' . DS . $conn['filename'] . '.php');
} elseif (fileExistsInPath(LIBS . 'model' . DS . 'datasources' . DS . $conn['filename'] . '.php')) {
require (LIBS . 'model' . DS . 'datasources' . DS . $conn['filename'] . '.php');
} else {
trigger_error(sprintf(__('Unable to load DataSource file %s.php', true), $conn['filename']), E_USER_ERROR);
return null;
}
}
/**
* Gets a list of class and file names associated with the user-defined DataSource connections
*
* @return array An associative array of elements where the key is the connection name
* (as defined in Connections), and the value is an array with keys 'filename' and 'classname'.
* @access public
* @static
*/
function enumConnectionObjects() {
$_this =& ConnectionManager::getInstance();
if (!empty($_this->_connectionsEnum)) {
return $_this->_connectionsEnum;
}
$connections = get_object_vars($_this->config);
if ($connections != null) {
foreach ($connections as $name => $config) {
$_this->_connectionsEnum[$name] = $_this->__getDriver($config);
}
return $_this->_connectionsEnum;
} else {
$_this->cakeError('missingConnection', array(array('className' => 'ConnectionManager')));
}
}
/**
* Dynamically creates a DataSource object at runtime, with the given name and settings
*
* @param string $name The DataSource name
* @param array $config The DataSource configuration settings
* @return object A reference to the DataSource object, or null if creation failed
* @access public
* @static
*/
function &create($name = '', $config = array()) {
$_this =& ConnectionManager::getInstance();
if (empty($name) || empty($config) || array_key_exists($name, $_this->_connectionsEnum)) {
$null = null;
return $null;
}
$_this->config->{$name} = $config;
$_this->_connectionsEnum[$name] = $_this->__getDriver($config);
return $_this->getDataSource($name);
}
/**
* Returns the file, class name, and parent for the given driver.
*
* @return array An indexed array with: filename, classname, and parent
* @access private
*/
function __getDriver($config) {
$_this =& ConnectionManager::getInstance();
if (!isset($config['datasource'])) {
$config['datasource'] = 'dbo';
}
if (isset($config['driver']) && $config['driver'] != null && !empty($config['driver'])) {
$filename = $config['datasource'] . DS . $config['datasource'] . '_' . $config['driver'];
$classname = Inflector::camelize(strtolower($config['datasource'] . '_' . $config['driver']));
$parent = $_this->__getDriver(array('datasource' => $config['datasource']));
} else {
$filename = $config['datasource'] . '_source';
$classname = Inflector::camelize(strtolower($config['datasource'] . '_source'));
$parent = null;
}
return array('filename' => $filename, 'classname' => $classname, 'parent' => $parent);
}
/**
* Destructor.
*
* @access private
*/
function __destruct() {
if (Configure::read('Session.save') == 'database' && function_exists('session_write_close')) {
session_write_close();
}
}
}
?>
|