mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 12:24:04 +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 */
|
||||
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->database = $database;
|
||||
$this->tenancy = $tenantManager;
|
||||
$this->originalDefaultConnectionName = $app['config']['database.default'];
|
||||
}
|
||||
|
||||
|
|
@ -150,6 +154,11 @@ class DatabaseManager
|
|||
$database = $tenant->getDatabaseName();
|
||||
$manager = $this->getTenantDatabaseManager($tenant);
|
||||
|
||||
$afterCreating = array_merge(
|
||||
$afterCreating,
|
||||
$this->tenancy->event('database.creating', $database, $tenant)
|
||||
);
|
||||
|
||||
if ($this->app['config']['tenancy.queue_database_creation'] ?? false) {
|
||||
$chain = [];
|
||||
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();
|
||||
$manager = $this->getTenantDatabaseManager($tenant);
|
||||
|
||||
$this->event('database.deleting', $database, $tenant);
|
||||
|
||||
if ($this->app['config']['tenancy.queue_database_deletion'] ?? false) {
|
||||
QueuedTenantDatabaseDeleter::dispatch($manager, $database);
|
||||
} else {
|
||||
$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->event('created', $tenant);
|
||||
|
|
@ -379,17 +377,6 @@ class TenantManager
|
|||
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
|
||||
{
|
||||
return $this->app['config']['tenancy.queue_database_creation'] ?? false;
|
||||
|
|
@ -440,7 +427,7 @@ class TenantManager
|
|||
* @param mixed ...$args
|
||||
* @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) {
|
||||
$results = array_merge($results, $listener($this, ...$args) ?? []);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue