mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 18:34:04 +00:00
Add runGlobal helper
Signed-off-by: michael lundbøl <michael.lundboel@gmail.com>
This commit is contained in:
parent
411d486282
commit
e5700e7f72
2 changed files with 75 additions and 0 deletions
|
|
@ -85,6 +85,27 @@ class Tenancy
|
||||||
return array_map('app', $resolve($this->tenant));
|
return array_map('app', $resolve($this->tenant));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param callable $callback
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function runGlobal(callable $callback)
|
||||||
|
{
|
||||||
|
$oldTenant = $this->tenant;
|
||||||
|
|
||||||
|
if ($this->initialized) {
|
||||||
|
$this->end();
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = $callback();
|
||||||
|
|
||||||
|
if ($oldTenant) {
|
||||||
|
$this->initialize($oldTenant);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
public function query(): Builder
|
public function query(): Builder
|
||||||
{
|
{
|
||||||
return $this->model()->query();
|
return $this->model()->query();
|
||||||
|
|
|
||||||
|
|
@ -71,10 +71,58 @@ class AutomaticModeTest extends TestCase
|
||||||
|
|
||||||
$this->assertSame('foobar', app('tenancy_initialized_for_tenant'));
|
$this->assertSame('foobar', app('tenancy_initialized_for_tenant'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function running_the_global_tenancy_helper_with_tenant_already_initialized()
|
||||||
|
{
|
||||||
|
MyBootstrapper::$revertedCallCount = 0;
|
||||||
|
GlobalRun::$count = 0;
|
||||||
|
|
||||||
|
config(['tenancy.bootstrappers' => [
|
||||||
|
MyBootstrapper::class,
|
||||||
|
]]);
|
||||||
|
|
||||||
|
$tenant = Tenant::create([
|
||||||
|
'id' => 'acme',
|
||||||
|
]);
|
||||||
|
|
||||||
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
|
$this->assertSame('acme', app('tenancy_initialized_for_tenant'));
|
||||||
|
|
||||||
|
tenancy()->runGlobal(function () {
|
||||||
|
GlobalRun::$count = 1;
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->assertSame(1, MyBootstrapper::$revertedCallCount);
|
||||||
|
$this->assertSame(1, GlobalRun::$count);
|
||||||
|
$this->assertSame('acme', app('tenancy_initialized_for_tenant'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function running_the_global_tenancy_helper_with_tenant_not_already_initialized()
|
||||||
|
{
|
||||||
|
MyBootstrapper::$revertedCallCount = 0;
|
||||||
|
GlobalRun::$count = 0;
|
||||||
|
|
||||||
|
config(['tenancy.bootstrappers' => [
|
||||||
|
MyBootstrapper::class,
|
||||||
|
]]);
|
||||||
|
|
||||||
|
tenancy()->runGlobal(function () {
|
||||||
|
GlobalRun::$count = 1;
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->assertSame(0, MyBootstrapper::$revertedCallCount);
|
||||||
|
$this->assertSame(1, GlobalRun::$count);
|
||||||
|
$this->assertFalse(app()->bound('tenancy_initialized_for_tenant'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class MyBootstrapper implements TenancyBootstrapper
|
class MyBootstrapper implements TenancyBootstrapper
|
||||||
{
|
{
|
||||||
|
public static $revertedCallCount = 0;
|
||||||
|
|
||||||
public function bootstrap(\Stancl\Tenancy\Contracts\Tenant $tenant)
|
public function bootstrap(\Stancl\Tenancy\Contracts\Tenant $tenant)
|
||||||
{
|
{
|
||||||
app()->instance('tenancy_initialized_for_tenant', $tenant->getTenantKey());
|
app()->instance('tenancy_initialized_for_tenant', $tenant->getTenantKey());
|
||||||
|
|
@ -82,6 +130,12 @@ class MyBootstrapper implements TenancyBootstrapper
|
||||||
|
|
||||||
public function revert()
|
public function revert()
|
||||||
{
|
{
|
||||||
|
static::$revertedCallCount++;
|
||||||
app()->instance('tenancy_ended', true);
|
app()->instance('tenancy_ended', true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class GlobalRun {
|
||||||
|
|
||||||
|
public static $count = 0;
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue