mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 08:14:02 +00:00
Use DB hooks instead of a SC key
This commit is contained in:
parent
93d9355391
commit
96679240de
2 changed files with 17 additions and 15 deletions
|
|
@ -25,10 +25,14 @@ class DatabaseManager
|
||||||
/** @var BaseDatabaseManager */
|
/** @var BaseDatabaseManager */
|
||||||
protected $database;
|
protected $database;
|
||||||
|
|
||||||
public function __construct(Application $app, BaseDatabaseManager $database)
|
/** @var TenantManager */
|
||||||
|
protected $tenancy;
|
||||||
|
|
||||||
|
public function __construct(Application $app, BaseDatabaseManager $database, TenantManager $tenantManager)
|
||||||
{
|
{
|
||||||
$this->app = $app;
|
$this->app = $app;
|
||||||
$this->database = $database;
|
$this->database = $database;
|
||||||
|
$this->tenancy = $tenantManager;
|
||||||
$this->originalDefaultConnectionName = $app['config']['database.default'];
|
$this->originalDefaultConnectionName = $app['config']['database.default'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -150,6 +154,11 @@ class DatabaseManager
|
||||||
$database = $tenant->getDatabaseName();
|
$database = $tenant->getDatabaseName();
|
||||||
$manager = $this->getTenantDatabaseManager($tenant);
|
$manager = $this->getTenantDatabaseManager($tenant);
|
||||||
|
|
||||||
|
$afterCreating = array_merge(
|
||||||
|
$afterCreating,
|
||||||
|
$this->tenancy->event('database.creating', $database, $tenant)
|
||||||
|
);
|
||||||
|
|
||||||
if ($this->app['config']['tenancy.queue_database_creation'] ?? false) {
|
if ($this->app['config']['tenancy.queue_database_creation'] ?? false) {
|
||||||
$chain = [];
|
$chain = [];
|
||||||
foreach ($afterCreating as $item) {
|
foreach ($afterCreating as $item) {
|
||||||
|
|
@ -173,6 +182,8 @@ class DatabaseManager
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->tenancy->event('database.created', $database, $tenant);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -186,11 +197,15 @@ class DatabaseManager
|
||||||
$database = $tenant->getDatabaseName();
|
$database = $tenant->getDatabaseName();
|
||||||
$manager = $this->getTenantDatabaseManager($tenant);
|
$manager = $this->getTenantDatabaseManager($tenant);
|
||||||
|
|
||||||
|
$this->event('database.deleting', $database, $tenant);
|
||||||
|
|
||||||
if ($this->app['config']['tenancy.queue_database_deletion'] ?? false) {
|
if ($this->app['config']['tenancy.queue_database_deletion'] ?? false) {
|
||||||
QueuedTenantDatabaseDeleter::dispatch($manager, $database);
|
QueuedTenantDatabaseDeleter::dispatch($manager, $database);
|
||||||
} else {
|
} else {
|
||||||
$manager->deleteDatabase($database);
|
$manager->deleteDatabase($database);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->event('database.deleted', $database, $tenant);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -95,8 +95,6 @@ class TenantManager
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
$afterCreating = array_merge($afterCreating, $this->getUserPostCreationActions());
|
|
||||||
|
|
||||||
$this->database->createDatabase($tenant, $afterCreating);
|
$this->database->createDatabase($tenant, $afterCreating);
|
||||||
|
|
||||||
$this->event('created', $tenant);
|
$this->event('created', $tenant);
|
||||||
|
|
@ -379,17 +377,6 @@ class TenantManager
|
||||||
return $this->shouldMigrateAfterCreation() && $this->app['config']['tenancy.seed_after_migration'] ?? false;
|
return $this->shouldMigrateAfterCreation() && $this->app['config']['tenancy.seed_after_migration'] ?? false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* A user-specified list of callbacks or jobs executed after
|
|
||||||
* creating, migrating, and seeding the tenant database.
|
|
||||||
*
|
|
||||||
* @return \Illuminate\Contracts\Queue\ShouldQueue[]|callable[]
|
|
||||||
*/
|
|
||||||
public function getUserPostCreationActions(): array
|
|
||||||
{
|
|
||||||
return $this->app['tenancy.postCreationActions'] ?? [];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function databaseCreationQueued(): bool
|
public function databaseCreationQueued(): bool
|
||||||
{
|
{
|
||||||
return $this->app['config']['tenancy.queue_database_creation'] ?? false;
|
return $this->app['config']['tenancy.queue_database_creation'] ?? false;
|
||||||
|
|
@ -440,7 +427,7 @@ class TenantManager
|
||||||
* @param mixed ...$args
|
* @param mixed ...$args
|
||||||
* @return string[]
|
* @return string[]
|
||||||
*/
|
*/
|
||||||
protected function event(string $name, ...$args): array
|
public function event(string $name, ...$args): array
|
||||||
{
|
{
|
||||||
return array_reduce($this->eventListeners[$name] ?? [], function ($results, $listener) use ($args) {
|
return array_reduce($this->eventListeners[$name] ?? [], function ($results, $listener) use ($args) {
|
||||||
$results = array_merge($results, $listener($this, ...$args) ?? []);
|
$results = array_merge($results, $listener($this, ...$args) ?? []);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue