Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for naming additional database connections #54162

Draft
wants to merge 1 commit into
base: 11.x
Choose a base branch
from
Draft
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
21 changes: 17 additions & 4 deletions src/Illuminate/Database/DatabaseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function __construct($app, ConnectionFactory $factory)
*/
public function connection($name = null)
{
$name = $name ?: $this->getDefaultConnection();
$name = $this->mapConnectionName($name);

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

Expand Down Expand Up @@ -166,6 +166,19 @@ public function connectUsing(string $name, array $config, bool $force = false)
return tap($connection, fn ($connection) => $this->connections[$name] = $connection);
}

/**
* Map the given connection name to its configuration name or default if not provided.
*
* @param string $name
* @return string
*/
protected function mapConnectionName($name)
{
$name = $name ?: $this->getDefaultConnection();

return $this->app['config']['database.' . $name] ??= $name;
}

/**
* Parse the connection into an array of the name and read / write type.
*
Expand All @@ -174,7 +187,7 @@ public function connectUsing(string $name, array $config, bool $force = false)
*/
protected function parseConnectionName($name)
{
$name = $name ?: $this->getDefaultConnection();
$name = $this->mapConnectionName($name);

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

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

Expand Down Expand Up @@ -304,7 +317,7 @@ protected function setPdoForType(Connection $connection, $type = null)
*/
public function purge($name = null)
{
$name = $name ?: $this->getDefaultConnection();
$name = $this->mapConnectionName($name);

$this->disconnect($name);

Expand Down
Loading