Skip to content

Commit ed28c7f

Browse files
committed
code cleaning
1 parent 66ce5b6 commit ed28c7f

2 files changed

Lines changed: 21 additions & 13 deletions

File tree

src/Illuminate/Database/Connectors/ConnectionFactory.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Illuminate\Database\PostgresConnection;
1212
use Illuminate\Database\SqlServerConnection;
1313
use Illuminate\Contracts\Container\Container;
14+
use Illuminate\Contracts\Debug\ExceptionHandler;
1415

1516
class ConnectionFactory
1617
{
@@ -113,30 +114,24 @@ protected function createPdoResolver(array $config)
113114
protected function createPdoResolverWithHosts(array $config)
114115
{
115116
return function () use ($config) {
116-
if (! is_array($config['host'])) {
117-
$hosts = [$config['host']];
118-
} else {
119-
$hosts = $config['host'];
120-
shuffle($hosts);
117+
$hosts = is_array($config['host']) ? $config['host'] : [$config['host']];
118+
119+
if (empty($hosts)) {
120+
throw new InvalidArgumentException('Database hosts array is empty.');
121121
}
122122

123-
$lastHost = end($hosts);
124-
foreach ($hosts as $host) {
123+
foreach (Arr::shuffle($hosts) as $key => $host) {
125124
$config['host'] = $host;
126125

127126
try {
128127
return $this->createConnector($config)->connect($config);
129128
} catch (PDOException $e) {
130-
if ($host !== $lastHost) {
131-
$this->container->make('Illuminate\Contracts\Debug\ExceptionHandler')->report($e);
129+
if (count($hosts) - 1 === $key) {
130+
$this->container->make(ExceptionHandler::class)->report($e);
132131
}
133132
}
134133
}
135134

136-
if (empty($hosts)) {
137-
throw new InvalidArgumentException('Database hosts array cannot be empty');
138-
}
139-
140135
throw $e;
141136
};
142137
}

src/Illuminate/Support/Arr.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,19 @@ public static function set(&$array, $key, $value)
457457
return $array;
458458
}
459459

460+
/**
461+
* Shuffle the given array and return the result.
462+
*
463+
* @param array $array
464+
* @return array
465+
*/
466+
public static function shuffle($array)
467+
{
468+
shuffle($array);
469+
470+
return $array;
471+
}
472+
460473
/**
461474
* Sort the array using the given callback or "dot" notation.
462475
*

0 commit comments

Comments
 (0)