diff --git a/src/Illuminate/Database/Connectors/ConnectionFactory.php b/src/Illuminate/Database/Connectors/ConnectionFactory.php index 479842a322d5..e8e572d622e8 100755 --- a/src/Illuminate/Database/Connectors/ConnectionFactory.php +++ b/src/Illuminate/Database/Connectors/ConnectionFactory.php @@ -11,6 +11,7 @@ use Illuminate\Database\PostgresConnection; use Illuminate\Database\SqlServerConnection; use Illuminate\Contracts\Container\Container; +use Illuminate\Contracts\Debug\ExceptionHandler; class ConnectionFactory { @@ -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; }; } diff --git a/src/Illuminate/Support/Arr.php b/src/Illuminate/Support/Arr.php index f72ebce1d5b9..f24de4f4721b 100755 --- a/src/Illuminate/Support/Arr.php +++ b/src/Illuminate/Support/Arr.php @@ -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. *