1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-16 00:14:02 +00:00

create host connection for creating, deleting tenants

This commit is contained in:
Abrar Ahmad 2022-09-13 14:41:28 +05:00
parent abd17f83a1
commit cd6e989c09
7 changed files with 201 additions and 8 deletions

View file

@ -100,6 +100,11 @@ class DatabaseConfig
?? config('tenancy.database.central_connection');
}
public function getTenantHostConnectionName(): ?string
{
return config('tenancy.database.tenant_host_connection_name');
}
/**
* Tenant's own database connection config.
*/
@ -114,6 +119,27 @@ class DatabaseConfig
);
}
/**
* Tenant's host database connection config.
*/
public function hostConnection(): array
{
$config = $this->tenantConfig();
$template = $this->getTemplateConnectionName();
$templateConnection = config("database.connections.{$template}");
if ($this->manager() instanceof Contracts\ManagesDatabaseUsers) {
unset($config['username']);
unset($config['password']);
}
if (empty($config)) {
$config = $templateConnection;
}
return array_replace($templateConnection, $config);
}
/**
* Additional config for the database connection, specific to this tenant.
*/
@ -158,4 +184,25 @@ class DatabaseConfig
return $databaseManager;
}
/** Get the TenantDatabaseManager for this tenant's connection. */
public function hostManager(): Contracts\TenantDatabaseManager
{
$tenantHostConnection = $this->getTenantHostConnectionName();
config(["database.connections.{$tenantHostConnection}" => $this->hostConnection()]);
$driver = config("database.connections.{$tenantHostConnection}.driver");
$databaseManagers = config('tenancy.database.managers');
if (! array_key_exists($driver, $databaseManagers)) {
throw new Exceptions\DatabaseManagerNotRegisteredException($driver);
}
/** @var Contracts\TenantDatabaseManager $databaseManager */
$databaseManager = app($databaseManagers[$driver]);
$databaseManager->setConnection($tenantHostConnection);
return $databaseManager;
}
}