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

WIP DatabaseManager

This commit is contained in:
Samuel Štancl 2019-09-08 13:46:07 +02:00
parent ab0a40a17e
commit 04b4e0644f
7 changed files with 27 additions and 11 deletions

View file

@ -4,37 +4,44 @@ declare(strict_types=1);
namespace Stancl\Tenancy;
use Illuminate\Foundation\Application;
use Illuminate\Database\DatabaseManager as BaseDatabaseManager;
class DatabaseManagerv2
{
/** @var string */
public $originalDefaultConnectionName;
/** @var Application */
protected $app;
/** @var BaseDatabaseManager */
protected $database;
public function __construct(BaseDatabaseManager $database)
public function __construct(Application $app, BaseDatabaseManager $database)
{
$this->database = $database;
$this->originalDefaultConnectionName = $app['config']['database.default'];
}
public function connect(Tenant $tenant)
{
$connection = $tenant->getConnectionName(); // todo
$connection = 'tenant'; // todo tenant-specific connections
$this->createTenantConnection($tenant->getDatabaseName(), $connection);
$this->switchConnection($connection);
}
public function reconnect()
{
$this->switchConnection($this->originalDefaultConnection);
$this->switchConnection($this->originalDefaultConnectionName);
}
public function createTenantConnection(string $databaseName, string $connectionName = null)
{
$connectionName = $connectionName ?: $this->defaultTenantConnectionName;
$connectionName = $connectionName ?? 'tenant'; // todo
// Create the database connection.
$based_on = config('tenancy.database.based_on') ?: config('database.default');
$based_on = config('tenancy.database.based_on') ?? $this->originalDefaultConnectionName;
config()->set([
"database.connections.$connectionName" => config('database.connections.' . $based_on),
]);