mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 13:54:03 +00:00
Add event prevents, Tenant facade
This commit is contained in:
parent
1a88cad4d6
commit
4aa35322da
9 changed files with 187 additions and 97 deletions
|
|
@ -41,6 +41,7 @@
|
||||||
],
|
],
|
||||||
"aliases": {
|
"aliases": {
|
||||||
"Tenancy": "Stancl\\Tenancy\\TenancyFacade",
|
"Tenancy": "Stancl\\Tenancy\\TenancyFacade",
|
||||||
|
"Tenant": "Stancl\\Tenancy\\TenancyFacade",
|
||||||
"GlobalCache": "Stancl\\Tenancy\\GlobalCacheFacade"
|
"GlobalCache": "Stancl\\Tenancy\\GlobalCacheFacade"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ use Stancl\Tenancy\Jobs\QueuedTenantDatabaseCreator;
|
||||||
use Stancl\Tenancy\Jobs\QueuedTenantDatabaseDeleter;
|
use Stancl\Tenancy\Jobs\QueuedTenantDatabaseDeleter;
|
||||||
use Illuminate\Database\DatabaseManager as BaseDatabaseManager;
|
use Illuminate\Database\DatabaseManager as BaseDatabaseManager;
|
||||||
|
|
||||||
class DatabaseManager
|
final class DatabaseManager
|
||||||
{
|
{
|
||||||
public $originalDefaultConnection;
|
public $originalDefaultConnection;
|
||||||
|
|
||||||
|
|
@ -19,8 +19,7 @@ class DatabaseManager
|
||||||
public function connect(string $database)
|
public function connect(string $database)
|
||||||
{
|
{
|
||||||
$this->createTenantConnection($database);
|
$this->createTenantConnection($database);
|
||||||
$this->database->setDefaultConnection('tenant');
|
$this->useConnection('tenant');
|
||||||
$this->database->reconnect('tenant');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function connectToTenant($tenant)
|
public function connectToTenant($tenant)
|
||||||
|
|
@ -105,4 +104,9 @@ class DatabaseManager
|
||||||
$database_name = $this->getDriver() === 'sqlite' ? database_path($database_name) : $database_name;
|
$database_name = $this->getDriver() === 'sqlite' ? database_path($database_name) : $database_name;
|
||||||
config()->set(['database.connections.tenant.database' => $database_name]);
|
config()->set(['database.connections.tenant.database' => $database_name]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function useConnection(string $connection) {
|
||||||
|
$this->database->setDefaultConnection($connection);
|
||||||
|
$this->database->reconnect($connection);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ use Stancl\Tenancy\Traits\BootstrapsTenancy;
|
||||||
use Illuminate\Contracts\Foundation\Application;
|
use Illuminate\Contracts\Foundation\Application;
|
||||||
use Stancl\Tenancy\Exceptions\CannotChangeUuidOrDomainException;
|
use Stancl\Tenancy\Exceptions\CannotChangeUuidOrDomainException;
|
||||||
|
|
||||||
class TenantManager
|
final class TenantManager
|
||||||
{
|
{
|
||||||
use BootstrapsTenancy;
|
use BootstrapsTenancy;
|
||||||
|
|
||||||
|
|
@ -30,7 +30,7 @@ class TenantManager
|
||||||
*
|
*
|
||||||
* @var DatabaseManager
|
* @var DatabaseManager
|
||||||
*/
|
*/
|
||||||
protected $database;
|
public $database;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Current tenant.
|
* Current tenant.
|
||||||
|
|
|
||||||
|
|
@ -21,42 +21,55 @@ trait BootstrapsTenancy
|
||||||
|
|
||||||
public function bootstrap()
|
public function bootstrap()
|
||||||
{
|
{
|
||||||
array_map(function ($listener) {
|
$prevented = $this->event('bootstrapping');
|
||||||
$listener($this);
|
|
||||||
}, $this->listeners['bootstrapping']);
|
|
||||||
|
|
||||||
$this->initialized = true;
|
$this->initialized = true;
|
||||||
|
|
||||||
$this->switchDatabaseConnection();
|
if (! $prevented->contains('database')) {
|
||||||
if ($this->app['config']['tenancy.redis.tenancy']) {
|
$this->switchDatabaseConnection();
|
||||||
$this->setPhpRedisPrefix($this->app['config']['tenancy.redis.prefixed_connections']);
|
|
||||||
}
|
}
|
||||||
$this->tagCache();
|
|
||||||
$this->suffixFilesystemRootPaths();
|
|
||||||
|
|
||||||
array_map(function ($listener) {
|
if (! $prevented->contains('redis')) {
|
||||||
$listener($this);
|
if ($this->app['config']['tenancy.redis.tenancy']) {
|
||||||
}, $this->listeners['bootstrapped']);
|
$this->setPhpRedisPrefix($this->app['config']['tenancy.redis.prefixed_connections']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $prevented->contains('cache')) {
|
||||||
|
$this->tagCache();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $prevented->contains('filesystem')) {
|
||||||
|
$this->suffixFilesystemRootPaths();
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->event('bootstrapped');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function end()
|
public function end()
|
||||||
{
|
{
|
||||||
array_map(function ($listener) {
|
$prevented = $this->event('ending');
|
||||||
$listener($this);
|
|
||||||
}, $this->listeners['ending']);
|
|
||||||
|
|
||||||
$this->initialized = false;
|
$this->initialized = false;
|
||||||
|
|
||||||
$this->disconnectDatabase();
|
if (! $prevented->contains('database')) {
|
||||||
if ($this->app['config']['tenancy.redis.tenancy']) {
|
$this->disconnectDatabase();
|
||||||
$this->resetPhpRedisPrefix($this->app['config']['tenancy.redis.prefixed_connections']);
|
|
||||||
}
|
}
|
||||||
$this->untagCache();
|
|
||||||
$this->resetFileSystemRootPaths();
|
|
||||||
|
|
||||||
array_map(function ($listener) {
|
if (! $prevented->contains('redis')) {
|
||||||
$listener($this);
|
if ($this->app['config']['tenancy.redis.tenancy']) {
|
||||||
}, $this->listeners['ended']);
|
$this->resetPhpRedisPrefix($this->app['config']['tenancy.redis.prefixed_connections']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $prevented->contains('cache')) {
|
||||||
|
$this->untagCache();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $prevented->contains('filesystem')) {
|
||||||
|
$this->resetFileSystemRootPaths();
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->event('ended');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function switchDatabaseConnection()
|
public function switchDatabaseConnection()
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Traits;
|
namespace Stancl\Tenancy\Traits;
|
||||||
|
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
trait TenantManagerEvents
|
trait TenantManagerEvents
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
|
@ -67,4 +69,17 @@ trait TenantManagerEvents
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fire an event.
|
||||||
|
*
|
||||||
|
* @param string $name Event name
|
||||||
|
* @return Collection Prevented events
|
||||||
|
*/
|
||||||
|
public function event(string $name): Collection
|
||||||
|
{
|
||||||
|
return array_reduce($this->listeners[$name], function ($prevents, $listener) {
|
||||||
|
return $prevents->merge($listener($this));
|
||||||
|
}, collect([]));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
namespace Stancl\Tenancy\Tests;
|
||||||
|
|
||||||
|
use Tenant;
|
||||||
use Tenancy;
|
use Tenancy;
|
||||||
|
|
||||||
class FacadeTest extends TestCase
|
class FacadeTest extends TestCase
|
||||||
|
|
@ -15,4 +16,14 @@ class FacadeTest extends TestCase
|
||||||
$this->assertSame('bar', Tenancy::get('foo'));
|
$this->assertSame('bar', Tenancy::get('foo'));
|
||||||
$this->assertSame('xyz', Tenancy::get('abc'));
|
$this->assertSame('xyz', Tenancy::get('abc'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function tenant_manager_can_be_accessed_using_the_Tenant_facade()
|
||||||
|
{
|
||||||
|
tenancy()->put('foo', 'bar');
|
||||||
|
Tenant::put('abc', 'xyz');
|
||||||
|
|
||||||
|
$this->assertSame('bar', Tenant::get('foo'));
|
||||||
|
$this->assertSame('xyz', Tenant::get('abc'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
114
tests/TenantManagerEventsTest.php
Normal file
114
tests/TenantManagerEventsTest.php
Normal file
|
|
@ -0,0 +1,114 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Tests;
|
||||||
|
|
||||||
|
use Tenant;
|
||||||
|
use Tenancy;
|
||||||
|
|
||||||
|
class TenantManagerEventsTest extends TestCase
|
||||||
|
{
|
||||||
|
/** @test */
|
||||||
|
public function bootstrapping_event_works()
|
||||||
|
{
|
||||||
|
$uuid = tenant()->create('foo.localhost')['uuid'];
|
||||||
|
|
||||||
|
Tenancy::bootstrapping(function ($tenantManager) use ($uuid) {
|
||||||
|
if ($tenantManager->tenant['uuid'] === $uuid) {
|
||||||
|
config(['tenancy.foo' => 'bar']);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->assertSame(null, config('tenancy.foo'));
|
||||||
|
tenancy()->init('foo.localhost');
|
||||||
|
$this->assertSame('bar', config('tenancy.foo'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function bootstrapped_event_works()
|
||||||
|
{
|
||||||
|
$uuid = tenant()->create('foo.localhost')['uuid'];
|
||||||
|
|
||||||
|
Tenancy::bootstrapped(function ($tenantManager) use ($uuid) {
|
||||||
|
if ($tenantManager->tenant['uuid'] === $uuid) {
|
||||||
|
config(['tenancy.foo' => 'bar']);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->assertSame(null, config('tenancy.foo'));
|
||||||
|
tenancy()->init('foo.localhost');
|
||||||
|
$this->assertSame('bar', config('tenancy.foo'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function ending_event_works()
|
||||||
|
{
|
||||||
|
$uuid = tenant()->create('foo.localhost')['uuid'];
|
||||||
|
|
||||||
|
Tenancy::ending(function ($tenantManager) use ($uuid) {
|
||||||
|
if ($tenantManager->tenant['uuid'] === $uuid) {
|
||||||
|
config(['tenancy.foo' => 'bar']);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->assertSame(null, config('tenancy.foo'));
|
||||||
|
tenancy()->init('foo.localhost');
|
||||||
|
$this->assertSame(null, config('tenancy.foo'));
|
||||||
|
tenancy()->end();
|
||||||
|
$this->assertSame('bar', config('tenancy.foo'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function ended_event_works()
|
||||||
|
{
|
||||||
|
$uuid = tenant()->create('foo.localhost')['uuid'];
|
||||||
|
|
||||||
|
Tenancy::ended(function ($tenantManager) use ($uuid) {
|
||||||
|
if ($tenantManager->tenant['uuid'] === $uuid) {
|
||||||
|
config(['tenancy.foo' => 'bar']);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->assertSame(null, config('tenancy.foo'));
|
||||||
|
tenancy()->init('foo.localhost');
|
||||||
|
$this->assertSame(null, config('tenancy.foo'));
|
||||||
|
tenancy()->end();
|
||||||
|
$this->assertSame('bar', config('tenancy.foo'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function event_returns_a_collection()
|
||||||
|
{
|
||||||
|
// Note: The event() method should not be called by your code.
|
||||||
|
tenancy()->bootstrapping(function ($tenancy) {
|
||||||
|
return ['database'];
|
||||||
|
});
|
||||||
|
tenancy()->bootstrapping(function ($tenancy) {
|
||||||
|
return ['redis', 'cache'];
|
||||||
|
});
|
||||||
|
|
||||||
|
$prevents = tenancy()->event('bootstrapping');
|
||||||
|
$this->assertEquals(collect(['database', 'redis', 'cache']), $prevents);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function database_can_be_reconnected_using_event_hooks()
|
||||||
|
{
|
||||||
|
config(['database.connections.tenantabc' => [
|
||||||
|
'driver' => 'sqlite',
|
||||||
|
'database' => database_path('some_special_database.sqlite'),
|
||||||
|
]]);
|
||||||
|
|
||||||
|
$uuid = Tenant::create('abc.localhost')['uuid'];
|
||||||
|
|
||||||
|
Tenancy::bootstrapping(function ($tenancy) use ($uuid) {
|
||||||
|
if ($tenancy->tenant['uuid'] === $uuid) {
|
||||||
|
$tenancy->database->useConnection('tenantabc');
|
||||||
|
return ['database'];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->assertNotSame('tenantabc', \DB::connection()->getConfig()['name']);
|
||||||
|
tenancy()->init('abc.localhost');
|
||||||
|
$this->assertSame('tenantabc', \DB::connection()->getConfig()['name']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Tests;
|
namespace Stancl\Tenancy\Tests;
|
||||||
|
|
||||||
use Tenancy;
|
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
use Stancl\Tenancy\Exceptions\CannotChangeUuidOrDomainException;
|
use Stancl\Tenancy\Exceptions\CannotChangeUuidOrDomainException;
|
||||||
|
|
@ -242,72 +241,4 @@ class TenantManagerTest extends TestCase
|
||||||
$this->expectException(CannotChangeUuidOrDomainException::class);
|
$this->expectException(CannotChangeUuidOrDomainException::class);
|
||||||
tenant()->put(['uuid' => 'foo']);
|
tenant()->put(['uuid' => 'foo']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
|
||||||
public function bootstrapping_event_works()
|
|
||||||
{
|
|
||||||
$uuid = tenant()->create('foo.localhost')['uuid'];
|
|
||||||
|
|
||||||
Tenancy::bootstrapping(function ($tenantManager) use ($uuid) {
|
|
||||||
if ($tenantManager->tenant['uuid'] === $uuid) {
|
|
||||||
config(['tenancy.foo' => 'bar']);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
$this->assertSame(null, config('tenancy.foo'));
|
|
||||||
tenancy()->init('foo.localhost');
|
|
||||||
$this->assertSame('bar', config('tenancy.foo'));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @test */
|
|
||||||
public function bootstrapped_event_works()
|
|
||||||
{
|
|
||||||
$uuid = tenant()->create('foo.localhost')['uuid'];
|
|
||||||
|
|
||||||
Tenancy::bootstrapped(function ($tenantManager) use ($uuid) {
|
|
||||||
if ($tenantManager->tenant['uuid'] === $uuid) {
|
|
||||||
config(['tenancy.foo' => 'bar']);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
$this->assertSame(null, config('tenancy.foo'));
|
|
||||||
tenancy()->init('foo.localhost');
|
|
||||||
$this->assertSame('bar', config('tenancy.foo'));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @test */
|
|
||||||
public function ending_event_works()
|
|
||||||
{
|
|
||||||
$uuid = tenant()->create('foo.localhost')['uuid'];
|
|
||||||
|
|
||||||
Tenancy::ending(function ($tenantManager) use ($uuid) {
|
|
||||||
if ($tenantManager->tenant['uuid'] === $uuid) {
|
|
||||||
config(['tenancy.foo' => 'bar']);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
$this->assertSame(null, config('tenancy.foo'));
|
|
||||||
tenancy()->init('foo.localhost');
|
|
||||||
$this->assertSame(null, config('tenancy.foo'));
|
|
||||||
tenancy()->end();
|
|
||||||
$this->assertSame('bar', config('tenancy.foo'));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @test */
|
|
||||||
public function ended_event_works()
|
|
||||||
{
|
|
||||||
$uuid = tenant()->create('foo.localhost')['uuid'];
|
|
||||||
|
|
||||||
Tenancy::ended(function ($tenantManager) use ($uuid) {
|
|
||||||
if ($tenantManager->tenant['uuid'] === $uuid) {
|
|
||||||
config(['tenancy.foo' => 'bar']);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
$this->assertSame(null, config('tenancy.foo'));
|
|
||||||
tenancy()->init('foo.localhost');
|
|
||||||
$this->assertSame(null, config('tenancy.foo'));
|
|
||||||
tenancy()->end();
|
|
||||||
$this->assertSame('bar', config('tenancy.foo'));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -101,6 +101,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'Tenancy' => \Stancl\Tenancy\TenancyFacade::class,
|
'Tenancy' => \Stancl\Tenancy\TenancyFacade::class,
|
||||||
|
'Tenant' => \Stancl\Tenancy\TenancyFacade::class,
|
||||||
'GlobalCache' => \Stancl\Tenancy\GlobalCacheFacade::class,
|
'GlobalCache' => \Stancl\Tenancy\GlobalCacheFacade::class,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue