Skip to content

Commit

Permalink
Replaced tabs with spaces Handled edge case where empty array of host…
Browse files Browse the repository at this point in the history
…s would break createPdoResolverWithHosts
  • Loading branch information
Data33 committed Sep 22, 2016
1 parent 4252494 commit 1a305f4
Showing 1 changed file with 47 additions and 43 deletions.
90 changes: 47 additions & 43 deletions Connectors/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,54 +96,58 @@ protected function createReadPdo(array $config)
*/
protected function createPdoResolver(array $config)
{
if (array_key_exists('host', $config)) {
return $this->createPdoResolverWithHosts($config);
}
else{
return $this->createPdoResolverWithoutHosts($config);
}
if (array_key_exists('host', $config)) {
return $this->createPdoResolverWithHosts($config);
}
else{
return $this->createPdoResolverWithoutHosts($config);
}
}

/**
* Create a new Closure that resolves to a PDO instance with a specific host or an array of hosts.
*
* @param array $config
* @return \Closure
*/
/**
* Create a new Closure that resolves to a PDO instance with a specific host or an array of hosts.
*
* @param array $config
* @return \Closure
*/
protected function createPdoResolverWithHosts(array $config){
return function () use ($config) {
if (!is_array($config['host'])) {
$hosts = [$config['host']];
} else {
$hosts = $config['host'];
shuffle($hosts);
}

foreach($hosts as $host){
$config['host'] = $host;

try{
return $this->createConnector($config)->connect($config);
}
catch(\PDOException $e){
}
}

throw $e;
};
return function () use ($config) {
if (!is_array($config['host'])) {
$hosts = [$config['host']];
} else {
$hosts = $config['host'];
shuffle($hosts);
}

foreach($hosts as $host){
$config['host'] = $host;

try{
return $this->createConnector($config)->connect($config);
}
catch(\PDOException $e){
}
}

if (empty($hosts)) {
throw new InvalidArgumentException("Database hosts array cannot be empty");
}

throw $e;
};
}

/**
* Create a new Closure that resolves to a PDO instance where there is no configured host
*
* @param array $config
* @return \Closure
*/
protected function createPdoResolverWithoutHosts(array $config){
return function () use ($config) {
return $this->createConnector($config)->connect($config);
};
}
/**
* Create a new Closure that resolves to a PDO instance where there is no configured host
*
* @param array $config
* @return \Closure
*/
protected function createPdoResolverWithoutHosts(array $config){
return function () use ($config) {
return $this->createConnector($config)->connect($config);
};
}

/**
* Get the read configuration for a read / write connection.
Expand Down

0 comments on commit 1a305f4

Please sign in to comment.