Skip to content
Permalink
Browse files

Update ConnectionManager to use StaticConfigTrait.

This removes support for the datasource key which isn't necessarily
a bad thing. Update drop() to be conformant with other implementations
in CakePHP.
  • Loading branch information...
markstory committed Aug 26, 2013
1 parent 2bed467 commit 6d006888c25f7093b7fbab014914b4466852f4d0
@@ -18,6 +18,7 @@
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\StaticConfigTrait;
use Cake\Database\Connection;
use Cake\Database\ConnectionRegistry;
use Cake\Error;
@@ -33,12 +34,16 @@
*/
class ConnectionManager {
use StaticConfigTrait {
config as protected _config;
}
/**
* Holds a list of connection configurations
*
* @var array
*/
protected static $_config = null;
protected static $_config = [];
/**
* The ConnectionRegistry used by the manager.
@@ -52,57 +57,18 @@ class ConnectionManager {
*
* The connection will not be constructed until it is first used.
*
* To change an adapter's configuration at runtime, first drop the adapter and then
* reconfigure it.
*
* Adapters will not be constructed until the first operation is done.
*
* ### Usage
*
* Reading config data back:
*
* `ConnectionManager::config('default');`
*
* Setting a connection up.
*
* `ConnectionManager::config('default', $settings);`
*
* Injecting a constructed driver in:
*
* `ConnectionManager::config('default', $instance);`
*
* Configure multiple adapters at once:
*
* `ConnectionManager::config($arrayOfConfig);`
* @see Cake\Core\StaticConfigTrait::config()
*
* @param string|array $key The name of the connection config, or an array of multiple configs.
* @param array $config An array of name => config data for adapter.
* @return mixed null when adding configuration and an array of configuration data when reading.
* @throws Cake\Error\Exception When trying to modify an existing config.
*/
public static function config($key, $config = null) {
// Read config.
if ($config === null && is_string($key)) {
return isset(static::$_config[$key]) ? static::$_config[$key] : null;
}
if ($config === null && is_array($key)) {
foreach ($key as $name => $settings) {
static::config($name, $settings);
}
return;
}
if (isset(static::$_config[$key])) {
throw new Error\Exception(__d('cake_dev', 'Cannot reconfigure existing adapter "%s"', $key));
if (is_array($config)) {
$config['name'] = $key;
}
if (is_object($config)) {
$config = ['className' => $config];
}
if (isset($config['datasource']) && empty($config['className'])) {
$config['className'] = $config['datasource'];
unset($config['datasource']);
}
$config['name'] = $key;
static::$_config[$key] = $config;
return static::_config($key, $config);
}
/**
@@ -128,29 +94,6 @@ public static function get($name) {
return static::$_registry->load($name, static::$_config[$name]);
}
/**
* Get the names of configured connections.
*
* @return array An array of connection names.
*/
public static function configured() {
return array_keys(static::$_config);
}
/**
* Drop a connection and its configuration.
*
* @param string $name The connection name to remove.
* @return boolean true
*/
public static function drop($name) {
if (isset(static::$_registry)) {
static::$_registry->unload($name);
}
unset(static::$_config[$name]);
return true;
}
/**
* Gets a reference to a DataSource object
*
@@ -42,10 +42,6 @@ public function tearDown() {
*/
public static function configProvider() {
return [
'Array of data using datasource key.' => [[
'datasource' => 'Cake\Database\Driver\Sqlite',
'database' => ':memory:',
]],
'Array of data using classname key.' => [[
'className' => 'Sqlite',
'database' => ':memory:',
@@ -85,7 +81,7 @@ public function testConfigInvalidOptions() {
* Test for errors on duplicate config.
*
* @expectedException Cake\Error\Exception
* @expectedExceptionMessage Cannot reconfigure existing adapter "test_variant"
* @expectedExceptionMessage Cannot reconfigure existing key "test_variant"
* @return void
*/
public function testConfigDuplicateConfig() {
@@ -173,7 +169,7 @@ public function testDrop() {
$result = ConnectionManager::configured();
$this->assertNotContains('test_variant', $result);
$this->assertTrue(ConnectionManager::drop('probably_does_not_exist'), 'Should always return true.');
$this->assertFalse(ConnectionManager::drop('probably_does_not_exist'), 'Should return false on failure.');
}
}
@@ -35,7 +35,7 @@ class QueryTest extends TestCase {
public function setUp() {
parent::setUp();
$this->connection = ConnectionManager::getDataSource('test');
$this->connection = ConnectionManager::get('test');
}
public function tearDown() {

0 comments on commit 6d00688

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