1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 16:34:04 +00:00

Add Postgres bootstrapper

This commit is contained in:
lukinovec 2023-05-09 15:13:10 +02:00
parent eb7baa5360
commit 3c098dc78e

View file

@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Bootstrappers\Integrations;
use Stancl\Tenancy\Contracts\Tenant;
use Illuminate\Database\DatabaseManager;
use Illuminate\Contracts\Config\Repository;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
class PostgresTenancyBootstrapper 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
{
$this->database->purge('central');
$this->config->set('database.connections.pgsql.username', $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);
}
}