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

Synced resources - proof of concept

This commit is contained in:
Samuel Štancl 2020-05-11 07:32:20 +02:00
parent 86a98b2bc8
commit daae67c0f7
8 changed files with 393 additions and 0 deletions

View file

@ -0,0 +1,14 @@
<?php
namespace Stancl\Tenancy\Contracts;
use Illuminate\Database\Eloquent\Collection;
// todo rename?
/**
* @property-read Tenant[]|Collection $tenants
*/
interface SyncMaster extends Syncable
{
public function tenants(); // Probably should return BelongsToMany
}

View file

@ -0,0 +1,12 @@
<?php
namespace Stancl\Tenancy\Contracts;
// todo add comments
interface Syncable
{
public function getGlobalIdentifierKeyName(): string;
public function getGlobalIdentifierKey(): string;
public function getCentralModelName(): string;
public function getSyncedAttributeNames(): array;
}

View file

@ -0,0 +1,16 @@
<?php
namespace Stancl\Tenancy\Database\Models\Concerns;
use Stancl\Tenancy\Contracts\Syncable;
use Stancl\Tenancy\Events\SyncedResourceSaved;
trait ResourceSyncing
{
public static function bootResourceSyncing()
{
static::saving(function (Syncable $model) {
event(new SyncedResourceSaved($model, tenant()));
});
}
}

View file

@ -0,0 +1,54 @@
<?php
namespace Stancl\Tenancy\Events\Listeners;
use Illuminate\Database\Eloquent\Model;
use Stancl\Tenancy\Contracts\SyncMaster;
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Events\SyncedResourceSaved;
class UpdateSyncedResource
{
public function handle(SyncedResourceSaved $event)
{
$syncedAttributes = $event->model->only($event->model->getSyncedAttributeNames());
// We update the central record only if the event comes from tenant context.
if ($event->tenant) {
/** @var Model|SyncMaster $centralModel */
$centralModel = $event->model->getCentralModelName()
::where($event->model->getGlobalIdentifierKeyName(), $event->model->getGlobalIdentifierKey())
->first();
// We disable events for this call, to avoid triggering this event & listener again.
$centralModel->withoutEvents(function () use ($centralModel, $syncedAttributes) {
$centralModel->update($syncedAttributes);
});
$tenants = $centralModel->tenants->except($event->tenant->getTenantKey());
} else {
$centralModel = $event->model;
$tenants = $centralModel->tenants;
}
foreach ($tenants as $tenant) {
// todo: performance optimization - $tenant->run() does tenancy()->end() after each call.
// we dont want that when we want to initialize for the next tenant afterwards rather than for the previous tenant
// so we should write a method like run() for running things on multiple tenants efficiently
$tenant->run(function () use ($event, $syncedAttributes) {
// Forget instance state and find the model,
// again in the current tenant's context.
/** @var Tenant|Model $model */
$localModel = $event->model::find($event->model->getKey());
// Also: We're syncing attributes, not columns, which is
// why we're using Eloquent instead of direct DB queries.
// We disable events for this call, to avoid triggering this event & listener again.
$localModel->update($syncedAttributes);
});
}
}
}

View file

@ -0,0 +1,22 @@
<?php
namespace Stancl\Tenancy\Events;
use Illuminate\Database\Eloquent\Model;
use Stancl\Tenancy\Contracts\Syncable;
use Stancl\Tenancy\Contracts\Tenant;
class SyncedResourceSaved
{
/** @var Syncable|Model */
public $model;
/** @var Tenant|Model|null */
public $tenant;
public function __construct(Syncable $model, ?Tenant $tenant)
{
$this->model = $model;
$this->tenant = $tenant;
}
}