diff --git a/composer.json b/composer.json index 1bd0a0aa..78357d1c 100644 --- a/composer.json +++ b/composer.json @@ -41,6 +41,7 @@ ], "aliases": { "Tenancy": "Stancl\\Tenancy\\TenancyFacade", + "Tenant": "Stancl\\Tenancy\\TenancyFacade", "GlobalCache": "Stancl\\Tenancy\\GlobalCacheFacade" } } diff --git a/src/DatabaseManager.php b/src/DatabaseManager.php index 47bee9fc..68e0cde0 100644 --- a/src/DatabaseManager.php +++ b/src/DatabaseManager.php @@ -6,7 +6,7 @@ use Stancl\Tenancy\Jobs\QueuedTenantDatabaseCreator; use Stancl\Tenancy\Jobs\QueuedTenantDatabaseDeleter; use Illuminate\Database\DatabaseManager as BaseDatabaseManager; -class DatabaseManager +final class DatabaseManager { public $originalDefaultConnection; @@ -19,8 +19,7 @@ class DatabaseManager public function connect(string $database) { $this->createTenantConnection($database); - $this->database->setDefaultConnection('tenant'); - $this->database->reconnect('tenant'); + $this->useConnection('tenant'); } public function connectToTenant($tenant) @@ -105,4 +104,10 @@ class DatabaseManager $database_name = $this->getDriver() === 'sqlite' ? database_path($database_name) : $database_name; config()->set(['database.connections.tenant.database' => $database_name]); } + + public function useConnection(string $connection) + { + $this->database->setDefaultConnection($connection); + $this->database->reconnect($connection); + } } diff --git a/src/TenantManager.php b/src/TenantManager.php index 11dd0174..c90c8b88 100644 --- a/src/TenantManager.php +++ b/src/TenantManager.php @@ -7,7 +7,7 @@ use Stancl\Tenancy\Traits\BootstrapsTenancy; use Illuminate\Contracts\Foundation\Application; use Stancl\Tenancy\Exceptions\CannotChangeUuidOrDomainException; -class TenantManager +final class TenantManager { use BootstrapsTenancy; @@ -30,7 +30,7 @@ class TenantManager * * @var DatabaseManager */ - protected $database; + public $database; /** * Current tenant. diff --git a/src/Traits/BootstrapsTenancy.php b/src/Traits/BootstrapsTenancy.php index 5f2b74d3..b7188968 100644 --- a/src/Traits/BootstrapsTenancy.php +++ b/src/Traits/BootstrapsTenancy.php @@ -9,6 +9,8 @@ use Stancl\Tenancy\Exceptions\PhpRedisNotInstalledException; trait BootstrapsTenancy { + use TenantManagerEvents; + public $originalSettings = []; /** * Was tenancy initialized/bootstrapped? @@ -19,26 +21,55 @@ trait BootstrapsTenancy public function bootstrap() { + $prevented = $this->event('bootstrapping'); $this->initialized = true; - $this->switchDatabaseConnection(); - if ($this->app['config']['tenancy.redis.tenancy']) { - $this->setPhpRedisPrefix($this->app['config']['tenancy.redis.prefixed_connections']); + if (! $prevented->contains('database')) { + $this->switchDatabaseConnection(); } - $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() { + $prevented = $this->event('ending'); + $this->initialized = false; - $this->disconnectDatabase(); - if ($this->app['config']['tenancy.redis.tenancy']) { - $this->resetPhpRedisPrefix($this->app['config']['tenancy.redis.prefixed_connections']); + if (! $prevented->contains('database')) { + $this->disconnectDatabase(); } - $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() diff --git a/src/Traits/TenantManagerEvents.php b/src/Traits/TenantManagerEvents.php new file mode 100644 index 00000000..416ebd72 --- /dev/null +++ b/src/Traits/TenantManagerEvents.php @@ -0,0 +1,85 @@ + [], + '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([])); + } +} diff --git a/test b/test index ae14fb61..e8f29dd3 100755 --- a/test +++ b/test @@ -1,4 +1,5 @@ #!/bin/bash +set -e # for development docker-compose up -d diff --git a/tests/FacadeTest.php b/tests/FacadeTest.php index a218f0dd..45da854c 100644 --- a/tests/FacadeTest.php +++ b/tests/FacadeTest.php @@ -2,6 +2,7 @@ namespace Stancl\Tenancy\Tests; +use Tenant; use Tenancy; class FacadeTest extends TestCase @@ -15,4 +16,14 @@ class FacadeTest extends TestCase $this->assertSame('bar', Tenancy::get('foo')); $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')); + } } diff --git a/tests/TenantManagerEventsTest.php b/tests/TenantManagerEventsTest.php new file mode 100644 index 00000000..2fcd74b1 --- /dev/null +++ b/tests/TenantManagerEventsTest.php @@ -0,0 +1,137 @@ +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']); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index aa81f6ff..94d3feac 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -117,6 +117,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase { return [ 'Tenancy' => \Stancl\Tenancy\TenancyFacade::class, + 'Tenant' => \Stancl\Tenancy\TenancyFacade::class, 'GlobalCache' => \Stancl\Tenancy\GlobalCacheFacade::class, ]; }