mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-04 19:04:03 +00:00
merge
This commit is contained in:
commit
f183235992
9 changed files with 287 additions and 15 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,10 @@ 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.
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,8 @@ use Stancl\Tenancy\Exceptions\PhpRedisNotInstalledException;
|
||||||
|
|
||||||
trait BootstrapsTenancy
|
trait BootstrapsTenancy
|
||||||
{
|
{
|
||||||
|
use TenantManagerEvents;
|
||||||
|
|
||||||
public $originalSettings = [];
|
public $originalSettings = [];
|
||||||
/**
|
/**
|
||||||
* Was tenancy initialized/bootstrapped?
|
* Was tenancy initialized/bootstrapped?
|
||||||
|
|
@ -19,26 +21,55 @@ trait BootstrapsTenancy
|
||||||
|
|
||||||
public function bootstrap()
|
public function bootstrap()
|
||||||
{
|
{
|
||||||
|
$prevented = $this->event('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();
|
if (! $prevented->contains('redis')) {
|
||||||
|
if ($this->app['config']['tenancy.redis.tenancy']) {
|
||||||
|
$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()
|
||||||
{
|
{
|
||||||
|
$prevented = $this->event('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();
|
if (! $prevented->contains('redis')) {
|
||||||
|
if ($this->app['config']['tenancy.redis.tenancy']) {
|
||||||
|
$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()
|
||||||
|
|
|
||||||
85
src/Traits/TenantManagerEvents.php
Normal file
85
src/Traits/TenantManagerEvents.php
Normal file
|
|
@ -0,0 +1,85 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Traits;
|
||||||
|
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
|
trait TenantManagerEvents
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Event listeners.
|
||||||
|
*
|
||||||
|
* @var callable[][]
|
||||||
|
*/
|
||||||
|
protected $listeners = [
|
||||||
|
'bootstrapping' => [],
|
||||||
|
'bootstrapped' => [],
|
||||||
|
'ending' => [],
|
||||||
|
'ended' => [],
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a listener that will be executed before tenancy is bootstrapped.
|
||||||
|
*
|
||||||
|
* @param callable $callback
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function bootstrapping(callable $callback)
|
||||||
|
{
|
||||||
|
$this->listeners['bootstrapping'][] = $callback;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a listener that will be executed after tenancy is bootstrapped.
|
||||||
|
*
|
||||||
|
* @param callable $callback
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function bootstrapped(callable $callback)
|
||||||
|
{
|
||||||
|
$this->listeners['bootstrapped'][] = $callback;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a listener that will be executed before tenancy is ended.
|
||||||
|
*
|
||||||
|
* @param callable $callback
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function ending(callable $callback)
|
||||||
|
{
|
||||||
|
$this->listeners['ending'][] = $callback;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a listener that will be executed after tenancy is ended.
|
||||||
|
*
|
||||||
|
* @param callable $callback
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function ended(callable $callback)
|
||||||
|
{
|
||||||
|
$this->listeners['ended'][] = $callback;
|
||||||
|
|
||||||
|
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([]));
|
||||||
|
}
|
||||||
|
}
|
||||||
1
test
1
test
|
|
@ -1,4 +1,5 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
set -e
|
||||||
|
|
||||||
# for development
|
# for development
|
||||||
docker-compose up -d
|
docker-compose up -d
|
||||||
|
|
|
||||||
|
|
@ -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'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
137
tests/TenantManagerEventsTest.php
Normal file
137
tests/TenantManagerEventsTest.php
Normal file
|
|
@ -0,0 +1,137 @@
|
||||||
|
<?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']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function database_cannot_be_reconnected_without_using_prevents()
|
||||||
|
{
|
||||||
|
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('tenant', \DB::connection()->getConfig()['name']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -117,6 +117,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