mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Implementing loading datasources from plugins.
This commit is contained in:
parent
12c5ebf5ba
commit
c3cd98af42
2 changed files with 62 additions and 19 deletions
|
@ -106,18 +106,32 @@ class ConnectionManager extends Object {
|
||||||
}
|
}
|
||||||
|
|
||||||
$connections = $_this->enumConnectionObjects();
|
$connections = $_this->enumConnectionObjects();
|
||||||
if (!empty($connections[$name])) {
|
|
||||||
$conn = $connections[$name];
|
if (empty($connections[$name])) {
|
||||||
$class = $conn['classname'];
|
|
||||||
$_this->loadDataSource($name);
|
|
||||||
$_this->_dataSources[$name] =& new $class($_this->config->{$name});
|
|
||||||
$_this->_dataSources[$name]->configKeyName = $name;
|
|
||||||
} else {
|
|
||||||
trigger_error(sprintf(__("ConnectionManager::getDataSource - Non-existent data source %s", true), $name), E_USER_ERROR);
|
trigger_error(sprintf(__("ConnectionManager::getDataSource - Non-existent data source %s", true), $name), E_USER_ERROR);
|
||||||
$null = null;
|
$null = null;
|
||||||
return $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 =& $_this->_dataSources[$name];
|
||||||
return $return;
|
return $return;
|
||||||
}
|
}
|
||||||
|
@ -181,13 +195,12 @@ class ConnectionManager extends Object {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (file_exists(MODELS . 'datasources' . DS . $conn['filename'] . '.php')) {
|
$conn = array_merge(array('plugin' => null, 'classname' => null, 'parent' => null), $conn);
|
||||||
require (MODELS . 'datasources' . DS . $conn['filename'] . '.php');
|
$class = "{$conn['plugin']}.{$conn['classname']}";
|
||||||
} elseif (fileExistsInPath(LIBS . 'model' . DS . 'datasources' . DS . $conn['filename'] . '.php')) {
|
|
||||||
require (LIBS . 'model' . DS . 'datasources' . DS . $conn['filename'] . '.php');
|
if (!App::import('Datasource', $class)) {
|
||||||
} else {
|
$error = __('ConnectionManager::loadDataSource - Unable to import DataSource class %s', true);
|
||||||
$error = __('Unable to load DataSource file %s.php', true);
|
trigger_error(sprintf($error, $class), E_USER_ERROR);
|
||||||
trigger_error(sprintf($error, $conn['filename']), E_USER_ERROR);
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -254,16 +267,24 @@ class ConnectionManager extends Object {
|
||||||
$config['datasource'] = 'dbo';
|
$config['datasource'] = 'dbo';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$filename = $classname = $parent = $plugin = null;
|
||||||
|
|
||||||
if (isset($config['driver']) && $config['driver'] != null && !empty($config['driver'])) {
|
if (isset($config['driver']) && $config['driver'] != null && !empty($config['driver'])) {
|
||||||
$filename = $config['datasource'] . DS . $config['datasource'] . '_' . $config['driver'];
|
$filename = $config['datasource'] . DS . $config['datasource'] . '_' . $config['driver'];
|
||||||
$classname = Inflector::camelize(strtolower($config['datasource'] . '_' . $config['driver']));
|
$classname = Inflector::camelize(strtolower($config['datasource'] . '_' . $config['driver']));
|
||||||
$parent = $this->__getDriver(array('datasource' => $config['datasource']));
|
$parent = $this->__getDriver(array('datasource' => $config['datasource']));
|
||||||
|
} else {
|
||||||
|
if (strpos($config['datasource'], '.') !== false) {
|
||||||
|
list($plugin, $classname) = explode('.', $config['datasource']);
|
||||||
|
$filename = Inflector::underscore($classname);
|
||||||
} else {
|
} else {
|
||||||
$filename = $config['datasource'] . '_source';
|
$filename = $config['datasource'] . '_source';
|
||||||
$classname = Inflector::camelize(strtolower($config['datasource'] . '_source'));
|
$classname = Inflector::camelize(strtolower($config['datasource'] . '_source'));
|
||||||
$parent = null;
|
|
||||||
}
|
}
|
||||||
return array('filename' => $filename, 'classname' => $classname, 'parent' => $parent);
|
}
|
||||||
|
|
||||||
|
$driver = compact('filename', 'classname', 'parent', 'plugin');
|
||||||
|
return $driver;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -88,6 +88,28 @@ class ConnectionManagerTest extends CakeTestCase {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
* testSourceList method
|
||||||
*
|
*
|
||||||
|
@ -138,7 +160,7 @@ class ConnectionManagerTest extends CakeTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
$connection = array('classname' => 'NonExistentDataSource', 'filename' => 'non_existent');
|
$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);
|
$loaded = ConnectionManager::loadDataSource($connection);
|
||||||
$this->assertEqual($loaded, null);
|
$this->assertEqual($loaded, null);
|
||||||
|
|
Loading…
Reference in a new issue