mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-06 04:34:02 +00:00
Rename bootstrapper
This commit is contained in:
parent
28efc0ca97
commit
8ccc27e8c3
2 changed files with 3 additions and 3 deletions
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Bootstrappers\Integrations;
|
||||
|
||||
use Illuminate\Contracts\Config\Repository;
|
||||
use Illuminate\Database\DatabaseManager;
|
||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
use Stancl\Tenancy\Database\Contracts\TenantWithDatabase;
|
||||
|
||||
/**
|
||||
* Purge central connection, set Postgres credentials to the tenant's credentials
|
||||
* and use Postgres as the central connection.
|
||||
*
|
||||
* This bootstrapper is intended to be used with single-database tenancy.
|
||||
*/
|
||||
class PostgresSingleDatabaseBootstrapper implements TenancyBootstrapper
|
||||
{
|
||||
protected array $originalCentralConnectionConfig;
|
||||
protected array $originalPostgresConfig;
|
||||
|
||||
public function __construct(
|
||||
protected Repository $config,
|
||||
protected DatabaseManager $database,
|
||||
) {
|
||||
$this->originalCentralConnectionConfig = $config->get('database.connections.central');
|
||||
$this->originalPostgresConfig = $config->get('database.connections.pgsql');
|
||||
}
|
||||
|
||||
public function bootstrap(Tenant $tenant): void
|
||||
{
|
||||
/** @var TenantWithDatabase $tenant */
|
||||
$this->database->purge('central');
|
||||
|
||||
$this->config->set('database.connections.pgsql.username', $tenant->database()->getUsername() ?? $tenant->getTenantKey());
|
||||
$this->config->set('database.connections.pgsql.password', $tenant->database()->getPassword() ?? 'password');
|
||||
|
||||
$this->config->set('database.connections.central', $this->config->get('database.connections.pgsql'));
|
||||
}
|
||||
|
||||
public function revert(): void
|
||||
{
|
||||
$this->database->purge('central');
|
||||
|
||||
$this->config->set('database.connections.central', $this->originalCentralConnectionConfig);
|
||||
$this->config->set('database.connections.pgsql', $this->originalPostgresConfig);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue