2006-01-12 02:10:47 +00:00
|
|
|
<?php
|
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Short description for file.
|
|
|
|
*
|
|
|
|
* Long description for file
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2007-02-02 10:39:45 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
|
|
|
|
* Copyright 2005-2007, Cake Software Foundation, Inc.
|
2006-05-26 05:29:17 +00:00
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
2007-02-02 10:39:45 +00:00
|
|
|
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
|
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
2006-05-26 05:29:17 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.model
|
2007-02-02 10:39:45 +00:00
|
|
|
* @since CakePHP(tm) v 0.10.x.1402
|
2006-05-26 05:29:17 +00:00
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
2006-01-12 02:10:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manages loaded instances of DataSource objects
|
|
|
|
*
|
|
|
|
* Long description for file
|
|
|
|
*
|
2006-05-26 05:29:17 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.model
|
2006-01-12 02:10:47 +00:00
|
|
|
*/
|
|
|
|
|
2006-05-26 05:29:17 +00:00
|
|
|
uses ('model' . DS . 'datasources' . DS . 'datasource');
|
2006-11-21 21:48:05 +00:00
|
|
|
config('database');
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2006-06-14 22:49:42 +00:00
|
|
|
class ConnectionManager extends Object {
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
|
|
|
* Holds a loaded instance of the Connections object
|
|
|
|
*
|
2007-10-30 17:42:31 +00:00
|
|
|
* @var object
|
2006-01-12 02:10:47 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
var $config = null;
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
|
|
|
* Holds instances DataSource objects
|
|
|
|
*
|
|
|
|
* @var array
|
2007-11-05 02:47:04 +00:00
|
|
|
* @access protected
|
2006-01-12 02:10:47 +00:00
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
var $_dataSources = array();
|
2006-03-15 20:45:59 +00:00
|
|
|
/**
|
|
|
|
* Contains a list of all file and class names used in Connection settings
|
|
|
|
*
|
|
|
|
* @var array
|
2007-11-05 02:47:04 +00:00
|
|
|
* @access protected
|
2006-03-15 20:45:59 +00:00
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
var $_connectionsEnum = array();
|
2006-01-17 17:52:23 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
function __construct() {
|
|
|
|
if (class_exists('DATABASE_CONFIG')) {
|
2007-10-30 17:42:31 +00:00
|
|
|
$this->config =& new DATABASE_CONFIG();
|
2006-06-14 18:02:37 +00:00
|
|
|
}
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
|
|
|
* Gets a reference to the ConnectionManger object instance
|
|
|
|
*
|
2007-10-30 17:42:31 +00:00
|
|
|
* @return object Instance
|
|
|
|
* @access public
|
|
|
|
* @static
|
2006-01-12 02:10:47 +00:00
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
function &getInstance() {
|
|
|
|
static $instance = array();
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2006-06-14 18:02:37 +00:00
|
|
|
if (!isset($instance[0]) || !$instance[0]) {
|
2007-10-30 17:42:31 +00:00
|
|
|
$instance[0] =& new ConnectionManager();
|
2006-06-14 18:02:37 +00:00
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
|
2006-06-14 18:02:37 +00:00
|
|
|
return $instance[0];
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
|
|
|
* Gets a reference to a DataSource object
|
|
|
|
*
|
|
|
|
* @param string $name The name of the DataSource, as defined in app/config/connections
|
2007-10-30 17:42:31 +00:00
|
|
|
* @return object Instance
|
|
|
|
* @access public
|
|
|
|
* @static
|
2006-01-12 02:10:47 +00:00
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
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 {
|
2006-12-22 22:49:47 +00:00
|
|
|
trigger_error(sprintf(__("ConnectionManager::getDataSource - Non-existent data source %s", true), $name), E_USER_ERROR);
|
2006-06-14 18:02:37 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $_this->_dataSources[$name];
|
|
|
|
}
|
2007-08-11 21:52:23 +00:00
|
|
|
/**
|
|
|
|
* Gets the list of available DataSource connections
|
|
|
|
*
|
2007-10-30 17:42:31 +00:00
|
|
|
* @return array List of available connections
|
|
|
|
* @access public
|
|
|
|
* @static
|
2007-08-11 21:52:23 +00:00
|
|
|
*/
|
|
|
|
function sourceList() {
|
|
|
|
$_this =& ConnectionManager::getInstance();
|
|
|
|
return array_keys($_this->_dataSources);
|
|
|
|
}
|
2006-06-14 18:02:37 +00:00
|
|
|
/**
|
|
|
|
* Gets a DataSource name from an object reference
|
|
|
|
*
|
2007-10-30 17:42:31 +00:00
|
|
|
* @param object $source DataSource object
|
|
|
|
* @return string Datasource name
|
|
|
|
* @access public
|
|
|
|
* @static
|
2006-06-14 18:02:37 +00:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
2006-03-15 20:45:59 +00:00
|
|
|
/**
|
|
|
|
* Loads the DataSource class for the given connection name
|
|
|
|
*
|
2006-10-18 23:03:55 +00:00
|
|
|
* @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.
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean True on success, null on failure or false if the class is already loaded
|
2007-10-30 17:42:31 +00:00
|
|
|
* @access public
|
|
|
|
* @static
|
2006-03-15 20:45:59 +00:00
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
function loadDataSource($connName) {
|
|
|
|
$_this =& ConnectionManager::getInstance();
|
2006-10-18 23:03:55 +00:00
|
|
|
|
|
|
|
if (is_array($connName)) {
|
|
|
|
$conn = $connName;
|
|
|
|
} else {
|
|
|
|
$connections = $_this->enumConnectionObjects();
|
|
|
|
$conn = $connections[$connName];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($conn['parent']) && !empty($conn['parent'])) {
|
|
|
|
$_this->loadDataSource($conn['parent']);
|
|
|
|
}
|
2006-06-14 18:02:37 +00:00
|
|
|
|
|
|
|
if (class_exists($conn['classname'])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if (file_exists(MODELS . 'datasources' . DS . $conn['filename'] . '.php')) {
|
2006-10-18 23:03:55 +00:00
|
|
|
require (MODELS . 'datasources' . DS . $conn['filename'] . '.php');
|
2007-06-20 06:15:35 +00:00
|
|
|
} elseif (fileExistsInPath(LIBS . 'model' . DS . 'datasources' . DS . $conn['filename'] . '.php')) {
|
2006-11-03 05:45:11 +00:00
|
|
|
require (LIBS . 'model' . DS . 'datasources' . DS . $conn['filename'] . '.php');
|
2006-06-14 18:02:37 +00:00
|
|
|
} else {
|
2006-12-22 22:49:47 +00:00
|
|
|
trigger_error(sprintf(__('Unable to load DataSource file %s.php', true), $conn['filename']), E_USER_ERROR);
|
2006-06-14 18:02:37 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2006-03-15 20:45:59 +00:00
|
|
|
/**
|
|
|
|
* 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'.
|
2007-10-30 17:42:31 +00:00
|
|
|
* @access public
|
|
|
|
* @static
|
2006-03-15 20:45:59 +00:00
|
|
|
*/
|
2006-06-14 18:02:37 +00:00
|
|
|
function enumConnectionObjects() {
|
|
|
|
$_this =& ConnectionManager::getInstance();
|
|
|
|
|
|
|
|
if (!empty($_this->_connectionsEnum)) {
|
|
|
|
return $_this->_connectionsEnum;
|
|
|
|
}
|
|
|
|
$connections = get_object_vars($_this->config);
|
|
|
|
|
|
|
|
if ($connections != null) {
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($connections as $name => $config) {
|
2006-06-29 06:23:11 +00:00
|
|
|
$_this->_connectionsEnum[$name] = $_this->__getDriver($config);
|
2006-06-14 18:02:37 +00:00
|
|
|
}
|
2007-03-16 13:26:09 +00:00
|
|
|
return $_this->_connectionsEnum;
|
2006-06-14 18:02:37 +00:00
|
|
|
} else {
|
2007-03-16 13:26:09 +00:00
|
|
|
$_this->cakeError('missingConnection', array(array('className' => 'ConnectionManager')));
|
2006-06-14 18:02:37 +00:00
|
|
|
}
|
|
|
|
}
|
2006-06-29 06:23:11 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2007-10-30 17:42:31 +00:00
|
|
|
* @access public
|
|
|
|
* @static
|
2006-06-29 06:23:11 +00:00
|
|
|
*/
|
|
|
|
function &create($name = '', $config = array()) {
|
|
|
|
$_this =& ConnectionManager::getInstance();
|
|
|
|
|
|
|
|
if (empty($name) || empty($config) || array_key_exists($name, $_this->_connectionsEnum)) {
|
2007-04-05 04:47:01 +00:00
|
|
|
$null = null;
|
|
|
|
return $null;
|
2006-06-29 06:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$_this->config->{$name} = $config;
|
|
|
|
$_this->_connectionsEnum[$name] = $_this->__getDriver($config);
|
|
|
|
return $_this->getDataSource($name);
|
|
|
|
}
|
|
|
|
/**
|
2007-10-30 17:42:31 +00:00
|
|
|
* Returns the file, class name, and parent for the given driver.
|
2006-06-29 06:23:11 +00:00
|
|
|
*
|
2007-10-30 17:42:31 +00:00
|
|
|
* @return array An indexed array with: filename, classname, and parent
|
|
|
|
* @access private
|
2006-06-29 06:23:11 +00:00
|
|
|
*/
|
|
|
|
function __getDriver($config) {
|
2006-10-18 23:03:55 +00:00
|
|
|
$_this =& ConnectionManager::getInstance();
|
|
|
|
|
2006-06-29 06:23:11 +00:00
|
|
|
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']));
|
2006-10-18 23:03:55 +00:00
|
|
|
$parent = $_this->__getDriver(array('datasource' => $config['datasource']));
|
2006-06-29 06:23:11 +00:00
|
|
|
} else {
|
|
|
|
$filename = $config['datasource'] . '_source';
|
|
|
|
$classname = Inflector::camelize(strtolower($config['datasource'] . '_source'));
|
2006-10-18 23:03:55 +00:00
|
|
|
$parent = null;
|
2006-06-29 06:23:11 +00:00
|
|
|
}
|
2006-10-18 23:03:55 +00:00
|
|
|
return array('filename' => $filename, 'classname' => $classname, 'parent' => $parent);
|
2006-06-29 06:23:11 +00:00
|
|
|
}
|
2006-06-19 16:15:49 +00:00
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*
|
2007-10-30 17:42:31 +00:00
|
|
|
* @access private
|
2006-06-19 16:15:49 +00:00
|
|
|
*/
|
|
|
|
function __destruct() {
|
2007-10-16 09:05:25 +00:00
|
|
|
if (Configure::read('Session.save') == 'database' && function_exists('session_write_close')) {
|
2006-06-19 16:15:49 +00:00
|
|
|
session_write_close();
|
|
|
|
}
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
}
|
2006-06-14 22:49:42 +00:00
|
|
|
|
2006-01-12 02:10:47 +00:00
|
|
|
?>
|