mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 23:34:03 +00:00
parent
cc74e9f3ca
commit
a1270189da
3 changed files with 94 additions and 0 deletions
|
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||
namespace Stancl\Tenancy;
|
||||
|
||||
use ArrayAccess;
|
||||
use Closure;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Support\Traits\ForwardsCalls;
|
||||
|
|
@ -362,6 +363,27 @@ class Tenant implements ArrayAccess
|
|||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a closure inside the tenant's environment.
|
||||
*
|
||||
* @param Closure $closure
|
||||
* @return mixed
|
||||
*/
|
||||
public function run(Closure $closure)
|
||||
{
|
||||
$originalTenant = $this->manager->getTenant();
|
||||
|
||||
$this->manager->initializeTenancy($this);
|
||||
$result = $closure($this);
|
||||
$this->manager->endTenancy($this);
|
||||
|
||||
if ($originalTenant) {
|
||||
$this->manager->initializeTenancy($originalTenant);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function __get($key)
|
||||
{
|
||||
return $this->get($key);
|
||||
|
|
|
|||
|
|
@ -206,6 +206,10 @@ class TenantManager
|
|||
*/
|
||||
public function initializeTenancy(Tenant $tenant): self
|
||||
{
|
||||
if ($this->initialized) {
|
||||
$this->endTenancy();
|
||||
}
|
||||
|
||||
$this->setTenant($tenant);
|
||||
$this->bootstrapTenancy($tenant);
|
||||
$this->initialized = true;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue