Skip to content
Browse files

add ConnectionManager back into the mix

  • Loading branch information...
1 parent 1b2ca0c commit 4856300201a6a98f66e5ad31fe07bbe4dcde79f0 @pmjones pmjones committed Feb 11, 2012
View
83 hold/ConnectionFactory.php
@@ -1,83 +0,0 @@
-<?php
-/**
- *
- * This file is part of the Aura Project for PHP.
- *
- * @license http://opensource.org/licenses/bsd-license.php BSD
- *
- */
-namespace Aura\Sql;
-use Aura\Di\ForgeInterface;
-
-/**
- *
- * Adapter Factory
- *
- * @package Aura.Sql
- *
- */
-class AdapterFactory
-{
- /**
- *
- * A Forge to create objects.
- *
- * @var Aura\Di\ForgeInterface
- *
- */
- protected $forge;
-
- /**
- *
- * A map of names (called at the command line) to their corresponding
- * Adapter classes.
- *
- * @var array
- *
- */
- protected $map = [];
-
- /**
- *
- * A Adapter class to use when no class exists for a mapped name.
- *
- * @param ForgeInterface $forge A Forge to create objects.
- *
- * @param array $map A map of PDO types to Adapter classes.
- *
- * @param string $not_found A Adapter class to use when no class
- * can be found for a mapped name.
- *
- */
- public function __construct(
- ForgeInterface $forge,
- array $map = []
- ) {
- $this->forge = $forge;
- $this->map = $map;
- }
-
- /**
- *
- * Creates and returns a Adapter class based on a PDO type.
- *
- * @param string $name A PDO type that maps to a Adapter class.
- *
- * @return Adapter
- *
- * @throws Exception\AdapterFactory when no mapped class can be found.
- *
- */
- public function newInstance(
- $name,
- array $params = []
- ) {
- if (isset($this->map[$name])) {
- $class = $this->map[$name];
- } else {
- throw new Exception\AdapterFactory("No Adapter class mapping found for '$name'.");
- }
-
- return $this->forge->newInstance($class, $params);
- }
-}
View
72 hold/ConnectionFactoryTest.php
@@ -1,72 +0,0 @@
-<?php
-namespace Aura\Sql;
-
-use Aura\Di\Forge;
-use Aura\Di\Config;
-
-/**
- * Test class for AdapterFactory.
- * Generated by PHPUnit on 2011-07-11 at 09:43:40.
- */
-class AdapterFactoryTest extends \PHPUnit_Framework_TestCase
-{
- /**
- * @var AdapterFactory
- */
- protected $factory;
-
- /**
- * Sets up the fixture, for example, opens a network connection.
- * This method is called before a test is executed.
- */
- protected function setUp()
- {
- parent::setUp();
-
- $forge = new Forge(new Config);
-
- $map = [
- 'mock' => 'Aura\Sql\Adapter\MockAdapter',
- ];
-
- $this->factory = new AdapterFactory($forge, $map);
- }
-
- /**
- * Tears down the fixture, for example, closes a network connection.
- * This method is called after a test is executed.
- */
- protected function tearDown()
- {
- parent::tearDown();
- }
-
- /**
- * @todo Implement testNewInstance().
- */
- public function testNewInstance()
- {
- $params = [
- 'dsn' => [],
- 'username' => null,
- 'password' => null,
- 'options' => [],
- ];
- $conn = $this->factory->newInstance('mock', $params);
- $this->assertInstanceOf('Aura\Sql\Adapter\MockAdapter', $conn);
- }
-
- /**
- * @expectedException Aura\Sql\Exception\AdapterFactory
- */
- public function testNoMapping()
- {
- $params = [
- 'dsn' => [],
- 'username' => null,
- 'password' => null,
- 'options' => [],
- ];
- $conn = $this->factory->newInstance('no_such_mapping', $params);
- }
-}
View
18 hold/Exception_ConnectionFactory.php
@@ -1,18 +0,0 @@
-<?php
-/**
- *
- * This file is part of the Aura Project for PHP.
- *
- * @license http://opensource.org/licenses/bsd-license.php BSD
- *
- */
-namespace Aura\Sql\Exception;
-
-/**
- *
- * Adapter Factory Exception
- *
- * @package Aura.Sql
- *
- */
-class AdapterFactory extends \Aura\Sql\Exception {}
View
72 hold/ConnectionManager.php → src/Aura/Sql/ConnectionManager.php
@@ -10,12 +10,15 @@
/**
*
- * Adapter Manager
+ * Manages connections to master and slave databases.
*
* @package Aura.Sql
*
+ * @todo add setDefault(), setSlave(), and setMaster() methods for
+ * programmatic setup.
+ *
*/
-class AdapterManager
+class ConnectionManager
{
protected $default = [
'adapter' => null,
@@ -29,7 +32,7 @@ class AdapterManager
protected $slaves = [];
- protected $factory;
+ protected $adapter_factory;
protected $conn = [
'default' => null,
@@ -38,15 +41,15 @@ class AdapterManager
];
public function __construct(
- AdapterFactory $factory,
+ AdapterFactory $adapter_factory,
array $default = [],
array $masters = [],
- array $slaves = []
+ array $slaves = []
) {
- $this->factory = $factory;
- $this->default = $default;
- $this->masters = $masters;
- $this->slaves = $slaves;
+ $this->adapter_factory = $adapter_factory;
+ $this->default = array_merge($this->default, $default);
+ $this->masters = (array) $masters;
+ $this->slaves = (array) $slaves;
}
// pick a random slave, or a random master if no slaves, or default if no masters
@@ -74,9 +77,15 @@ public function getWrite()
// converts $this->default to a Adapter object and returns it
public function getDefault()
{
- if (! $this->conn['default'] instanceof Adapter) {
- list($adapter, $params) = $this->mergeAdapterParams();
- $this->conn['default'] = $this->factory->newInstance($adapter, $params);
+ if (! $this->conn['default'] instanceof AbstractAdapter) {
+ $params = $this->mergeParams();
+ $this->conn['default'] = $this->adapter_factory->newInstance(
+ $params['adapter'],
+ $params['dsn'],
+ $params['username'],
+ $params['password'],
+ $params['options']
+ );
}
return $this->conn['default'];
}
@@ -90,18 +99,24 @@ public function getMaster($key = null)
throw new Exception\NoSuchMaster($key);
}
- $is_conn = ! empty($this->conn['masters'][$key])
- && $this->conn['masters'][$key] instanceof Adapter;
+ $is_conn = isset($this->conn['masters'][$key])
+ && $this->conn['masters'][$key] instanceof AbstractAdapter;
if (! $is_conn) {
- list($adapter, $params) = $this->mergeAdapterParams($this->masters[$key]);
- $this->conn['masters'][$key] = $this->factory->newInstance($adapter, $params);
+ $params = $this->mergeParams($this->masters[$key]);
+ $this->conn['masters'][$key] = $this->adapter_factory->newInstance(
+ $params['adapter'],
+ $params['dsn'],
+ $params['username'],
+ $params['password'],
+ $params['options']
+ );
}
return $this->conn['masters'][$key];
}
- // converts a random $this->masters entry to a Adapter object
+ // converts a random $this->slave entry to a Adapter object
public function getSlave($key = null)
{
if (! $key) {
@@ -110,28 +125,33 @@ public function getSlave($key = null)
throw new Exception\NoSuchSlave($key);
}
- $is_conn = ! empty($this->conn['slaves'][$key])
- && $this->conn['slaves'][$key] instanceof Adapter;
+ $is_conn = isset($this->conn['slaves'][$key])
+ && $this->conn['slaves'][$key] instanceof AbstractAdapter;
if (! $is_conn) {
- list($adapter, $params) = $this->mergeAdapterParams($this->slaves[$key]);
- $this->conn['slaves'][$key] = $this->factory->newInstance($adapter, $params);
+ $params = $this->mergeParams($this->slaves[$key]);
+ $this->conn['slaves'][$key] = $this->adapter_factory->newInstance(
+ $params['adapter'],
+ $params['dsn'],
+ $params['username'],
+ $params['password'],
+ $params['options']
+ );
}
return $this->conn['slaves'][$key];
}
// merges $this->default with master or slave override values
- protected function mergeAdapterParams(array $override = [])
+ protected function mergeParams(array $override = [])
{
- $merged = $this->merge($this->default, $override);
- $adapter = $merged['adapter'];
- $params = [
+ $merged = $this->merge($this->default, $override);
+ return [
+ 'adapter' => $merged['adapter'],
'dsn' => $merged['dsn'],
'username' => $merged['username'],
'password' => $merged['password'],
'options' => $merged['options'],
];
- return [$adapter, $params];
}
protected function merge($baseline, $override)
View
0 hold/Exception.php → src/Aura/Sql/Exception.php
File renamed without changes.
View
0 hold/Exception_NoSuchMaster.php → src/Aura/Sql/Exception/NoSuchMaster.php
File renamed without changes.
View
0 hold/Exception_NoSuchSlave.php → src/Aura/Sql/Exception/NoSuchSlave.php
File renamed without changes.
View
26 hold/MockConnection.php → tests/Aura/Sql/Adapter/Mock.php
@@ -1,15 +1,19 @@
<?php
namespace Aura\Sql\Adapter;
-class MockAdapter
+use Aura\Sql\ProfilerInterface;
+use Aura\Sql\ColumnFactory;
+
+class Mock extends AbstractAdapter
{
protected $params = [];
- // skip the signal manager, otherwise mimic the AbstractAdapter params
public function __construct(
- array $dsn,
- $username,
- $password,
- array $options
+ ProfilerInterface $profiler,
+ ColumnFactory $column_factory,
+ $dsn,
+ $username = null,
+ $password = null,
+ array $options = []
) {
$this->params = [
'dsn' => $dsn,
@@ -28,4 +32,14 @@ public function getDsnHost()
{
return $this->params['dsn']['host'];
}
+
+ public function fetchTableList($schema = null)
+ {
+ return [];
+ }
+
+ public function fetchTableCols($spec)
+ {
+ return [];
+ }
}
View
16 hold/ConnectionManagerTest.php → tests/Aura/Sql/ConnectionManagerTest.php
@@ -1,16 +1,13 @@
<?php
namespace Aura\Sql;
-use Aura\Di\Forge;
-use Aura\Di\Config;
/**
- * Test class for AdapterManager.
* Generated by PHPUnit on 2011-07-11 at 09:43:46.
*/
-class AdapterManagerTest extends \PHPUnit_Framework_TestCase
+class ConnectionManagerTest extends \PHPUnit_Framework_TestCase
{
/**
- * @var AdapterManager
+ * @var ConnectionManager
*/
protected $manager;
@@ -58,12 +55,11 @@ protected function newManager(
array $masters = [],
array $slaves = []
) {
- $forge = new Forge(new Config);
$map = [
- 'mock' => 'Aura\Sql\Adapter\MockAdapter',
+ 'mock' => 'Aura\Sql\Adapter\Mock',
];
- $factory = new AdapterFactory($forge, $map);
- return new AdapterManager($factory, $default, $masters, $slaves);
+ $factory = new AdapterFactory($map);
+ return new ConnectionManager($factory, $default, $masters, $slaves);
}
/**
@@ -203,7 +199,7 @@ public function testGetDefault()
{
$manager = $this->newManager($this->default);
$conn = $manager->getDefault();
- $this->assertInstanceOf('Aura\Sql\Adapter\MockAdapter', $conn);
+ $this->assertInstanceOf('Aura\Sql\Adapter\Mock', $conn);
$expect = $this->default;
unset($expect['adapter']);

0 comments on commit 4856300

Please sign in to comment.
Something went wrong with that request. Please try again.