config =& new DATABASE_CONFIG(); } } /** * Gets a reference to the ConnectionManger object instance * * @return object Instance * @access public * @static */ function &getInstance() { static $instance = array(); if (!$instance) { $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/database.php * @return object Instance * @access public * @static */ function &getDataSource($name) { $_this =& ConnectionManager::getInstance(); if (!empty($_this->_dataSources[$name])) { $return =& $_this->_dataSources[$name]; return $return; } $connections = $_this->enumConnectionObjects(); if (empty($connections[$name])) { trigger_error(sprintf(__("ConnectionManager::getDataSource - Non-existent data source %s", true), $name), E_USER_ERROR); $null = null; return $null; } $conn = $connections[$name]; if (strpos($conn['classname'], '.') !== false) { list($plugin, $class) = explode('.', $conn['classname']); } else { $class = $conn['classname']; } $class = Inflector::classify($class); if ($_this->loadDataSource($name) === null) { trigger_error(sprintf(__("ConnectionManager::getDataSource - Could not load class %s", true), $class), E_USER_ERROR); $null = null; return $null; } $_this->_dataSources[$name] =& new $class($_this->config->{$name}); $_this->_dataSources[$name]->configKeyName = $name; $return =& $_this->_dataSources[$name]; return $return; } /** * 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 app/config/database.php, * or an array containing the filename (without extension) and class name of the object, * to be found in app/models/datasources/ or cake/libs/model/datasources/. * @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 (!empty($conn['parent'])) { $_this->loadDataSource($conn['parent']); } if (class_exists($conn['classname'])) { return false; } $conn = array_merge(array('plugin' => null, 'classname' => null, 'parent' => null), $conn); $class = "{$conn['plugin']}.{$conn['classname']}"; if (!App::import('Datasource', $class)) { $error = __('ConnectionManager::loadDataSource - Unable to import DataSource class %s', true); trigger_error(sprintf($error, $class), E_USER_ERROR); return null; } return true; } /** * 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); return $return; } /** * 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) { if (!isset($config['datasource'])) { $config['datasource'] = 'dbo'; } $filename = $classname = $parent = $plugin = null; 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 { if (strpos($config['datasource'], '.') !== false) { list($plugin, $classname) = explode('.', $config['datasource']); $filename = Inflector::underscore($classname); } else { $filename = $config['datasource'] . '_source'; $classname = Inflector::camelize(strtolower($config['datasource'] . '_source')); } } $driver = compact('filename', 'classname', 'parent', 'plugin'); return $driver; } /** * Destructor. * * @access private */ function __destruct() { if (Configure::read('Session.save') == 'database' && function_exists('session_write_close')) { session_write_close(); } } } ?>