1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21:
22: require LIBS . 'model' . DS . 'datasources' . DS . 'datasource.php';
23: include_once CONFIGS . 'database.php';
24:
25: 26: 27: 28: 29: 30:
31: class ConnectionManager extends Object {
32:
33: 34: 35: 36: 37: 38:
39: var $config = null;
40:
41: 42: 43: 44: 45: 46:
47: var $_dataSources = array();
48:
49: 50: 51: 52: 53: 54:
55: var $_connectionsEnum = array();
56:
57: 58: 59: 60:
61: function __construct() {
62: if (class_exists('DATABASE_CONFIG')) {
63: $this->config =& new DATABASE_CONFIG();
64: $this->_getConnectionObjects();
65: }
66: }
67:
68: 69: 70: 71: 72: 73: 74:
75: function &getInstance() {
76: static $instance = array();
77:
78: if (!$instance) {
79: $instance[0] =& new ConnectionManager();
80: }
81:
82: return $instance[0];
83: }
84:
85: 86: 87: 88: 89: 90: 91: 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: if (empty($_this->_connectionsEnum[$name])) {
102: trigger_error(sprintf(__("ConnectionManager::getDataSource - Non-existent data source %s", true), $name), E_USER_ERROR);
103: $null = null;
104: return $null;
105: }
106: $conn = $_this->_connectionsEnum[$name];
107: $class = $conn['classname'];
108:
109: if ($_this->loadDataSource($name) === null) {
110: trigger_error(sprintf(__("ConnectionManager::getDataSource - Could not load class %s", true), $class), E_USER_ERROR);
111: $null = null;
112: return $null;
113: }
114: $_this->_dataSources[$name] =& new $class($_this->config->{$name});
115: $_this->_dataSources[$name]->configKeyName = $name;
116:
117: $return =& $_this->_dataSources[$name];
118: return $return;
119: }
120:
121: 122: 123: 124: 125: 126: 127:
128: function sourceList() {
129: $_this =& ConnectionManager::getInstance();
130: return array_keys($_this->_dataSources);
131: }
132:
133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143:
144: function getSourceName(&$source) {
145: $_this =& ConnectionManager::getInstance();
146: foreach ($_this->_dataSources as $name => $ds) {
147: if ($ds == $source) {
148: return $name;
149: }
150: }
151: return '';
152: }
153:
154: 155: 156: 157: 158: 159: 160: 161: 162: 163:
164: function loadDataSource($connName) {
165: $_this =& ConnectionManager::getInstance();
166:
167: if (is_array($connName)) {
168: $conn = $connName;
169: } else {
170: $conn = $_this->_connectionsEnum[$connName];
171: }
172:
173: if (class_exists($conn['classname'])) {
174: return false;
175: }
176:
177: if (!empty($conn['parent'])) {
178: $_this->loadDataSource($conn['parent']);
179: }
180:
181: $conn = array_merge(array('plugin' => null, 'classname' => null, 'parent' => null), $conn);
182: $class = "{$conn['plugin']}.{$conn['classname']}";
183:
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);
186: return null;
187: }
188: return true;
189: }
190:
191: 192: 193: 194: 195: 196: 197: 198:
199: function enumConnectionObjects() {
200: $_this =& ConnectionManager::getInstance();
201:
202: return $_this->_connectionsEnum;
203: }
204:
205: 206: 207: 208: 209: 210: 211: 212: 213:
214: function &create($name = '', $config = array()) {
215: $_this =& ConnectionManager::getInstance();
216:
217: if (empty($name) || empty($config) || array_key_exists($name, $_this->_connectionsEnum)) {
218: $null = null;
219: return $null;
220: }
221: $_this->config->{$name} = $config;
222: $_this->_connectionsEnum[$name] = $_this->__connectionData($config);
223: $return =& $_this->getDataSource($name);
224: return $return;
225: }
226:
227: 228: 229: 230: 231: 232: 233:
234: function _getConnectionObjects() {
235: $connections = get_object_vars($this->config);
236:
237: if ($connections != null) {
238: foreach ($connections as $name => $config) {
239: $this->_connectionsEnum[$name] = $this->__connectionData($config);
240: }
241: } else {
242: $this->cakeError('missingConnection', array(array('code' => 500, 'className' => 'ConnectionManager')));
243: }
244: }
245:
246: 247: 248: 249: 250: 251:
252: function __connectionData($config) {
253: if (!isset($config['datasource'])) {
254: $config['datasource'] = 'dbo';
255: }
256: $filename = $classname = $parent = $plugin = null;
257:
258: if (!empty($config['driver'])) {
259: $parent = $this->__connectionData(array('datasource' => $config['datasource']));
260: $parentSource = preg_replace('/_source$/', '', $parent['filename']);
261:
262: list($plugin, $classname) = pluginSplit($config['driver']);
263: if ($plugin) {
264: $source = Inflector::underscore($classname);
265: } else {
266: $source = $parentSource . '_' . $config['driver'];
267: $classname = Inflector::camelize(strtolower($source));
268: }
269: $filename = $parentSource . DS . $source;
270: } else {
271: list($plugin, $classname) = pluginSplit($config['datasource']);
272: if ($plugin) {
273: $filename = Inflector::underscore($classname);
274: } else {
275: $filename = Inflector::underscore($config['datasource']);
276: }
277: if (substr($filename, -7) != '_source') {
278: $filename .= '_source';
279: }
280: $classname = Inflector::camelize(strtolower($filename));
281: }
282: return compact('filename', 'classname', 'parent', 'plugin');
283: }
284:
285: 286: 287: 288: 289:
290: function __destruct() {
291: if (Configure::read('Session.save') == 'database' && function_exists('session_write_close')) {
292: session_write_close();
293: }
294: }
295: }
296: