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..e494ac69 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,9 @@ 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 006bcce2..9d5531bb 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 624b2180..b7188968 100644 --- a/src/Traits/BootstrapsTenancy.php +++ b/src/Traits/BootstrapsTenancy.php @@ -21,42 +21,55 @@ trait BootstrapsTenancy public function bootstrap() { - array_map(function ($listener) { - $listener($this); - }, $this->listeners['bootstrapping']); - + $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(); - array_map(function ($listener) { - $listener($this); - }, $this->listeners['bootstrapped']); + 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() { - array_map(function ($listener) { - $listener($this); - }, $this->listeners['ending']); + $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(); - array_map(function ($listener) { - $listener($this); - }, $this->listeners['ended']); + 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 index c54b322b..b4cb7455 100644 --- a/src/Traits/TenantManagerEvents.php +++ b/src/Traits/TenantManagerEvents.php @@ -2,6 +2,8 @@ namespace Stancl\Tenancy\Traits; +use Illuminate\Support\Collection; + trait TenantManagerEvents { /** @@ -67,4 +69,17 @@ trait TenantManagerEvents 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/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..1863a2cc --- /dev/null +++ b/tests/TenantManagerEventsTest.php @@ -0,0 +1,114 @@ +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']); + } +} \ No newline at end of file diff --git a/tests/TenantManagerTest.php b/tests/TenantManagerTest.php index faa2da3d..453730ea 100644 --- a/tests/TenantManagerTest.php +++ b/tests/TenantManagerTest.php @@ -2,7 +2,6 @@ namespace Stancl\Tenancy\Tests; -use Tenancy; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Storage; use Stancl\Tenancy\Exceptions\CannotChangeUuidOrDomainException; @@ -242,72 +241,4 @@ class TenantManagerTest extends TestCase $this->expectException(CannotChangeUuidOrDomainException::class); 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')); - } } diff --git a/tests/TestCase.php b/tests/TestCase.php index b5d28acd..0183cc5a 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -101,6 +101,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase { return [ 'Tenancy' => \Stancl\Tenancy\TenancyFacade::class, + 'Tenant' => \Stancl\Tenancy\TenancyFacade::class, 'GlobalCache' => \Stancl\Tenancy\GlobalCacheFacade::class, ]; }