1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-04 22:54:04 +00:00

DatabaseManager WIP

This commit is contained in:
Samuel Štancl 2019-09-05 19:26:29 +02:00
parent 798df54d76
commit 41d8bb7e09
8 changed files with 125 additions and 78 deletions

51
src/DatabaseManagerv2.php Normal file
View file

@ -0,0 +1,51 @@
<?php
namespace Stancl\Tenancy;
use Illuminate\Database\DatabaseManager as BaseDatabaseManager;
class DatabaseManagerv2
{
/** @var BaseDatabaseManager */
protected $database;
public function __construct(BaseDatabaseManager $database)
{
$this->database = $database;
}
public function connect(Tenant $tenant)
{
$connection = $tenant->getConnectionName(); // todo
$this->createTenantConnection($tenant->getDatabaseName(), $connection);
$this->switchConnection($connection);
}
public function reconnect()
{
$this->switchConnection($this->originalDefaultConnection);
}
public function createTenantConnection(string $databaseName, string $connectionName = null)
{
$connectionName = $connectionName ?: $this->defaultTenantConnectionName;
// Create the database connection.
$based_on = config('tenancy.database.based_on') ?: config('database.default');
config()->set([
"database.connections.$connectionName" => config('database.connections.' . $based_on),
]);
// Change DB name
$databaseName = $this->getDriver($connectionName) === 'sqlite' ? database_path($databaseName) : $databaseName;
config()->set(["database.connections.$connectionName.database" => $databaseName]);
}
public function switchConnection($connection)
{
$this->database->purge();
$this->database->reconnect($connection);
$this->database->setDefaultConnection($connection);
}
}