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
00051 var $config = null;
00052
00053
00054
00055
00056
00057
00058 var $_dataSources = array();
00059
00060
00061
00062
00063
00064
00065 var $_connectionsEnum = array();
00066
00067
00068
00069
00070 function __construct() {
00071 if (class_exists('DATABASE_CONFIG')) {
00072 $this->config = new DATABASE_CONFIG();
00073 }
00074 }
00075
00076
00077
00078
00079
00080 function &getInstance() {
00081 static $instance = array();
00082
00083 if (!isset($instance[0]) || !$instance[0]) {
00084 $instance[0] = &new ConnectionManager();
00085 }
00086
00087 return $instance[0];
00088 }
00089
00090
00091
00092
00093
00094
00095 function &getDataSource($name) {
00096 $_this =& ConnectionManager::getInstance();
00097
00098 if (in_array($name, array_keys($_this->_dataSources))) {
00099 return $_this->_dataSources[$name];
00100 }
00101
00102 $connections = $_this->enumConnectionObjects();
00103 if (in_array($name, array_keys($connections))) {
00104 $conn = $connections[$name];
00105 $class = $conn['classname'];
00106 $_this->loadDataSource($name);
00107 $_this->_dataSources[$name] =& new $class($_this->config->{$name});
00108 $_this->_dataSources[$name]->configKeyName = $name;
00109 } else {
00110 trigger_error(sprintf(__("ConnectionManager::getDataSource - Non-existent data source %s", true), $name), E_USER_ERROR);
00111 return null;
00112 }
00113
00114 return $_this->_dataSources[$name];
00115 }
00116
00117
00118
00119
00120
00121
00122 function getSourceName(&$source) {
00123 $_this =& ConnectionManager::getInstance();
00124
00125 $names = array_keys($_this->_dataSources);
00126 for ($i = 0; $i < count($names); $i++) {
00127 if ($_this->_dataSources[$names[$i]] === $source) {
00128 return $names[$i];
00129 }
00130 }
00131 return null;
00132 }
00133
00134
00135
00136
00137
00138
00139
00140 function loadDataSource($connName) {
00141 $_this =& ConnectionManager::getInstance();
00142
00143 if (is_array($connName)) {
00144 $conn = $connName;
00145 } else {
00146 $connections = $_this->enumConnectionObjects();
00147 $conn = $connections[$connName];
00148 }
00149
00150 if (isset($conn['parent']) && !empty($conn['parent'])) {
00151 $_this->loadDataSource($conn['parent']);
00152 }
00153
00154 if (class_exists($conn['classname'])) {
00155 return false;
00156 }
00157
00158 if (file_exists(MODELS . DS . $conn['filename'] . '.php')) {
00159 require (MODELS . DS . $conn['filename'] . '.php');
00160 } elseif (fileExistsInPath(LIBS . 'model' . DS . $conn['filename'] . '.php')) {
00161 require (LIBS . 'model' . DS . $conn['filename'] . '.php');
00162 } else {
00163 trigger_error(sprintf(__('Unable to load DataSource file %s.php', true), $conn['filename']), E_USER_ERROR);
00164 return null;
00165 }
00166 }
00167
00168
00169
00170
00171
00172
00173 function enumConnectionObjects() {
00174 $_this =& ConnectionManager::getInstance();
00175
00176 if (!empty($_this->_connectionsEnum)) {
00177 return $_this->_connectionsEnum;
00178 }
00179 $connections = get_object_vars($_this->config);
00180
00181 if ($connections != null) {
00182 foreach ($connections as $name => $config) {
00183 $_this->_connectionsEnum[$name] = $_this->__getDriver($config);
00184 }
00185 return $_this->_connectionsEnum;
00186 } else {
00187 $_this->cakeError('missingConnection', array(array('className' => 'ConnectionManager')));
00188 }
00189 }
00190
00191
00192
00193
00194
00195
00196
00197 function &create($name = '', $config = array()) {
00198 $_this =& ConnectionManager::getInstance();
00199
00200 if (empty($name) || empty($config) || array_key_exists($name, $_this->_connectionsEnum)) {
00201 $null = null;
00202 return $null;
00203 }
00204
00205 $_this->config->{$name} = $config;
00206 $_this->_connectionsEnum[$name] = $_this->__getDriver($config);
00207 return $_this->getDataSource($name);
00208 }
00209
00210
00211
00212
00213
00214 function __getDriver($config) {
00215 $_this =& ConnectionManager::getInstance();
00216
00217 if (!isset($config['datasource'])) {
00218 $config['datasource'] = 'dbo';
00219 }
00220
00221 if (isset($config['driver']) && $config['driver'] != null && !empty($config['driver'])) {
00222 $filename = $config['datasource'] . DS . $config['datasource'] . '_' . $config['driver'];
00223 $classname = Inflector::camelize(strtolower($config['datasource'] . '_' . $config['driver']));
00224 $parent = $_this->__getDriver(array('datasource' => $config['datasource']));
00225 } else {
00226 $filename = 'datasources' . DS . $config['datasource'] . '_source';
00227 $classname = Inflector::camelize(strtolower($config['datasource'] . '_source'));
00228 $parent = null;
00229 }
00230 return array('filename' => $filename, 'classname' => $classname, 'parent' => $parent);
00231 }
00232
00233
00234
00235
00236 function __destruct() {
00237 if (CAKE_SESSION_SAVE == 'database' && function_exists('session_write_close')) {
00238 session_write_close();
00239 }
00240 }
00241 }
00242
00243 ?>