Making ConnectionManager an static object instead of a singleton

This commit is contained in:
José Lorenzo Rodríguez 2010-12-06 22:47:43 -04:30
parent 2ce2ea222c
commit 66744c68b0

View file

@ -36,7 +36,7 @@ class ConnectionManager {
* @var DATABASE_CONFIG * @var DATABASE_CONFIG
* @access public * @access public
*/ */
public $config = null; public static $config = null;
/** /**
* Holds instances DataSource objects * Holds instances DataSource objects
@ -44,7 +44,7 @@ class ConnectionManager {
* @var array * @var array
* @access protected * @access protected
*/ */
protected $_dataSources = array(); protected static $_dataSources = array();
/** /**
* Contains a list of all file and class names used in Connection settings * Contains a list of all file and class names used in Connection settings
@ -52,33 +52,27 @@ class ConnectionManager {
* @var array * @var array
* @access protected * @access protected
*/ */
protected $_connectionsEnum = array(); protected static $_connectionsEnum = array();
/** /**
* Constructor. * Indicates if the init code for this class has alredy been executed
*
* @var boolean
*/
private static $_init = false;
/**
* Loads connections configuration.
* *
*/ */
function __construct() { private static function init() {
include_once CONFIGS . 'database.php'; include_once CONFIGS . 'database.php';
if (class_exists('DATABASE_CONFIG')) { if (class_exists('DATABASE_CONFIG')) {
$this->config = new DATABASE_CONFIG(); self::$config = new DATABASE_CONFIG();
$this->_getConnectionObjects(); self::_getConnectionObjects();
} }
} register_shutdown_function('ConnectionManager::shutdown');
self::$_init = true;
/**
* Gets a reference to the ConnectionManger object instance
*
* @return object Instance
*/
public static function &getInstance() {
static $instance = array();
if (!$instance) {
$instance[0] = new ConnectionManager();
}
return $instance[0];
} }
/** /**
@ -87,32 +81,33 @@ class ConnectionManager {
* @param string $name The name of the DataSource, as defined in app/config/database.php * @param string $name The name of the DataSource, as defined in app/config/database.php
* @return object Instance * @return object Instance
*/ */
public static function &getDataSource($name) { public static function getDataSource($name) {
$_this = ConnectionManager::getInstance(); if (empty(self::$_init)) {
self::init();
}
if (!empty($_this->_dataSources[$name])) { if (!empty(self::$_dataSources[$name])) {
$return = $_this->_dataSources[$name]; $return = self::$_dataSources[$name];
return $return; return $return;
} }
if (empty($_this->_connectionsEnum[$name])) { if (empty(self::$_connectionsEnum[$name])) {
trigger_error(__("ConnectionManager::getDataSource - Non-existent data source %s", $name), E_USER_ERROR); trigger_error(__("ConnectionManager::getDataSource - Non-existent data source %s", $name), E_USER_ERROR);
$null = null; $null = null;
return $null; return $null;
} }
$conn = $_this->_connectionsEnum[$name]; $conn = self::$_connectionsEnum[$name];
$class = $conn['classname']; $class = $conn['classname'];
if ($_this->loadDataSource($name) === null) { if (self::loadDataSource($name) === null) {
trigger_error(__("ConnectionManager::getDataSource - Could not load class %s", $class), E_USER_ERROR); trigger_error(__("ConnectionManager::getDataSource - Could not load class %s", $class), E_USER_ERROR);
$null = null; $null = null;
return $null; return $null;
} }
$_this->_dataSources[$name] = new $class($_this->config->{$name}); self::$_dataSources[$name] = new $class(self::$config->{$name});
$_this->_dataSources[$name]->configKeyName = $name; self::$_dataSources[$name]->configKeyName = $name;
$return = $_this->_dataSources[$name]; return self::$_dataSources[$name];
return $return;
} }
/** /**
@ -121,8 +116,10 @@ class ConnectionManager {
* @return array List of available connections * @return array List of available connections
*/ */
public static function sourceList() { public static function sourceList() {
$_this = ConnectionManager::getInstance(); if (empty(self::$_init)) {
return array_keys($_this->_dataSources); self::init();
}
return array_keys(self::$_dataSources);
} }
/** /**
@ -135,8 +132,10 @@ class ConnectionManager {
* in the ConnectionManager. * in the ConnectionManager.
*/ */
public static function getSourceName(&$source) { public static function getSourceName(&$source) {
$_this = ConnectionManager::getInstance(); if (empty(self::$_init)) {
foreach ($_this->_dataSources as $name => $ds) { self::init();
}
foreach (self::$_dataSources as $name => $ds) {
if ($ds == $source) { if ($ds == $source) {
return $name; return $name;
} }
@ -153,12 +152,14 @@ class ConnectionManager {
* @return boolean True on success, null on failure or false if the class is already loaded * @return boolean True on success, null on failure or false if the class is already loaded
*/ */
public static function loadDataSource($connName) { public static function loadDataSource($connName) {
$_this = ConnectionManager::getInstance(); if (empty(self::$_init)) {
self::init();
}
if (is_array($connName)) { if (is_array($connName)) {
$conn = $connName; $conn = $connName;
} else { } else {
$conn = $_this->_connectionsEnum[$connName]; $conn = self::$_connectionsEnum[$connName];
} }
if (class_exists($conn['classname'], false)) { if (class_exists($conn['classname'], false)) {
@ -188,8 +189,10 @@ class ConnectionManager {
* (as defined in Connections), and the value is an array with keys 'filename' and 'classname'. * (as defined in Connections), and the value is an array with keys 'filename' and 'classname'.
*/ */
public static function enumConnectionObjects() { public static function enumConnectionObjects() {
$_this = ConnectionManager::getInstance(); if (empty(self::$_init)) {
return $_this->_connectionsEnum; self::init();
}
return self::$_connectionsEnum;
} }
/** /**
@ -199,16 +202,18 @@ class ConnectionManager {
* @param array $config The DataSource configuration settings * @param array $config The DataSource configuration settings
* @return object A reference to the DataSource object, or null if creation failed * @return object A reference to the DataSource object, or null if creation failed
*/ */
public static function &create($name = '', $config = array()) { public static function create($name = '', $config = array()) {
$_this = ConnectionManager::getInstance(); if (empty(self::$_init)) {
self::init();
}
if (empty($name) || empty($config) || array_key_exists($name, $_this->_connectionsEnum)) { if (empty($name) || empty($config) || array_key_exists($name, self::$_connectionsEnum)) {
$null = null; $null = null;
return $null; return $null;
} }
$_this->config->{$name} = $config; self::$config->{$name} = $config;
$_this->_connectionsEnum[$name] = $_this->__connectionData($config); self::$_connectionsEnum[$name] = self::_connectionData($config);
$return = $_this->getDataSource($name); $return = self::getDataSource($name);
return $return; return $return;
} }
@ -217,12 +222,12 @@ class ConnectionManager {
* *
* @return void * @return void
*/ */
protected function _getConnectionObjects() { protected static function _getConnectionObjects() {
$connections = get_object_vars($this->config); $connections = get_object_vars(self::$config);
if ($connections != null) { if ($connections != null) {
foreach ($connections as $name => $config) { foreach ($connections as $name => $config) {
$this->_connectionsEnum[$name] = $this->__connectionData($config); self::$_connectionsEnum[$name] = self::_connectionData($config);
} }
} else { } else {
throw new MissingConnectionException(array('class' => 'ConnectionManager')); throw new MissingConnectionException(array('class' => 'ConnectionManager'));
@ -234,7 +239,7 @@ class ConnectionManager {
* *
* @return array An indexed array with: filename, classname, plugin and parent * @return array An indexed array with: filename, classname, plugin and parent
*/ */
private function __connectionData($config) { private static function _connectionData($config) {
$package = $classname = $plugin = null; $package = $classname = $plugin = null;
list($plugin, $classname) = pluginSplit($config['datasource']); list($plugin, $classname) = pluginSplit($config['datasource']);
@ -249,7 +254,7 @@ class ConnectionManager {
* Destructor. * Destructor.
* *
*/ */
function __destruct() { public static function shutdown() {
if (Configure::read('Session.defaults') == 'database' && function_exists('session_write_close')) { if (Configure::read('Session.defaults') == 'database' && function_exists('session_write_close')) {
session_write_close(); session_write_close();
} }