Skip to content
Permalink
Browse files

Refactoring ConnectionManager & tests.

  • Loading branch information...
jperras committed Sep 25, 2009
1 parent 7e5fe04 commit c2ec96067a027661e1ea8fdd68d3853f9469de65
Showing with 20 additions and 29 deletions.
  1. +11 −29 cake/libs/model/connection_manager.php
  2. +9 −0 cake/tests/cases/libs/model/connection_manager.test.php
@@ -25,8 +25,8 @@
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
uses ('model' . DS . 'datasources' . DS . 'datasource');
config('database');
require LIBS . 'model' . DS . 'datasources' . DS . 'datasource.php';
include_once CONFIGS . 'database.php';
/**
* Manages loaded instances of DataSource objects
@@ -104,31 +104,21 @@ function &getDataSource($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);
$class = $conn['classname'];
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;
@@ -152,19 +142,14 @@ function sourceList() {
* Gets a DataSource name from an object reference
*
* @param object $source DataSource object
* @return string Datasource name
* @return string Datasource name, or null if source is not present
* in the ConnectionManager.
* @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;
return array_search($source, $_this->_dataSources);
}
/**
@@ -187,23 +172,22 @@ function loadDataSource($connName) {
$conn = $connections[$connName];
}
if (!empty($conn['parent'])) {
$_this->loadDataSource($conn['parent']);
}
if (class_exists($conn['classname'])) {
return false;
}
if (!empty($conn['parent'])) {
$_this->loadDataSource($conn['parent']);
}
$conn = array_merge(array('plugin' => null, 'classname' => null, 'parent' => null), $conn);
$class = "{$conn['plugin']}.{$conn['classname']}";
if (!App::import('Datasource', $class)) {
if (!App::import('Datasource', $class, false)) {
$error = __('ConnectionManager::loadDataSource - Unable to import DataSource class %s', true);
trigger_error(sprintf($error, $class), E_USER_ERROR);
return null;
}
return true;
}
@@ -249,7 +233,6 @@ function &create($name = '', $config = array()) {
$null = null;
return $null;
}
$_this->config->{$name} = $config;
$_this->_connectionsEnum[$name] = $_this->__getDriver($config);
$return =& $_this->getDataSource($name);
@@ -282,7 +265,6 @@ function __getDriver($config) {
$classname = Inflector::camelize(strtolower($config['datasource'] . '_source'));
}
}
$driver = compact('filename', 'classname', 'parent', 'plugin');
return $driver;
}
@@ -86,6 +86,11 @@ function testGetDataSource() {
$source = ConnectionManager::getDataSource(key($connections));
$this->assertTrue(is_object($source));
$this->expectError(new PatternExpectation('/Non-existent data source/i'));
$source = ConnectionManager::getDataSource('non_existent_source');
$this->assertEqual($source, null);
}
/**
@@ -137,6 +142,10 @@ function testGetSourceName() {
$result = ConnectionManager::getSourceName($source);
$this->assertEqual($result, $name);
$source =& new StdClass();
$result = ConnectionManager::getSourceName($source);
$this->assertEqual($result, null);
}
/**

0 comments on commit c2ec960

Please sign in to comment.
You can’t perform that action at this time.