Skip to content

Commit

Permalink
Implementing loading datasources from plugins.
Browse files Browse the repository at this point in the history
  • Loading branch information
jperras committed Sep 25, 2009
1 parent 12c5ebf commit c3cd98a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 19 deletions.
57 changes: 39 additions & 18 deletions cake/libs/model/connection_manager.php
Expand Up @@ -106,18 +106,32 @@ function &getDataSource($name) {
}

$connections = $_this->enumConnectionObjects();
if (!empty($connections[$name])) {
$conn = $connections[$name];
$class = $conn['classname'];
$_this->loadDataSource($name);
$_this->_dataSources[$name] =& new $class($_this->config->{$name});
$_this->_dataSources[$name]->configKeyName = $name;
} else {

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;
}
Expand Down Expand Up @@ -181,13 +195,12 @@ function loadDataSource($connName) {
return false;
}

if (file_exists(MODELS . 'datasources' . DS . $conn['filename'] . '.php')) {
require (MODELS . 'datasources' . DS . $conn['filename'] . '.php');
} elseif (fileExistsInPath(LIBS . 'model' . DS . 'datasources' . DS . $conn['filename'] . '.php')) {
require (LIBS . 'model' . DS . 'datasources' . DS . $conn['filename'] . '.php');
} else {
$error = __('Unable to load DataSource file %s.php', true);
trigger_error(sprintf($error, $conn['filename']), E_USER_ERROR);
$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;
}

Expand Down Expand Up @@ -254,16 +267,24 @@ function __getDriver($config) {
$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 {
$filename = $config['datasource'] . '_source';
$classname = Inflector::camelize(strtolower($config['datasource'] . '_source'));
$parent = null;
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'));
}
}
return array('filename' => $filename, 'classname' => $classname, 'parent' => $parent);

$driver = compact('filename', 'classname', 'parent', 'plugin');
return $driver;
}

/**
Expand Down
24 changes: 23 additions & 1 deletion cake/tests/cases/libs/model/connection_manager.test.php
Expand Up @@ -88,6 +88,28 @@ function testGetDataSource() {

}

/**
* testGetPluginDataSource method
*
* @access public
* @return void
*/
function testGetPluginDataSource() {
App::build(array(
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
));

$name = 'test_source';
$config = array('datasource' => 'TestPlugin.TestSource');
$connection = ConnectionManager::create($name, $config);

$this->assertTrue(class_exists('TestSource'));
$this->assertEqual($connection->configKeyName, $name);
$this->assertEqual($connection->config, $config);

App::build();
}

/**
* testSourceList method
*
Expand Down Expand Up @@ -138,7 +160,7 @@ function testLoadDataSource() {
}

$connection = array('classname' => 'NonExistentDataSource', 'filename' => 'non_existent');
$this->expectError(new PatternExpectation('/Unable to load DataSource file/i'));
$this->expectError(new PatternExpectation('/Unable to import DataSource class/i'));

$loaded = ConnectionManager::loadDataSource($connection);
$this->assertEqual($loaded, null);
Expand Down

0 comments on commit c3cd98a

Please sign in to comment.