1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-05-06 19:44:03 +00:00

Merge branch 'master' into storage-listeners

This commit is contained in:
Samuel Štancl 2026-04-12 11:37:08 +02:00 committed by GitHub
commit dc659ed67a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 61 additions and 1 deletions

View file

@ -32,7 +32,7 @@
"league/flysystem-aws-s3-v3": "^3.12.2", "league/flysystem-aws-s3-v3": "^3.12.2",
"doctrine/dbal": "^3.6.0", "doctrine/dbal": "^3.6.0",
"spatie/valuestore": "^1.2.5", "spatie/valuestore": "^1.2.5",
"pestphp/pest": "^3.0", "pestphp/pest": "^4.0",
"larastan/larastan": "^3.0", "larastan/larastan": "^3.0",
"league/flysystem-path-prefixing": "^3.0" "league/flysystem-path-prefixing": "^3.0"
}, },

View file

@ -152,6 +152,26 @@ class Tenancy
$this->initialized = false; $this->initialized = false;
} }
/**
* End tenancy and initialize it again for the current tenant.
*
* This can be helpful when changing "dependencies" of bootstrappers such as
* attributes of the current tenant that are only read once, during bootstrap().
*
* If tenancy is not initialized, this method is a no-op.
*/
public function reinitialize(): void
{
if ($this->tenant === null) {
return;
}
$tenant = $this->tenant;
$this->end();
$this->initialize($tenant);
}
/** @return TenancyBootstrapper[] */ /** @return TenancyBootstrapper[] */
public function getBootstrappers(): array public function getBootstrappers(): array
{ {

View file

@ -103,6 +103,33 @@ test('central helper doesnt change tenancy state when called in central context'
expect(tenant())->toBeNull(); expect(tenant())->toBeNull();
}); });
test('reinitialize method does nothing in the central context', function () {
expect(tenancy()->initialized)->toBe(false);
expect(fn () => tenancy()->reinitialize())->not()->toThrow(\Throwable::class);
expect(tenancy()->initialized)->toBe(false);
});
test('reinitialize method runs bootstrappers again for the current tenant', function () {
config(['tenancy.bootstrappers' => [
ReinitBootstrapper::class,
]]);
tenancy()->initialize($tenant = Tenant::create(['reinit_bootstrapper_key' => 'foo']));
expect(tenant()->getKey())->toBe($tenant->getKey());
expect(app('tenancy_reinit_bootstrapper_key'))->toBe('foo');
$tenant->update(['reinit_bootstrapper_key' => 'bar']);
// Unchanged until we reinitialize...
expect(app('tenancy_reinit_bootstrapper_key'))->toBe('foo');
tenancy()->reinitialize();
expect(tenant()->getKey())->toBe($tenant->getKey());
expect(app('tenancy_reinit_bootstrapper_key'))->toBe('bar');
});
class MyBootstrapper implements TenancyBootstrapper class MyBootstrapper implements TenancyBootstrapper
{ {
public function bootstrap(\Stancl\Tenancy\Contracts\Tenant $tenant): void public function bootstrap(\Stancl\Tenancy\Contracts\Tenant $tenant): void
@ -115,3 +142,16 @@ class MyBootstrapper implements TenancyBootstrapper
app()->instance('tenancy_ended', true); app()->instance('tenancy_ended', true);
} }
} }
class ReinitBootstrapper implements TenancyBootstrapper
{
public function bootstrap(\Stancl\Tenancy\Contracts\Tenant $tenant): void
{
app()->instance('tenancy_reinit_bootstrapper_key', $tenant->getAttribute('reinit_bootstrapper_key'));
}
public function revert(): void
{
app()->instance('tenancy_reinit_bootstrapper_key', null);
}
}