mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 23:14:03 +00:00
Simplify Tenant contract (remove run method), move run logic to Tenancy, add generics
This commit is contained in:
parent
32a063b834
commit
af3b693dd1
9 changed files with 39 additions and 26 deletions
|
|
@ -41,7 +41,7 @@ class TenantDump extends DumpCommand
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
$tenant->run(fn () => parent::handle($connections, $dispatcher));
|
tenancy()->run($tenant, fn () => parent::handle($connections, $dispatcher));
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,6 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Contracts;
|
namespace Stancl\Tenancy\Contracts;
|
||||||
|
|
||||||
use Closure;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see \Stancl\Tenancy\Database\Models\Tenant
|
* @see \Stancl\Tenancy\Database\Models\Tenant
|
||||||
*
|
*
|
||||||
|
|
@ -24,7 +22,4 @@ interface Tenant
|
||||||
|
|
||||||
/** Set the value of an internal key. */
|
/** Set the value of an internal key. */
|
||||||
public function setInternal(string $key, mixed $value): static;
|
public function setInternal(string $key, mixed $value): static;
|
||||||
|
|
||||||
/** Run a callback in this tenant's environment. */
|
|
||||||
public function run(Closure $callback): mixed;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,21 +13,14 @@ trait TenantRun
|
||||||
* Run a callback in this tenant's context.
|
* Run a callback in this tenant's context.
|
||||||
*
|
*
|
||||||
* This method is atomic and safely reverts to the previous context.
|
* This method is atomic and safely reverts to the previous context.
|
||||||
|
*
|
||||||
|
* @template T
|
||||||
|
* @param Closure(Tenant): T $callback
|
||||||
|
* @return T
|
||||||
*/
|
*/
|
||||||
public function run(Closure $callback): mixed
|
public function run(Closure $callback): mixed
|
||||||
{
|
{
|
||||||
/** @var Tenant $this */
|
/** @var Tenant $this */
|
||||||
$originalTenant = tenant();
|
return tenancy()->run($this, $callback);
|
||||||
|
|
||||||
tenancy()->initialize($this);
|
|
||||||
$result = $callback($this);
|
|
||||||
|
|
||||||
if ($originalTenant) {
|
|
||||||
tenancy()->initialize($originalTenant);
|
|
||||||
} else {
|
|
||||||
tenancy()->end();
|
|
||||||
}
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ class CreateTenantStorage
|
||||||
{
|
{
|
||||||
public function handle(TenantCreated $event): void
|
public function handle(TenantCreated $event): void
|
||||||
{
|
{
|
||||||
$storage_path = $event->tenant->run(fn () => storage_path());
|
$storage_path = tenancy()->run($event->tenant, fn () => storage_path());
|
||||||
|
|
||||||
mkdir("$storage_path", 0777, true); // Create the tenant's folder inside storage/
|
mkdir("$storage_path", 0777, true); // Create the tenant's folder inside storage/
|
||||||
mkdir("$storage_path/framework/cache", 0777, true); // Create /framework/cache inside the tenant's storage (used for e.g. real-time facades)
|
mkdir("$storage_path/framework/cache", 0777, true); // Create /framework/cache inside the tenant's storage (used for e.g. real-time facades)
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,6 @@ class DeleteTenantStorage
|
||||||
{
|
{
|
||||||
public function handle(DeletingTenant $event): void
|
public function handle(DeletingTenant $event): void
|
||||||
{
|
{
|
||||||
File::deleteDirectory($event->tenant->run(fn () => storage_path()));
|
File::deleteDirectory(tenancy()->run($event->tenant, fn () => storage_path()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ class CreateTenantResource extends QueueableListener
|
||||||
{
|
{
|
||||||
$tenantResourceClass = $event->centralResource->getTenantModelName();
|
$tenantResourceClass = $event->centralResource->getTenantModelName();
|
||||||
|
|
||||||
$event->tenant->run(function () use ($event, $tenantResourceClass) {
|
tenancy()->run($event->tenant, function () use ($event, $tenantResourceClass) {
|
||||||
// Prevent $tenantResourceClass::create() from firing the SyncedResourceSaved event
|
// Prevent $tenantResourceClass::create() from firing the SyncedResourceSaved event
|
||||||
// Manually fire the SyncedResourceSavedInForeignDatabase event instead
|
// Manually fire the SyncedResourceSavedInForeignDatabase event instead
|
||||||
$tenantResourceClass::withoutEvents(function () use ($event, $tenantResourceClass) {
|
$tenantResourceClass::withoutEvents(function () use ($event, $tenantResourceClass) {
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,6 @@ class DeleteResourceInTenant extends QueueableListener
|
||||||
|
|
||||||
public function handle(CentralResourceDetachedFromTenant $event): void
|
public function handle(CentralResourceDetachedFromTenant $event): void
|
||||||
{
|
{
|
||||||
$event->tenant->run(fn () => $this->deleteSyncedResource($event->centralResource, true));
|
tenancy()->run($event->tenant, fn () => $this->deleteSyncedResource($event->centralResource, true));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,10 +4,10 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\ResourceSyncing;
|
namespace Stancl\Tenancy\ResourceSyncing;
|
||||||
|
|
||||||
use Stancl\Tenancy\Contracts\Tenant;
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\Pivot;
|
|
||||||
use Illuminate\Database\Eloquent\Relations\MorphPivot;
|
use Illuminate\Database\Eloquent\Relations\MorphPivot;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\Pivot;
|
||||||
|
use Stancl\Tenancy\Contracts\Tenant;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used on pivot models.
|
* Used on pivot models.
|
||||||
|
|
@ -31,7 +31,7 @@ trait TriggerSyncingEvents
|
||||||
* @var static&Pivot $pivot
|
* @var static&Pivot $pivot
|
||||||
* @var SyncMaster|null $centralResource
|
* @var SyncMaster|null $centralResource
|
||||||
* @var (Tenant&Model)|null $tenant
|
* @var (Tenant&Model)|null $tenant
|
||||||
*/
|
*/
|
||||||
[$centralResource, $tenant] = $pivot->getCentralResourceAndTenant();
|
[$centralResource, $tenant] = $pivot->getCentralResourceAndTenant();
|
||||||
|
|
||||||
if ($tenant && $centralResource?->shouldSync()) {
|
if ($tenant && $centralResource?->shouldSync()) {
|
||||||
|
|
@ -44,7 +44,7 @@ trait TriggerSyncingEvents
|
||||||
* @var static&Pivot $pivot
|
* @var static&Pivot $pivot
|
||||||
* @var SyncMaster|null $centralResource
|
* @var SyncMaster|null $centralResource
|
||||||
* @var (Tenant&Model)|null $tenant
|
* @var (Tenant&Model)|null $tenant
|
||||||
*/
|
*/
|
||||||
[$centralResource, $tenant] = $pivot->getCentralResourceAndTenant();
|
[$centralResource, $tenant] = $pivot->getCentralResourceAndTenant();
|
||||||
|
|
||||||
if ($tenant && $centralResource?->shouldSync()) {
|
if ($tenant && $centralResource?->shouldSync()) {
|
||||||
|
|
|
||||||
|
|
@ -59,6 +59,31 @@ class Tenancy
|
||||||
event(new Events\TenancyInitialized($this));
|
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
|
public function end(): void
|
||||||
{
|
{
|
||||||
if (! $this->initialized) {
|
if (! $this->initialized) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue