connection_manager.php
Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 uses ('model' . DS . 'datasources' . DS . 'datasource');
00041 config('database');
00042
00043 class ConnectionManager extends Object {
00044
00045
00046
00047
00048
00049
00050 var $config = null;
00051
00052
00053
00054
00055
00056
00057 var $_dataSources = array();
00058
00059
00060
00061
00062
00063
00064 var $_connectionsEnum = array();
00065
00066
00067
00068
00069 function __construct() {
00070 if (class_exists('DATABASE_CONFIG')) {
00071 $this->config =& new DATABASE_CONFIG();
00072 }
00073 }
00074
00075
00076
00077
00078
00079
00080
00081 function &getInstance() {
00082 static $instance = array();
00083
00084 if (!isset($instance[0]) || !$instance[0]) {
00085 $instance[0] =& new ConnectionManager();
00086 }
00087
00088 return $instance[0];
00089 }
00090
00091
00092
00093
00094
00095
00096
00097
00098 function &getDataSource($name) {
00099 $_this =& ConnectionManager::getInstance();
00100
00101 if (in_array($name, array_keys($_this->_dataSources))) {
00102 return $_this->_dataSources[$name];
00103 }
00104
00105 $connections = $_this->enumConnectionObjects();
00106 if (in_array($name, array_keys($connections))) {
00107 $conn = $connections[$name];
00108 $class = $conn['classname'];
00109 $_this->loadDataSource($name);
00110 $_this->_dataSources[$name] =& new $class($_this->config->{$name});
00111 $_this->_dataSources[$name]->configKeyName = $name;
00112 } else {
00113 trigger_error(sprintf(__("ConnectionManager::getDataSource - Non-existent data source %s", true), $name), E_USER_ERROR);
00114 return null;
00115 }
00116
00117 return $_this->_dataSources[$name];
00118 }
00119
00120
00121
00122
00123
00124
00125
00126 function sourceList() {
00127 $_this =& ConnectionManager::getInstance();
00128 return array_keys($_this->_dataSources);
00129 }
00130
00131
00132
00133
00134
00135
00136
00137
00138 function getSourceName(&$source) {
00139 $_this =& ConnectionManager::getInstance();
00140 $names = array_keys($_this->_dataSources);
00141 for ($i = 0; $i < count($names); $i++) {
00142 if ($_this->_dataSources[$names[$i]] === $source) {
00143 return $names[$i];
00144 }
00145 }
00146 return null;
00147 }
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157 function loadDataSource($connName) {
00158 $_this =& ConnectionManager::getInstance();
00159
00160 if (is_array($connName)) {
00161 $conn = $connName;
00162 } else {
00163 $connections = $_this->enumConnectionObjects();
00164 $conn = $connections[$connName];
00165 }
00166
00167 if (isset($conn['parent']) && !empty($conn['parent'])) {
00168 $_this->loadDataSource($conn['parent']);
00169 }
00170
00171 if (class_exists($conn['classname'])) {
00172 return false;
00173 }
00174
00175 if (file_exists(MODELS . 'datasources' . DS . $conn['filename'] . '.php')) {
00176 require (MODELS . 'datasources' . DS . $conn['filename'] . '.php');
00177 } elseif (fileExistsInPath(LIBS . 'model' . DS . 'datasources' . DS . $conn['filename'] . '.php')) {
00178 require (LIBS . 'model' . DS . 'datasources' . DS . $conn['filename'] . '.php');
00179 } else {
00180 trigger_error(sprintf(__('Unable to load DataSource file %s.php', true), $conn['filename']), E_USER_ERROR);
00181 return null;
00182 }
00183 }
00184
00185
00186
00187
00188
00189
00190
00191
00192 function enumConnectionObjects() {
00193 $_this =& ConnectionManager::getInstance();
00194
00195 if (!empty($_this->_connectionsEnum)) {
00196 return $_this->_connectionsEnum;
00197 }
00198 $connections = get_object_vars($_this->config);
00199
00200 if ($connections != null) {
00201 foreach ($connections as $name => $config) {
00202 $_this->_connectionsEnum[$name] = $_this->__getDriver($config);
00203 }
00204 return $_this->_connectionsEnum;
00205 } else {
00206 $_this->cakeError('missingConnection', array(array('className' => 'ConnectionManager')));
00207 }
00208 }
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218 function &create($name = '', $config = array()) {
00219 $_this =& ConnectionManager::getInstance();
00220
00221 if (empty($name) || empty($config) || array_key_exists($name, $_this->_connectionsEnum)) {
00222 $null = null;
00223 return $null;
00224 }
00225
00226 $_this->config->{$name} = $config;
00227 $_this->_connectionsEnum[$name] = $_this->__getDriver($config);
00228 return $_this->getDataSource($name);
00229 }
00230
00231
00232
00233
00234
00235
00236 function __getDriver($config) {
00237 $_this =& ConnectionManager::getInstance();
00238
00239 if (!isset($config['datasource'])) {
00240 $config['datasource'] = 'dbo';
00241 }
00242
00243 if (isset($config['driver']) && $config['driver'] != null && !empty($config['driver'])) {
00244 $filename = $config['datasource'] . DS . $config['datasource'] . '_' . $config['driver'];
00245 $classname = Inflector::camelize(strtolower($config['datasource'] . '_' . $config['driver']));
00246 $parent = $_this->__getDriver(array('datasource' => $config['datasource']));
00247 } else {
00248 $filename = $config['datasource'] . '_source';
00249 $classname = Inflector::camelize(strtolower($config['datasource'] . '_source'));
00250 $parent = null;
00251 }
00252 return array('filename' => $filename, 'classname' => $classname, 'parent' => $parent);
00253 }
00254
00255
00256
00257
00258
00259 function __destruct() {
00260 if (Configure::read('Session.save') == 'database' && function_exists('session_write_close')) {
00261 session_write_close();
00262 }
00263 }
00264 }
00265
00266 ?>