diff --git a/lib/Cake/Model/ConnectionManager.php b/lib/Cake/Model/ConnectionManager.php index 0f0ac3435..914e8911a 100644 --- a/lib/Cake/Model/ConnectionManager.php +++ b/lib/Cake/Model/ConnectionManager.php @@ -201,8 +201,7 @@ class ConnectionManager { } if (empty($name) || empty($config) || array_key_exists($name, self::$_connectionsEnum)) { - $null = null; - return $null; + return null; } self::$config->{$name} = $config; self::$_connectionsEnum[$name] = self::_connectionData($config); @@ -210,6 +209,24 @@ class ConnectionManager { return $return; } +/** + * Removes a connection configuration at runtime given its name + * + * @param string $name the connection name as it was created + * @return boolean success if connection was removed, false if it does not exist + */ + public static function drop($name) { + if (empty(self::$_init)) { + self::init(); + } + + if (!isset(self::$config->{$name})) { + return false; + } + unset(self::$_connectionsEnum[$name], self::$_dataSources[$name], self::$config->{$name}); + return true; + } + /** * Gets a list of class and file names associated with the user-defined DataSource connections * diff --git a/lib/Cake/tests/Case/Model/ConnectionManagerTest.php b/lib/Cake/tests/Case/Model/ConnectionManagerTest.php index 2256a3696..5b0291520 100644 --- a/lib/Cake/tests/Case/Model/ConnectionManagerTest.php +++ b/lib/Cake/tests/Case/Model/ConnectionManagerTest.php @@ -310,4 +310,24 @@ class ConnectionManagerTest extends CakeTestCase { $connections = ConnectionManager::enumConnectionObjects(); $this->assertEqual($expected, $connections['connection8']); } + +/** + * Tests that a connection configuration can be deleted in runtime + * + * @return void + */ + public function testDrop() { + App::build(array( + 'Model/Datasource' => array( + LIBS . 'tests' . DS . 'test_app' . DS . 'Model' . DS . 'Datasource' . DS + ) + )); + ConnectionManager::create('droppable', array('datasource' => 'Test2Source')); + $connections = ConnectionManager::enumConnectionObjects(); + $this->assertEqual(array('datasource' => 'Test2Source'), $connections['droppable']); + + $this->assertTrue(ConnectionManager::drop('droppable')); + $connections = ConnectionManager::enumConnectionObjects(); + $this->assertFalse(isset($connections['droppable'])); + } }