1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-13 04:24:03 +00:00

WIP DatabaseManager

This commit is contained in:
Samuel Štancl 2019-09-08 15:31:45 +02:00
parent af5b7e8558
commit 9dd90746aa
5 changed files with 61 additions and 17 deletions

View file

@ -9,9 +9,11 @@ use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
interface StorageDriver interface StorageDriver
{ {
public function createTenant(Tenant $tenant): bool; public function createTenant(Tenant $tenant): void;
public function updateTenant(Tenant $tenant): bool; public function updateTenant(Tenant $tenant): void;
public function deleteTenant(Tenant $tenant): void;
/** /**
* Find a tenant using an id. * Find a tenant using an id.

View file

@ -8,7 +8,7 @@ abstract class TenantCannotBeCreatedException extends \Exception
{ {
abstract public function reason(): string; abstract public function reason(): string;
private $message; protected $message;
public function __construct() public function __construct()
{ {

View file

@ -24,6 +24,12 @@ class DatabaseManagerv2
$this->originalDefaultConnectionName = $app['config']['database.default']; $this->originalDefaultConnectionName = $app['config']['database.default'];
} }
/**
* Connect to a tenant's database.
*
* @param Tenant $tenant
* @return void
*/
public function connect(Tenant $tenant) public function connect(Tenant $tenant)
{ {
$connection = 'tenant'; // todo tenant-specific connections $connection = 'tenant'; // todo tenant-specific connections
@ -31,24 +37,45 @@ class DatabaseManagerv2
$this->switchConnection($connection); $this->switchConnection($connection);
} }
/**
* Reconnect to the default non-tenant connection.
*
* @return void
*/
public function reconnect() public function reconnect()
{ {
$this->switchConnection($this->originalDefaultConnectionName); $this->switchConnection($this->originalDefaultConnectionName);
} }
/**
* Create the tenant database connection.
*
* @param string $databaseName
* @param string $connectionName
* @return void
*/
public function createTenantConnection(string $databaseName, string $connectionName = null) public function createTenantConnection(string $databaseName, string $connectionName = null)
{ {
$connectionName = $connectionName ?? 'tenant'; // todo $connectionName = $connectionName ?? 'tenant'; // todo
// Create the database connection. // Create the database connection.
$based_on = config('tenancy.database.based_on') ?? $this->originalDefaultConnectionName; $based_on = $this->app['config']['tenancy.database.based_on'] ?? $this->originalDefaultConnectionName;
config()->set([ $this->app['config']["database.connections.$connectionName"] = $this->app['config']['database.connections.' . $based_on];
"database.connections.$connectionName" => config('database.connections.' . $based_on),
]);
// Change DB name // Change database name.
$databaseName = $this->getDriver($connectionName) === 'sqlite' ? database_path($databaseName) : $databaseName; $databaseName = $this->getDriver($connectionName) === 'sqlite' ? database_path($databaseName) : $databaseName;
config()->set(["database.connections.$connectionName.database" => $databaseName]); $this->app['config']["database.connections.$connectionName.database"] = $databaseName;
}
/**
* Get the driver of a database connection.
*
* @param string $connectionName
* @return string
*/
protected function getDriver(string $connectionName): string
{
return $this->app['config']["database.connections.$connectionName.driver"];
} }
public function switchConnection($connection) public function switchConnection($connection)

View file

@ -24,7 +24,7 @@ class DatabaseTenancyBootstrapper implements TenancyBootstrapper
public function start(Tenant $tenant) public function start(Tenant $tenant)
{ {
$this->database->connect($tenant->getDatabaseName()); $this->database->connect($tenant);
} }
public function end() public function end()

View file

@ -5,7 +5,7 @@ declare(strict_types=1);
namespace Stancl\Tenancy; namespace Stancl\Tenancy;
use Illuminate\Foundation\Application; use Illuminate\Foundation\Application;
use Illuminate\Contracts\Console\Kernel as Artisan; use Illuminate\Contracts\Console\Kernel as ConsoleKernel;
use Stancl\Tenancy\Exceptions\NoTenantIdentifiedException; use Stancl\Tenancy\Exceptions\NoTenantIdentifiedException;
use Stancl\Tenancy\Contracts\TenantCannotBeCreatedException; use Stancl\Tenancy\Contracts\TenantCannotBeCreatedException;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException; use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
@ -25,21 +25,25 @@ class TenantManagerv2
/** @var Application */ /** @var Application */
protected $app; protected $app;
/** @var ConsoleKernel */
protected $artisan;
/** @var Contracts\StorageDriver */ /** @var Contracts\StorageDriver */
protected $storage; protected $storage;
/** @var Artisan */ /** @var DatabaseManager */
protected $artisan; protected $database;
// todo event "listeners" instead of "callbacks" // todo event "listeners" instead of "callbacks"
/** @var callable[][] */ /** @var callable[][] */
protected $callbacks = []; protected $callbacks = [];
public function __construct(Application $app, Contracts\StorageDriver $storage, Artisan $artisan) public function __construct(Application $app, ConsoleKernel $artisan, Contracts\StorageDriver $storage, DatabaseManager $database)
{ {
$this->app = $app; $this->app = $app;
$this->storage = $storage; $this->storage = $storage;
$this->artisan = $artisan; $this->artisan = $artisan;
$this->database = $database;
$this->bootstrapFeatures(); $this->bootstrapFeatures();
} }
@ -49,9 +53,9 @@ class TenantManagerv2
$this->ensureTenantCanBeCreated($tenant); $this->ensureTenantCanBeCreated($tenant);
$this->storage->createTenant($tenant); $this->storage->createTenant($tenant);
$this->database->create($tenant); $this->database->createDatabase($tenant);
if ($this->migrateAfterCreation()) { if ($this->shouldMigrateAfterCreation()) {
$this->artisan->call('tenants:migrate', [ $this->artisan->call('tenants:migrate', [
'--tenants' => [$tenant['id']], '--tenants' => [$tenant['id']],
]); ]);
@ -60,6 +64,17 @@ class TenantManagerv2
return $this; return $this;
} }
public function deleteTenant(Tenant $tenant): self
{
$this->storage->deleteTenant($tenant);
if ($this->shouldDeleteDatabase()) {
$this->database->deleteDatabase($tenant);
}
return $this;
}
/** /**
* Ensure that a tenant can be created. * Ensure that a tenant can be created.
* *
@ -199,7 +214,7 @@ class TenantManagerv2
return array_key_diff($this->app['config']['tenancy.bootstrappers'], $except); return array_key_diff($this->app['config']['tenancy.bootstrappers'], $except);
} }
public function migrateAfterCreation(): bool public function shouldMigrateAfterCreation(): bool
{ {
return $this->app['config']['tenancy.migrate_after_creation'] ?? false; return $this->app['config']['tenancy.migrate_after_creation'] ?? false;
} }