Skip to content

Commit

Permalink
code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Nov 2, 2016
1 parent 66ce5b6 commit ed28c7f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
21 changes: 8 additions & 13 deletions src/Illuminate/Database/Connectors/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Database\PostgresConnection;
use Illuminate\Database\SqlServerConnection;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Debug\ExceptionHandler;

class ConnectionFactory
{
Expand Down Expand Up @@ -113,30 +114,24 @@ protected function createPdoResolver(array $config)
protected function createPdoResolverWithHosts(array $config)
{
return function () use ($config) {
if (! is_array($config['host'])) {
$hosts = [$config['host']];
} else {
$hosts = $config['host'];
shuffle($hosts);
$hosts = is_array($config['host']) ? $config['host'] : [$config['host']];

if (empty($hosts)) {
throw new InvalidArgumentException('Database hosts array is empty.');
}

$lastHost = end($hosts);
foreach ($hosts as $host) {
foreach (Arr::shuffle($hosts) as $key => $host) {
$config['host'] = $host;

try {
return $this->createConnector($config)->connect($config);
} catch (PDOException $e) {
if ($host !== $lastHost) {
$this->container->make('Illuminate\Contracts\Debug\ExceptionHandler')->report($e);
if (count($hosts) - 1 === $key) {
$this->container->make(ExceptionHandler::class)->report($e);
}
}
}

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

throw $e;
};
}
Expand Down
13 changes: 13 additions & 0 deletions src/Illuminate/Support/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,19 @@ public static function set(&$array, $key, $value)
return $array;
}

/**
* Shuffle the given array and return the result.
*
* @param array $array
* @return array
*/
public static function shuffle($array)
{
shuffle($array);

return $array;
}

/**
* Sort the array using the given callback or "dot" notation.
*
Expand Down

0 comments on commit ed28c7f

Please sign in to comment.