mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 23:24:03 +00:00
merge
This commit is contained in:
commit
1e75221e12
26 changed files with 680 additions and 14 deletions
90
src/Database/Concerns/HasPending.php
Normal file
90
src/Database/Concerns/HasPending.php
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Database\Concerns;
|
||||
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
use Stancl\Tenancy\Events\PullingPendingTenant;
|
||||
use Stancl\Tenancy\Events\PendingTenantPulled;
|
||||
use Stancl\Tenancy\Events\CreatingPendingTenant;
|
||||
use Stancl\Tenancy\Events\PendingTenantCreated;
|
||||
|
||||
/**
|
||||
* @property $pending_since
|
||||
*
|
||||
* @method static static|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder withPending(bool $withPending = true)
|
||||
* @method static static|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder onlyPending()
|
||||
* @method static static|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder withoutPending()
|
||||
*/
|
||||
trait HasPending
|
||||
{
|
||||
/**
|
||||
* Boot the has pending trait for a model.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function bootHasPending()
|
||||
{
|
||||
static::addGlobalScope(new PendingScope());
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the has pending trait for an instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function initializeHasPending()
|
||||
{
|
||||
$this->casts['pending_since'] = 'timestamp';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determine if the model instance is in a pending state.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function pending()
|
||||
{
|
||||
return !is_null($this->pending_since);
|
||||
}
|
||||
|
||||
public static function createPending($attributes = []): void
|
||||
{
|
||||
$tenant = static::create($attributes);
|
||||
|
||||
event(new CreatingPendingTenant($tenant));
|
||||
|
||||
// We add the pending value only after the model has then been created.
|
||||
// this ensures the model is not marked as pending until the migrations, seeders, etc. are done
|
||||
$tenant->update([
|
||||
'pending_since' => now()->timestamp
|
||||
]);
|
||||
|
||||
event(new PendingTenantCreated($tenant));
|
||||
}
|
||||
|
||||
public static function pullPendingTenant(bool $firstOrCreate = false): ?Tenant
|
||||
{
|
||||
if (!static::onlyPending()->exists()) {
|
||||
if (!$firstOrCreate) {
|
||||
return null;
|
||||
}
|
||||
static::createPending();
|
||||
}
|
||||
|
||||
// At this point we can guarantee a pending tenant is free and can be called.
|
||||
$tenant = static::onlyPending()->first();
|
||||
|
||||
event(new PullingPendingTenant($tenant));
|
||||
|
||||
$tenant->update([
|
||||
'pending_since' => null
|
||||
]);
|
||||
|
||||
event(new PendingTenantPulled($tenant));
|
||||
|
||||
return $tenant;
|
||||
}
|
||||
}
|
||||
101
src/Database/Concerns/PendingScope.php
Normal file
101
src/Database/Concerns/PendingScope.php
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Database\Concerns;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Scope;
|
||||
|
||||
class PendingScope implements Scope
|
||||
{
|
||||
|
||||
/**
|
||||
* All of the extensions to be added to the builder.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $extensions = ['WithPending', 'WithoutPending', 'OnlyPending'];
|
||||
|
||||
/**
|
||||
* Apply the scope to a given Eloquent query builder.
|
||||
*
|
||||
* @param Builder $builder
|
||||
* @param Model $model
|
||||
* @return void
|
||||
*/
|
||||
public function apply(Builder $builder, Model $model)
|
||||
{
|
||||
$builder->when(!config('tenancy.pending.include_in_queries'), function (Builder $builder){
|
||||
$builder->whereNull('data->pending_since');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Extend the query builder with the needed functions.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $builder
|
||||
* @return void
|
||||
*/
|
||||
public function extend(Builder $builder)
|
||||
{
|
||||
foreach ($this->extensions as $extension) {
|
||||
$this->{"add{$extension}"}($builder);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Add the with-pending extension to the builder.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $builder
|
||||
* @return void
|
||||
*/
|
||||
protected function addWithPending(Builder $builder)
|
||||
{
|
||||
$builder->macro('withPending', function (Builder $builder, $withPending = true) {
|
||||
if (! $withPending) {
|
||||
return $builder->withoutPending();
|
||||
}
|
||||
|
||||
return $builder->withoutGlobalScope($this);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the without-pending extension to the builder.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $builder
|
||||
* @return void
|
||||
*/
|
||||
protected function addWithoutPending(Builder $builder)
|
||||
{
|
||||
$builder->macro('withoutPending', function (Builder $builder) {
|
||||
|
||||
// Only use whereNull('data->pending_since') when Laravel 6 support is dropped
|
||||
// Issue fixed in Laravel 7 https://github.com/laravel/framework/pull/32417
|
||||
$builder->withoutGlobalScope($this)
|
||||
->where('data->pending_since', 'like', 'null')
|
||||
->orWhereNull('data');
|
||||
|
||||
return $builder;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the only-pending extension to the builder.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $builder
|
||||
* @return void
|
||||
*/
|
||||
protected function addOnlyPending(Builder $builder)
|
||||
{
|
||||
$builder->macro('onlyPending', function (Builder $builder) {
|
||||
|
||||
// Use whereNotNull when Laravel 6 is dropped
|
||||
// Issue fixed in Laravel 7 https://github.com/laravel/framework/pull/32417
|
||||
$builder->withoutGlobalScope($this)->where('data->pending_since', 'not like', 'null');
|
||||
|
||||
return $builder;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -26,7 +26,8 @@ class Tenant extends Model implements Contracts\Tenant
|
|||
Concerns\HasDataColumn,
|
||||
Concerns\HasInternalKeys,
|
||||
Concerns\TenantRun,
|
||||
Concerns\InvalidatesResolverCache;
|
||||
Concerns\InvalidatesResolverCache,
|
||||
Concerns\HasPending;
|
||||
|
||||
protected $table = 'tenants';
|
||||
protected $primaryKey = 'id';
|
||||
|
|
|
|||
|
|
@ -16,9 +16,9 @@ use Stancl\Tenancy\Contracts\Tenant;
|
|||
*/
|
||||
class TenantCollection extends Collection
|
||||
{
|
||||
public function runForEach(callable $callable): self
|
||||
public function runForEach(callable $callable, bool $withPending = null): self
|
||||
{
|
||||
tenancy()->runForMultiple($this->items, $callable);
|
||||
tenancy()->runForMultiple($this->items, $callable, $withPending);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue