2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-03-19 21:10:13 +00:00
|
|
|
* Datasource connection manager
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-03-19 21:10:13 +00:00
|
|
|
* Provides an interface for loading and enumerating connections defined in app/config/database.php
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.model
|
|
|
|
* @since CakePHP(tm) v 0.10.x.1402
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
|
2010-12-04 15:46:42 +00:00
|
|
|
App::uses('DataSource', 'Model/Datasource');
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Manages loaded instances of DataSource objects
|
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.model
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-07-06 03:08:25 +00:00
|
|
|
class ConnectionManager {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Holds a loaded instance of the Connections object
|
|
|
|
*
|
2009-03-19 21:10:13 +00:00
|
|
|
* @var DATABASE_CONFIG
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $config = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Holds instances DataSource objects
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access protected
|
|
|
|
*/
|
2010-04-04 06:36:12 +00:00
|
|
|
protected $_dataSources = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Contains a list of all file and class names used in Connection settings
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access protected
|
|
|
|
*/
|
2010-04-04 06:36:12 +00:00
|
|
|
protected $_connectionsEnum = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function __construct() {
|
2010-12-03 22:38:52 +00:00
|
|
|
include_once CONFIGS . 'database.php';
|
2008-05-30 11:40:08 +00:00
|
|
|
if (class_exists('DATABASE_CONFIG')) {
|
2010-11-09 03:07:34 +00:00
|
|
|
$this->config = new DATABASE_CONFIG();
|
2010-03-06 02:37:28 +00:00
|
|
|
$this->_getConnectionObjects();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Gets a reference to the ConnectionManger object instance
|
|
|
|
*
|
|
|
|
* @return object Instance
|
|
|
|
*/
|
2010-07-06 03:08:25 +00:00
|
|
|
public static function &getInstance() {
|
2008-05-30 11:40:08 +00:00
|
|
|
static $instance = array();
|
|
|
|
|
2008-09-12 05:11:34 +00:00
|
|
|
if (!$instance) {
|
2010-11-09 03:07:34 +00:00
|
|
|
$instance[0] = new ConnectionManager();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $instance[0];
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Gets a reference to a DataSource object
|
|
|
|
*
|
2009-03-19 21:10:13 +00:00
|
|
|
* @param string $name The name of the DataSource, as defined in app/config/database.php
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return object Instance
|
|
|
|
*/
|
2010-07-06 03:08:25 +00:00
|
|
|
public static function &getDataSource($name) {
|
2010-11-09 03:07:34 +00:00
|
|
|
$_this = ConnectionManager::getInstance();
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-08-07 15:36:26 +00:00
|
|
|
if (!empty($_this->_dataSources[$name])) {
|
2010-11-09 03:07:34 +00:00
|
|
|
$return = $_this->_dataSources[$name];
|
2008-08-07 15:36:26 +00:00
|
|
|
return $return;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-09-25 02:56:02 +00:00
|
|
|
|
2010-01-16 17:03:13 +00:00
|
|
|
if (empty($_this->_connectionsEnum[$name])) {
|
2010-04-15 16:00:25 +00:00
|
|
|
trigger_error(sprintf(__("ConnectionManager::getDataSource - Non-existent data source %s"), $name), E_USER_ERROR);
|
2009-09-24 23:29:35 +00:00
|
|
|
$null = null;
|
|
|
|
return $null;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-01-16 17:03:13 +00:00
|
|
|
$conn = $_this->_connectionsEnum[$name];
|
2009-09-25 03:48:52 +00:00
|
|
|
$class = $conn['classname'];
|
2009-09-25 02:56:02 +00:00
|
|
|
|
|
|
|
if ($_this->loadDataSource($name) === null) {
|
2010-04-15 16:00:25 +00:00
|
|
|
trigger_error(sprintf(__("ConnectionManager::getDataSource - Could not load class %s"), $class), E_USER_ERROR);
|
2009-09-25 02:56:02 +00:00
|
|
|
$null = null;
|
|
|
|
return $null;
|
|
|
|
}
|
2010-11-09 03:07:34 +00:00
|
|
|
$_this->_dataSources[$name] = new $class($_this->config->{$name});
|
2009-09-25 02:56:02 +00:00
|
|
|
$_this->_dataSources[$name]->configKeyName = $name;
|
|
|
|
|
2010-11-09 03:07:34 +00:00
|
|
|
$return = $_this->_dataSources[$name];
|
2008-08-07 15:36:26 +00:00
|
|
|
return $return;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Gets the list of available DataSource connections
|
|
|
|
*
|
|
|
|
* @return array List of available connections
|
|
|
|
*/
|
2010-07-06 03:08:25 +00:00
|
|
|
public static function sourceList() {
|
2010-11-09 03:07:34 +00:00
|
|
|
$_this = ConnectionManager::getInstance();
|
2008-05-30 11:40:08 +00:00
|
|
|
return array_keys($_this->_dataSources);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-21 18:18:00 +00:00
|
|
|
* Gets a DataSource name from an object reference.
|
|
|
|
*
|
|
|
|
* **Warning** this method may cause fatal errors in PHP4.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param object $source DataSource object
|
2009-09-25 03:48:52 +00:00
|
|
|
* @return string Datasource name, or null if source is not present
|
2010-01-21 18:18:00 +00:00
|
|
|
* in the ConnectionManager.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-07-06 03:08:25 +00:00
|
|
|
public static function getSourceName(&$source) {
|
2010-11-09 03:07:34 +00:00
|
|
|
$_this = ConnectionManager::getInstance();
|
2010-01-11 14:48:53 +00:00
|
|
|
foreach ($_this->_dataSources as $name => $ds) {
|
|
|
|
if ($ds == $source) {
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return '';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Loads the DataSource class for the given connection name
|
|
|
|
*
|
2009-03-19 21:10:13 +00:00
|
|
|
* @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/.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return boolean True on success, null on failure or false if the class is already loaded
|
|
|
|
*/
|
2010-07-06 03:08:25 +00:00
|
|
|
public static function loadDataSource($connName) {
|
2010-11-09 03:07:34 +00:00
|
|
|
$_this = ConnectionManager::getInstance();
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (is_array($connName)) {
|
|
|
|
$conn = $connName;
|
|
|
|
} else {
|
2010-01-16 17:03:13 +00:00
|
|
|
$conn = $_this->_connectionsEnum[$connName];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (class_exists($conn['classname'])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-09-25 03:48:52 +00:00
|
|
|
if (!empty($conn['parent'])) {
|
|
|
|
$_this->loadDataSource($conn['parent']);
|
|
|
|
}
|
|
|
|
|
2009-09-25 02:56:02 +00:00
|
|
|
$conn = array_merge(array('plugin' => null, 'classname' => null, 'parent' => null), $conn);
|
|
|
|
$class = "{$conn['plugin']}.{$conn['classname']}";
|
|
|
|
|
2010-01-25 16:14:18 +00:00
|
|
|
if (!App::import('Datasource', $class, !is_null($conn['plugin']))) {
|
2010-04-15 16:00:25 +00:00
|
|
|
trigger_error(sprintf(__('ConnectionManager::loadDataSource - Unable to import DataSource class %s'), $class), E_USER_ERROR);
|
2008-05-30 11:40:08 +00:00
|
|
|
return null;
|
|
|
|
}
|
2009-09-24 20:23:24 +00:00
|
|
|
return true;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-16 17:03:13 +00:00
|
|
|
* Return a list of connections
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @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'.
|
|
|
|
*/
|
2010-07-06 03:08:25 +00:00
|
|
|
public static function enumConnectionObjects() {
|
2010-11-09 03:07:34 +00:00
|
|
|
$_this = ConnectionManager::getInstance();
|
2010-01-16 17:03:13 +00:00
|
|
|
return $_this->_connectionsEnum;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +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
|
|
|
|
*/
|
2010-07-06 03:08:25 +00:00
|
|
|
public static function &create($name = '', $config = array()) {
|
2010-11-09 03:07:34 +00:00
|
|
|
$_this = ConnectionManager::getInstance();
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (empty($name) || empty($config) || array_key_exists($name, $_this->_connectionsEnum)) {
|
|
|
|
$null = null;
|
|
|
|
return $null;
|
|
|
|
}
|
|
|
|
$_this->config->{$name} = $config;
|
2009-09-25 05:02:41 +00:00
|
|
|
$_this->_connectionsEnum[$name] = $_this->__connectionData($config);
|
2010-11-09 03:07:34 +00:00
|
|
|
$return = $_this->getDataSource($name);
|
2008-08-07 15:36:26 +00:00
|
|
|
return $return;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-01-16 17:03:13 +00:00
|
|
|
/**
|
|
|
|
* Gets a list of class and file names associated with the user-defined DataSource connections
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-07-06 03:08:25 +00:00
|
|
|
protected function _getConnectionObjects() {
|
2010-01-16 17:03:13 +00:00
|
|
|
$connections = get_object_vars($this->config);
|
|
|
|
|
|
|
|
if ($connections != null) {
|
|
|
|
foreach ($connections as $name => $config) {
|
|
|
|
$this->_connectionsEnum[$name] = $this->__connectionData($config);
|
|
|
|
}
|
|
|
|
} else {
|
2010-08-30 01:37:25 +00:00
|
|
|
throw new MissingConnectionException(array('class' => 'ConnectionManager'));
|
2010-01-16 17:03:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns the file, class name, and parent for the given driver.
|
|
|
|
*
|
2009-09-25 05:02:41 +00:00
|
|
|
* @return array An indexed array with: filename, classname, plugin and parent
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-07-06 03:08:25 +00:00
|
|
|
private function __connectionData($config) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!isset($config['datasource'])) {
|
|
|
|
$config['datasource'] = 'dbo';
|
|
|
|
}
|
2009-09-25 02:56:02 +00:00
|
|
|
$filename = $classname = $parent = $plugin = null;
|
|
|
|
|
2009-09-25 05:02:41 +00:00
|
|
|
if (!empty($config['driver'])) {
|
|
|
|
$parent = $this->__connectionData(array('datasource' => $config['datasource']));
|
2010-02-17 03:35:16 +00:00
|
|
|
$parentSource = preg_replace('/_source$/', '', $parent['filename']);
|
|
|
|
|
2010-02-17 03:42:06 +00:00
|
|
|
list($plugin, $classname) = pluginSplit($config['driver']);
|
|
|
|
if ($plugin) {
|
2010-02-17 03:35:16 +00:00
|
|
|
$source = Inflector::underscore($classname);
|
|
|
|
} else {
|
|
|
|
$source = $parentSource . '_' . $config['driver'];
|
|
|
|
$classname = Inflector::camelize(strtolower($source));
|
|
|
|
}
|
|
|
|
$filename = $parentSource . DS . $source;
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2010-02-17 03:42:06 +00:00
|
|
|
list($plugin, $classname) = pluginSplit($config['datasource']);
|
|
|
|
if ($plugin) {
|
2009-09-25 02:56:02 +00:00
|
|
|
$filename = Inflector::underscore($classname);
|
|
|
|
} else {
|
2010-06-21 02:53:54 +00:00
|
|
|
$filename = Inflector::underscore($config['datasource']);
|
2009-09-25 02:56:02 +00:00
|
|
|
}
|
2010-06-21 02:53:54 +00:00
|
|
|
if (substr($filename, -7) != '_source') {
|
|
|
|
$filename .= '_source';
|
|
|
|
}
|
|
|
|
$classname = Inflector::camelize(strtolower($filename));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-09-25 05:02:41 +00:00
|
|
|
return compact('filename', 'classname', 'parent', 'plugin');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function __destruct() {
|
2010-09-06 05:10:45 +00:00
|
|
|
if (Configure::read('Session.defaults') == 'database' && function_exists('session_write_close')) {
|
2008-05-30 11:40:08 +00:00
|
|
|
session_write_close();
|
|
|
|
}
|
|
|
|
}
|
2010-08-28 04:08:35 +00:00
|
|
|
}
|