Skip to content

Commit

Permalink
add ConnectionManager back into the mix
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul M. Jones committed Feb 11, 2012
1 parent 1b2ca0c commit 4856300
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 215 deletions.
83 changes: 0 additions & 83 deletions hold/ConnectionFactory.php

This file was deleted.

72 changes: 0 additions & 72 deletions hold/ConnectionFactoryTest.php

This file was deleted.

18 changes: 0 additions & 18 deletions hold/Exception_ConnectionFactory.php

This file was deleted.

72 changes: 46 additions & 26 deletions hold/ConnectionManager.php → src/Aura/Sql/ConnectionManager.php
Expand Up @@ -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,
Expand All @@ -29,7 +32,7 @@ class AdapterManager

protected $slaves = [];

protected $factory;
protected $adapter_factory;

protected $conn = [
'default' => null,
Expand All @@ -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
Expand Down Expand Up @@ -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'];
}
Expand All @@ -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) {
Expand All @@ -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)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
26 changes: 20 additions & 6 deletions 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,
Expand All @@ -28,4 +32,14 @@ public function getDsnHost()
{
return $this->params['dsn']['host'];
}

public function fetchTableList($schema = null)
{
return [];
}

public function fetchTableCols($spec)
{
return [];
}
}

0 comments on commit 4856300

Please sign in to comment.