mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 20:34:03 +00:00
Refactor more old code and get tests to pass
This commit is contained in:
parent
c5377a16f7
commit
c32f229dd5
72 changed files with 425 additions and 531 deletions
18
src/Listeners/BootstrapTenancy.php
Normal file
18
src/Listeners/BootstrapTenancy.php
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Listeners;
|
||||
|
||||
use Stancl\Tenancy\Events\TenancyBootstrapped;
|
||||
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||
|
||||
class BootstrapTenancy
|
||||
{
|
||||
public function handle(TenancyInitialized $event)
|
||||
{
|
||||
foreach ($event->tenancy->getBootstrappers() as $bootstrapper) {
|
||||
$bootstrapper->bootstrap($event->tenancy->tenant);
|
||||
}
|
||||
|
||||
event(new TenancyBootstrapped($event->tenancy));
|
||||
}
|
||||
}
|
||||
95
src/Listeners/JobPipeline.php
Normal file
95
src/Listeners/JobPipeline.php
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Listeners;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
class JobPipeline implements ShouldQueue
|
||||
{
|
||||
/** @var bool */
|
||||
public static $queueByDefault = false;
|
||||
|
||||
/** @var callable[]|string[] */
|
||||
public $jobs;
|
||||
|
||||
/** @var callable|null */
|
||||
public $send;
|
||||
|
||||
/**
|
||||
* A value passed to the jobs. This is the return value of $send.
|
||||
*/
|
||||
public $passable;
|
||||
|
||||
/** @var bool */
|
||||
public $queue;
|
||||
|
||||
public function __construct($jobs, callable $send = null, bool $queue = null)
|
||||
{
|
||||
$this->jobs = $jobs;
|
||||
$this->send = $send ?? function ($event) {
|
||||
// If no $send callback is set, we'll just pass the event through the jobs.
|
||||
return $event;
|
||||
};
|
||||
$this->queue = $queue ?? static::$queueByDefault;
|
||||
}
|
||||
|
||||
/** @param callable[]|string[] $jobs */
|
||||
public static function make(array $jobs): self
|
||||
{
|
||||
return new static($jobs);
|
||||
}
|
||||
|
||||
public function send(callable $send): self
|
||||
{
|
||||
$this->send = $send;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function queue(bool $queue)
|
||||
{
|
||||
$this->queue = $queue;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
foreach ($this->jobs as $job) {
|
||||
app()->call([new $job(...$this->passable), 'handle']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a closure that can be used as a listener.
|
||||
*/
|
||||
public function toListener(): Closure
|
||||
{
|
||||
return function (...$args) {
|
||||
$executable = $this->executable($args);
|
||||
|
||||
if ($this->queue) {
|
||||
dispatch($executable);
|
||||
} else {
|
||||
dispatch_now($executable);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a serializable version of the current object.
|
||||
*/
|
||||
public function executable($listenerArgs): self
|
||||
{
|
||||
$clone = clone $this;
|
||||
|
||||
$passable = ($clone->send)(...$listenerArgs);
|
||||
$passable = is_array($passable) ? $passable : [$passable];
|
||||
|
||||
$clone->passable = $passable;
|
||||
unset($clone->send);
|
||||
|
||||
return $clone;
|
||||
}
|
||||
}
|
||||
26
src/Listeners/QueueableListener.php
Normal file
26
src/Listeners/QueueableListener.php
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Listeners;
|
||||
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
/**
|
||||
* @method handle(object $event)
|
||||
*/
|
||||
abstract class QueueableListener implements ShouldQueue
|
||||
{
|
||||
public static $shouldQueue = false;
|
||||
|
||||
public function shouldQueue($event)
|
||||
{
|
||||
if (static::$shouldQueue) {
|
||||
return true;
|
||||
} else {
|
||||
// The listener is not queued so we manually
|
||||
// pass the event to the handle() method.
|
||||
$this->handle($event);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
18
src/Listeners/RevertToCentralContext.php
Normal file
18
src/Listeners/RevertToCentralContext.php
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Listeners;
|
||||
|
||||
use Stancl\Tenancy\Events\RevertedToCentralContext;
|
||||
use Stancl\Tenancy\Events\TenancyEnded;
|
||||
|
||||
class RevertToCentralContext
|
||||
{
|
||||
public function handle(TenancyEnded $event)
|
||||
{
|
||||
foreach ($event->tenancy->getBootstrappers() as $bootstrapper) {
|
||||
$bootstrapper->revert();
|
||||
}
|
||||
|
||||
event(new RevertedToCentralContext($event->tenancy));
|
||||
}
|
||||
}
|
||||
121
src/Listeners/UpdateSyncedResource.php
Normal file
121
src/Listeners/UpdateSyncedResource.php
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Listeners;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\Pivot;
|
||||
use Stancl\Tenancy\Contracts\SyncMaster;
|
||||
use Stancl\Tenancy\Events\SyncedResourceChangedInForeignDatabase;
|
||||
use Stancl\Tenancy\Events\SyncedResourceSaved;
|
||||
use Stancl\Tenancy\Exceptions\ModelNotSyncMaster;
|
||||
|
||||
class UpdateSyncedResource extends QueueableListener
|
||||
{
|
||||
public static $shouldQueue = false;
|
||||
|
||||
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) {
|
||||
$tenants = $this->updateResourceInCentralDatabaseAndGetTenants($event, $syncedAttributes);
|
||||
} else {
|
||||
$tenants = $this->getTenantsForCentralModel($event->model);
|
||||
}
|
||||
|
||||
$this->updateResourceInTenantDatabases($tenants, $event, $syncedAttributes);
|
||||
}
|
||||
|
||||
protected function getTenantsForCentralModel($centralModel)
|
||||
{
|
||||
if (! $centralModel instanceof SyncMaster) {
|
||||
// If we're trying to use a tenant User model instead of the central User model, for example.
|
||||
throw new ModelNotSyncMaster(get_class($centralModel));
|
||||
}
|
||||
|
||||
/** @var SyncMaster|Model $centralModel */
|
||||
|
||||
// Since this model is "dirty" (taken by reference from the event), it might have the tenants
|
||||
// relationship already loaded and cached. For this reason, we refresh the relationship.
|
||||
$centralModel->load('tenants');
|
||||
|
||||
return $centralModel->tenants;
|
||||
}
|
||||
|
||||
protected function updateResourceInCentralDatabaseAndGetTenants($event, $syncedAttributes)
|
||||
{
|
||||
/** @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.
|
||||
$event->model->getCentralModelName()::withoutEvents(function () use (&$centralModel, $syncedAttributes, $event) {
|
||||
if ($centralModel) {
|
||||
$centralModel->update($syncedAttributes);
|
||||
event(new SyncedResourceChangedInForeignDatabase($event->model, null));
|
||||
} else {
|
||||
// If the resource doesn't exist at all in the central DB,we create
|
||||
// the record with all attributes, not just the synced ones.
|
||||
$centralModel = $event->model->getCentralModelName()::create($event->model->getAttributes());
|
||||
event(new SyncedResourceChangedInForeignDatabase($event->model, null));
|
||||
}
|
||||
});
|
||||
|
||||
// If the model was just created, the mapping of the tenant to the user likely doesn't exist, so we create it.
|
||||
$currentTenantMapping = function ($model) use ($event) {
|
||||
return ((string) $model->pivot->tenant_id) === ((string) $event->tenant->getTenantKey());
|
||||
};
|
||||
|
||||
$mappingExists = $centralModel->tenants->contains($currentTenantMapping);
|
||||
|
||||
if (!$mappingExists) {
|
||||
// Here we should call TenantPivot, but we call general Pivot, so that this works
|
||||
// even if people use their own pivot model that is not based on our TenantPivot
|
||||
Pivot::withoutEvents(function () use ($centralModel, $event) {
|
||||
$centralModel->tenants()->attach($event->tenant->getTenantKey());
|
||||
});
|
||||
}
|
||||
|
||||
return $centralModel->tenants->filter(function ($model) use ($currentTenantMapping) {
|
||||
// Remove the mapping for the current tenant.
|
||||
return !$currentTenantMapping($model);
|
||||
});
|
||||
}
|
||||
|
||||
protected function updateResourceInTenantDatabases($tenants, $event, $syncedAttributes)
|
||||
{
|
||||
tenancy()->runForMultiple($tenants, function ($tenant) use ($event, $syncedAttributes) {
|
||||
// Forget instance state and find the model,
|
||||
// again in the current tenant's context.
|
||||
|
||||
$eventModel = $event->model;
|
||||
|
||||
if ($eventModel instanceof SyncMaster) {
|
||||
// If event model comes from central DB, we get the tenant model name to run the query
|
||||
$localModelClass = $eventModel->getTenantModelName();
|
||||
} else {
|
||||
$localModelClass = get_class($eventModel);
|
||||
}
|
||||
|
||||
/** @var Model|null */
|
||||
$localModel = $localModelClass::firstWhere($event->model->getGlobalIdentifierKeyName(), $event->model->getGlobalIdentifierKey());
|
||||
|
||||
// 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.
|
||||
$localModelClass::withoutEvents(function () use ($localModelClass, $localModel, $syncedAttributes, $eventModel, $tenant) {
|
||||
if ($localModel) {
|
||||
$localModel->update($syncedAttributes);
|
||||
} else {
|
||||
// When creating, we use all columns, not just the synced ones.
|
||||
$localModel = $localModelClass::create($eventModel->getAttributes());
|
||||
}
|
||||
|
||||
event(new SyncedResourceChangedInForeignDatabase($localModel, $tenant));
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue