mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 12:24:04 +00:00
Syncing: move global ID generation logic to an overridable method
Also make all resource syncing-related listener closures static. Also correct return type for getGlobalIdentifierKey to string|int. (We intentionally do not support returning null like many other "get x key" methods would since such a case might break resource syncing logic. This is also why we use inline getAttribute() in the creating listener instead of calling the method.)
This commit is contained in:
parent
a75528e228
commit
cbd2f45737
2 changed files with 21 additions and 14 deletions
|
|
@ -20,35 +20,32 @@ trait ResourceSyncing
|
||||||
{
|
{
|
||||||
public static function bootResourceSyncing(): void
|
public static function bootResourceSyncing(): void
|
||||||
{
|
{
|
||||||
static::saved(function (Syncable&Model $model) {
|
static::saved(static function (Syncable&Model $model) {
|
||||||
if ($model->shouldSync() && ($model->wasRecentlyCreated || $model->wasChanged($model->getSyncedAttributeNames()))) {
|
if ($model->shouldSync() && ($model->wasRecentlyCreated || $model->wasChanged($model->getSyncedAttributeNames()))) {
|
||||||
$model->triggerSyncEvent();
|
$model->triggerSyncEvent();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
static::deleted(function (Syncable&Model $model) {
|
static::deleted(static function (Syncable&Model $model) {
|
||||||
if ($model->shouldSync()) {
|
if ($model->shouldSync()) {
|
||||||
$model->triggerDeleteEvent();
|
$model->triggerDeleteEvent();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
static::creating(function (Syncable&Model $model) {
|
static::creating(static function (Syncable&Model $model) {
|
||||||
if (! $model->getAttribute($model->getGlobalIdentifierKeyName()) && app()->bound(UniqueIdentifierGenerator::class)) {
|
if (! $model->getAttribute($model->getGlobalIdentifierKeyName())) {
|
||||||
$model->setAttribute(
|
$model->generateGlobalIdentifierKey();
|
||||||
$model->getGlobalIdentifierKeyName(),
|
|
||||||
app(UniqueIdentifierGenerator::class)->generate($model)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (in_array(SoftDeletes::class, class_uses_recursive(static::class), true)) {
|
if (in_array(SoftDeletes::class, class_uses_recursive(static::class), true)) {
|
||||||
static::forceDeleting(function (Syncable&Model $model) {
|
static::forceDeleting(static function (Syncable&Model $model) {
|
||||||
if ($model->shouldSync()) {
|
if ($model->shouldSync()) {
|
||||||
$model->triggerDeleteEvent(true);
|
$model->triggerDeleteEvent(true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
static::restoring(function (Syncable&Model $model) {
|
static::restoring(static function (Syncable&Model $model) {
|
||||||
if ($model->shouldSync()) {
|
if ($model->shouldSync()) {
|
||||||
$model->triggerRestoredEvent();
|
$model->triggerRestoredEvent();
|
||||||
}
|
}
|
||||||
|
|
@ -119,8 +116,18 @@ trait ResourceSyncing
|
||||||
return 'global_id';
|
return 'global_id';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getGlobalIdentifierKey(): string
|
public function getGlobalIdentifierKey(): string|int
|
||||||
{
|
{
|
||||||
return $this->getAttribute($this->getGlobalIdentifierKeyName());
|
return $this->getAttribute($this->getGlobalIdentifierKeyName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function generateGlobalIdentifierKey(): void
|
||||||
|
{
|
||||||
|
if (! app()->bound(UniqueIdentifierGenerator::class)) return;
|
||||||
|
|
||||||
|
$this->setAttribute(
|
||||||
|
$this->getGlobalIdentifierKeyName(),
|
||||||
|
app(UniqueIdentifierGenerator::class)->generate($this),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,14 +20,14 @@ trait TriggerSyncingEvents
|
||||||
{
|
{
|
||||||
public static function bootTriggerSyncingEvents(): void
|
public static function bootTriggerSyncingEvents(): void
|
||||||
{
|
{
|
||||||
static::saving(function (self $pivot) {
|
static::saving(static function (self $pivot) {
|
||||||
// Try getting the central resource to see if it is available
|
// Try getting the central resource to see if it is available
|
||||||
// If it is not available, throw an exception to interrupt the saving process
|
// If it is not available, throw an exception to interrupt the saving process
|
||||||
// And prevent creating a pivot record without a central resource
|
// And prevent creating a pivot record without a central resource
|
||||||
$pivot->getCentralResourceAndTenant();
|
$pivot->getCentralResourceAndTenant();
|
||||||
});
|
});
|
||||||
|
|
||||||
static::saved(function (self $pivot) {
|
static::saved(static function (self $pivot) {
|
||||||
/**
|
/**
|
||||||
* @var static&Pivot $pivot
|
* @var static&Pivot $pivot
|
||||||
* @var SyncMaster|null $centralResource
|
* @var SyncMaster|null $centralResource
|
||||||
|
|
@ -40,7 +40,7 @@ trait TriggerSyncingEvents
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
static::deleting(function (self $pivot) {
|
static::deleting(static function (self $pivot) {
|
||||||
/**
|
/**
|
||||||
* @var static&Pivot $pivot
|
* @var static&Pivot $pivot
|
||||||
* @var SyncMaster|null $centralResource
|
* @var SyncMaster|null $centralResource
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue