1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 15:54:03 +00:00
This commit is contained in:
Samuel Štancl 2019-09-16 18:09:09 +02:00
parent 50a77ee826
commit a9c37d1535
8 changed files with 24 additions and 25 deletions

View file

@ -37,7 +37,7 @@ class DatabaseManager
*/
public function connect(Tenant $tenant)
{
$this->createTenantConnection($tenant);
$this->createTenantConnection($tenant->getDatabaseName(), $tenant->getConnectionName());
$this->switchConnection($tenant->getConnectionName());
}
@ -54,14 +54,12 @@ class DatabaseManager
/**
* Create the tenant database connection.
*
* @param Tenant $tenant
* @param string $databaseName
* @param string $connectionName
* @return void
*/
public function createTenantConnection(Tenant $tenant)
public function createTenantConnection($databaseName, $connectionName)
{
$databaseName = $tenant->getDatabaseName();
$connectionName = $tenant->getConnectionName();
// Create the database connection.
$based_on = $this->app['config']['tenancy.database.based_on'] ?? $this->originalDefaultConnectionName;
$this->app['config']["database.connections.$connectionName"] = $this->app['config']['database.connections.' . $based_on];
@ -129,7 +127,7 @@ class DatabaseManager
protected function getTenantDatabaseManager(Tenant $tenant): TenantDatabaseManager
{
$this->createTenantConnection($tenant);
$this->createTenantConnection($tenant->getDatabaseName(), $tenant->getConnectionName());
$driver = $this->getDriver($tenant->getConnectionName());
$databaseManagers = $this->app['config']['tenancy.database_managers'];

View file

@ -26,7 +26,7 @@ class TelescopeTags implements Feature
if (in_array('tenancy', optional(request()->route())->middleware() ?? [])) {
$tags = array_merge($tags, [
'tenant:' . tenant('id'),
'domain:' . tenant('domain'),
// 'domain:' . tenant('domain'), todo?
]);
}

View file

@ -44,13 +44,13 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper
return [];
}
[$id, $domain] = tenant()->get(['id', 'domain']);
$id = tenant()->get('id');
return [
'tenant_id' => $id,
'tags' => [
"tenant:$id",
"domain:$domain",
// todo domain?
],
];
}

View file

@ -97,8 +97,9 @@ class TenantManager
return $this;
}
public function init(string $domain): self
public function init(string $domain = null): self
{
$domain = $domain ?? request()->getHost();
$this->initializeTenancy($this->findByDomain($domain));
return $this;

View file

@ -11,11 +11,11 @@ class DatabaseManagerTest extends TestCase
public $autoInitTenancy = false;
/** @test */
public function disconnect_method_works()
public function reconnect_method_works()
{
$old_connection_name = app(\Illuminate\Database\DatabaseManager::class)->connection()->getName();
tenancy()->init();
tenancy()->disconnectDatabase();
tenancy()->init('test.localhost');
app(\Stancl\Tenancy\DatabaseManager::class)->reconnect();
$new_connection_name = app(\Illuminate\Database\DatabaseManager::class)->connection()->getName();
$this->assertSame($old_connection_name, $new_connection_name);

View file

@ -22,7 +22,7 @@ class ReidentificationTest extends TestCase
$originals[$disk] = config("filesystems.disks.{$disk}.root");
}
tenancy()->init('localhost');
tenancy()->init('test.localhost');
Tenant::new()->withDomains(['second.localhost'])->save();
tenancy()->init('second.localhost');
@ -49,7 +49,7 @@ class ReidentificationTest extends TestCase
{
$original = storage_path();
tenancy()->init('localhost');
tenancy()->init('test.localhost');
Tenant::new()->withDomains(['second.localhost'])->save();
tenancy()->init('second.localhost');

View file

@ -14,7 +14,7 @@ class TenantManagerEventsTest extends TestCase
{
$id = Tenant::new()->withDomains(['foo.localhost'])->save()['id'];
Tenancy::bootstrapping(function ($tenantManager) use ($id) {
Tenancy::eventListener('bootstrapping', function ($tenantManager) use ($id) {
if ($tenantManager->tenant['id'] === $id) {
config(['tenancy.foo' => 'bar']);
}
@ -30,7 +30,7 @@ class TenantManagerEventsTest extends TestCase
{
$id = Tenant::new()->withDomains(['foo.localhost'])->save()['id'];
Tenancy::bootstrapped(function ($tenantManager) use ($id) {
Tenancy::eventListener('bootstrapped', function ($tenantManager) use ($id) {
if ($tenantManager->tenant['id'] === $id) {
config(['tenancy.foo' => 'bar']);
}
@ -46,7 +46,7 @@ class TenantManagerEventsTest extends TestCase
{
$id = Tenant::new()->withDomains(['foo.localhost'])->save()['id'];
Tenancy::ending(function ($tenantManager) use ($id) {
Tenancy::eventListener('ending', function ($tenantManager) use ($id) {
if ($tenantManager->tenant['id'] === $id) {
config(['tenancy.foo' => 'bar']);
}
@ -64,7 +64,7 @@ class TenantManagerEventsTest extends TestCase
{
$id = Tenant::new()->withDomains(['foo.localhost'])->save()['id'];
Tenancy::ended(function ($tenantManager) use ($id) {
Tenancy::eventListener('ended', function ($tenantManager) use ($id) {
if ($tenantManager->tenant['id'] === $id) {
config(['tenancy.foo' => 'bar']);
}
@ -102,7 +102,7 @@ class TenantManagerEventsTest extends TestCase
$id = Tenant::create('abc.localhost')['id'];
Tenancy::bootstrapping(function ($tenancy) use ($id) {
Tenancy::eventListener('bootstrapping', function ($tenancy) use ($id) {
if ($tenancy->tenant['id'] === $id) {
$tenancy->database->useConnection('tenantabc');
@ -125,7 +125,7 @@ class TenantManagerEventsTest extends TestCase
$id = Tenant::create('abc.localhost')['id'];
Tenancy::bootstrapping(function ($tenancy) use ($id) {
Tenancy::eventListener('bootstrapping', function ($tenancy) use ($id) {
if ($tenancy->tenant['id'] === $id) {
$tenancy->database->useConnection('tenantabc');
// return ['database'];

View file

@ -14,13 +14,13 @@ class TenantManagerTest extends TestCase
public $autoInitTenancy = false;
/** @test */
public function current_tenant_is_stored_in_the_tenant_property()
public function current_tenant_can_be_retrieved_using_getTenant()
{
$tenant = Tenant::new()->withDomains(['localhost'])->save();
tenancy()->init('localhost');
tenancy()->init('test.localhost');
$this->assertSame($tenant, tenancy()->tenant);
$this->assertSame($tenant, tenancy()->getTenant());
}
/** @test */