1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-13 04:04:04 +00:00

Simplify Tenant contract (remove run method), move run logic to Tenancy, add generics

This commit is contained in:
Samuel Štancl 2024-03-01 08:42:11 +01:00
parent 32a063b834
commit af3b693dd1
9 changed files with 39 additions and 26 deletions

View file

@ -59,6 +59,31 @@ class Tenancy
event(new Events\TenancyInitialized($this));
}
/**
* Run a callback in the current tenant's context.
*
* This method is atomic and safely reverts to the previous context.
*
* @template T
* @param Closure(Tenant): T $callback
* @return T
*/
public function run(Tenant $tenant, Closure $callback): mixed
{
$originalTenant = $this->tenant;
$this->initialize($tenant);
$result = $callback($tenant);
if ($originalTenant) {
$this->initialize($originalTenant);
} else {
$this->end();
}
return $result;
}
public function end(): void
{
if (! $this->initialized) {