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

[11.x] Add DB::build method #53464

Merged
merged 2 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions src/Illuminate/Database/DatabaseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ public function connection($name = null)
return $this->connections[$name];
}

/**
* Build a database connection instance from the given configuration.
*
* @param array $config
* @return \Illuminate\Database\ConnectionInterface
*/
public function build(array $config)
{
return $this->connectUsing(
$config['name'] ?? 'ondemand',
$config,
true,
);
}

/**
* Get a database connection instance from the given configuration.
*
Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/Support/Facades/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
/**
* @method static \Illuminate\Database\Connection connection(string|null $name = null)
* @method static \Illuminate\Database\ConnectionInterface connectUsing(string $name, array $config, bool $force = false)
* @method static \Illuminate\Database\ConnectionInterface build(array $config)
* @method static void purge(string|null $name = null)
* @method static void disconnect(string|null $name = null)
* @method static \Illuminate\Database\Connection reconnect(string|null $name = null)
Expand Down
14 changes: 14 additions & 0 deletions tests/Integration/Database/DatabaseConnectionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,25 @@

use Illuminate\Database\DatabaseManager;
use Illuminate\Database\Events\ConnectionEstablished;
use Illuminate\Database\SQLiteConnection;
use Illuminate\Events\Dispatcher;
use RuntimeException;

class DatabaseConnectionsTest extends DatabaseTestCase
{
public function testBuildDatabaseConnection()
{
/** @var \Illuminate\Database\DatabaseManager $manager */
$manager = $this->app->make(DatabaseManager::class);

$connection = $manager->build([
'driver' => 'sqlite',
'database' => ':memory:',
]);

$this->assertInstanceOf(SQLiteConnection::class, $connection);
}

public function testEstablishDatabaseConnection()
{
/** @var \Illuminate\Database\DatabaseManager $manager */
Expand Down