forked from archtechx/tenancy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseManager.php
105 lines (89 loc) · 3.07 KB
/
DatabaseManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Database;
use Illuminate\Config\Repository;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Database\DatabaseManager as BaseDatabaseManager;
use Stancl\Tenancy\Contracts\ManagesDatabaseUsers;
use Stancl\Tenancy\Contracts\TenantCannotBeCreatedException;
use Stancl\Tenancy\Contracts\TenantWithDatabase;
use Stancl\Tenancy\Exceptions\DatabaseManagerNotRegisteredException;
use Stancl\Tenancy\Exceptions\TenantDatabaseAlreadyExistsException;
use Stancl\Tenancy\Exceptions\TenantDatabaseUserAlreadyExistsException;
/**
* @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->purgeTenantConnection();
$this->createTenantConnection($tenant);
$this->setDefaultConnection('tenant');
}
/**
* Reconnect to the default non-tenant connection.
*/
public function reconnectToCentral()
{
$this->purgeTenantConnection();
$this->setDefaultConnection($this->config->get('tenancy.database.central_connection'));
}
/**
* Change the default database connection config.
*/
public function setDefaultConnection(string $connection)
{
$this->config['database.default'] = $connection;
$this->database->setDefaultConnection($connection);
}
/**
* Create the tenant database connection.
*/
public function createTenantConnection(TenantWithDatabase $tenant)
{
$this->config['database.connections.tenant'] = $tenant->database()->connection();
}
/**
* Purge the tenant database connection.
*/
public function purgeTenantConnection()
{
if (array_key_exists('tenant', $this->database->getConnections())) {
$this->database->purge('tenant');
}
unset($this->config['database.connections.tenant']);
}
/**
* Check if a tenant can be created.
*
* @throws TenantCannotBeCreatedException
* @throws DatabaseManagerNotRegisteredException
* @throws TenantDatabaseAlreadyExistsException
*/
public function ensureTenantCanBeCreated(TenantWithDatabase $tenant): void
{
$manager = $tenant->database()->manager();
if ($manager->databaseExists($database = $tenant->database()->getName())) {
throw new TenantDatabaseAlreadyExistsException($database);
}
if ($manager instanceof ManagesDatabaseUsers && $manager->userExists($username = $tenant->database()->getUsername())) {
throw new TenantDatabaseUserAlreadyExistsException($username);
}
}
}