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

Move DatabaseManager

This commit is contained in:
Samuel Štancl 2020-05-30 15:38:29 +02:00
parent 16a598bb17
commit 579779b88b
9 changed files with 13 additions and 27 deletions

View file

@ -1,88 +0,0 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy;
use Illuminate\Config\Repository;
use Illuminate\Database\DatabaseManager as BaseDatabaseManager;
use Illuminate\Foundation\Application;
use Stancl\Tenancy\Contracts\TenantCannotBeCreatedException;
use Stancl\Tenancy\Contracts\TenantWithDatabase;
use Stancl\Tenancy\Exceptions\DatabaseManagerNotRegisteredException;
use Stancl\Tenancy\Exceptions\TenantDatabaseAlreadyExistsException;
/**
* @internal Class is subject to breaking changes in minor and patch versions.
*/
class DatabaseManager
{
/** @var Application */
protected $app;
/** @var BaseDatabaseManager */
protected $database;
/** @var Repository */
protected $config;
public function __construct(Application $app, BaseDatabaseManager $database, Repository $config)
{
$this->app = $app;
$this->database = $database;
$this->config = $config;
}
/**
* Connect to a tenant's database.
*/
public function connectToTenant(TenantWithDatabase $tenant)
{
$this->database->purge('tenant');
$this->createTenantConnection($tenant);
$this->setDefaultConnection('tenant');
}
/**
* Reconnect to the default non-tenant connection.
*/
public function reconnectToCentral()
{
if (tenancy()->initialized) {
$this->database->purge('tenant');
}
$this->setDefaultConnection($this->config->get('tenancy.database.central_connection'));
}
/**
* Change the default database connection config.
*/
public function setDefaultConnection(string $connection)
{
$this->app['config']['database.default'] = $connection;
$this->database->setDefaultConnection($connection);
}
/**
* Create the tenant database connection.
*/
public function createTenantConnection(TenantWithDatabase $tenant)
{
$this->app['config']['database.connections.tenant'] = $tenant->database()->connection();
}
/**
* Check if a tenant can be created.
*
* @throws TenantCannotBeCreatedException
* @throws DatabaseManagerNotRegisteredException
* @throws TenantDatabaseAlreadyExistsException
*/
public function ensureTenantCanBeCreated(TenantWithDatabase $tenant): void
{
if ($tenant->database()->manager()->databaseExists($database = $tenant->database()->getName())) {
throw new TenantDatabaseAlreadyExistsException($database);
}
}
}