Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 11 additions & 19 deletions src/Illuminate/Database/DatabaseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ public function __construct($app, ConnectionFactory $factory)
*/
public function connection($name = null)
{
$name = enum_value($name) ?: $this->getDefaultConnection();

[$database, $type] = $this->parseConnectionName($name);
[$database, $type] = $this->parseConnectionName($name = enum_value($name) ?? $this->getDefaultConnection());

// If we haven't created this connection, we'll create it based on the config
// provided in the application. Once we've created the connections we will
Expand Down Expand Up @@ -141,15 +139,15 @@ public static function calculateDynamicConnectionName(array $config)
/**
* Get a database connection instance from the given configuration.
*
* @param string $name
* @param \UnitEnum|string $name
* @param array $config
* @param bool $force
* @return \Illuminate\Database\ConnectionInterface
*/
public function connectUsing(string $name, array $config, bool $force = false)
{
if ($force) {
$this->purge($name);
$this->purge($name = enum_value($name));
}

if (isset($this->connections[$name])) {
Expand All @@ -173,8 +171,6 @@ public function connectUsing(string $name, array $config, bool $force = false)
*/
protected function parseConnectionName($name)
{
$name = $name ?: $this->getDefaultConnection();

return Str::endsWith($name, ['::read', '::write'])
? explode('::', $name, 2)
: [$name, null];
Expand Down Expand Up @@ -217,8 +213,6 @@ protected function makeConnection($name)
*/
protected function configuration($name)
{
$name = $name ?: $this->getDefaultConnection();

$connections = $this->app['config']['database.connections'];

$config = $this->dynamicConnectionConfigurations[$name] ?? Arr::get($connections, $name);
Expand Down Expand Up @@ -299,40 +293,38 @@ protected function setPdoForType(Connection $connection, $type = null)
/**
* Disconnect from the given database and remove from local cache.
*
* @param string|null $name
* @param \UnitEnum|string|null $name
* @return void
*/
public function purge($name = null)
{
$name = $name ?: $this->getDefaultConnection();

$this->disconnect($name);
$this->disconnect($name = enum_value($name) ?? $this->getDefaultConnection());

unset($this->connections[$name]);
}

/**
* Disconnect from the given database.
*
* @param string|null $name
* @param \UnitEnum|string|null $name
* @return void
*/
public function disconnect($name = null)
{
if (isset($this->connections[$name = $name ?: $this->getDefaultConnection()])) {
if (isset($this->connections[$name = enum_value($name) ?? $this->getDefaultConnection()])) {
$this->connections[$name]->disconnect();
}
}

/**
* Reconnect to the given database.
*
* @param string|null $name
* @param \UnitEnum|string|null $name
* @return \Illuminate\Database\Connection
*/
public function reconnect($name = null)
{
$this->disconnect($name = $name ?: $this->getDefaultConnection());
$this->disconnect($name = enum_value($name) ?? $this->getDefaultConnection());

if (! isset($this->connections[$name])) {
return $this->connection($name);
Expand All @@ -344,15 +336,15 @@ public function reconnect($name = null)
/**
* Set the default database connection for the callback execution.
*
* @param string $name
* @param \UnitEnum|string $name
* @param callable $callback
* @return mixed
*/
public function usingConnection($name, callable $callback)
{
$previousName = $this->getDefaultConnection();

$this->setDefaultConnection($name);
$this->setDefaultConnection($name = enum_value($name));

try {
return $callback();
Expand Down