diff --git a/src/Traits/BootstrapsTenancy.php b/src/Traits/BootstrapsTenancy.php index 5f2b74d3..cbfd9465 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,6 +21,10 @@ trait BootstrapsTenancy public function bootstrap() { + array_map(function ($listener) { + $listener($this); + }, $this->listeners['bootstrapping']); + $this->initialized = true; $this->switchDatabaseConnection(); @@ -27,6 +33,10 @@ trait BootstrapsTenancy } $this->tagCache(); $this->suffixFilesystemRootPaths(); + + array_map(function ($listener) { + $listener($this); + }, $this->listeners['bootstrapped']); } public function end() diff --git a/src/Traits/TenantManagerEvents.php b/src/Traits/TenantManagerEvents.php new file mode 100644 index 00000000..c54b322b --- /dev/null +++ b/src/Traits/TenantManagerEvents.php @@ -0,0 +1,70 @@ + [], + '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; + } +} diff --git a/tests/TenantManagerTest.php b/tests/TenantManagerTest.php index 453730ea..faa2da3d 100644 --- a/tests/TenantManagerTest.php +++ b/tests/TenantManagerTest.php @@ -2,6 +2,7 @@ namespace Stancl\Tenancy\Tests; +use Tenancy; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Storage; use Stancl\Tenancy\Exceptions\CannotChangeUuidOrDomainException; @@ -241,4 +242,72 @@ 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')); + } }