mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-17 06:24:04 +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
|
|
@ -1,11 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Database\Models\Concerns;
|
||||
|
||||
trait CentralConnection
|
||||
{
|
||||
public function getConnectionName()
|
||||
{
|
||||
return config('tenancy.central_connection');
|
||||
}
|
||||
}
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Database\Models\Concerns;
|
||||
|
||||
use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
|
||||
|
||||
trait GeneratesIds
|
||||
{
|
||||
public static function bootGeneratesIds()
|
||||
{
|
||||
static::creating(function (self $model) {
|
||||
if (! $model->id && app()->bound(UniqueIdentifierGenerator::class)) {
|
||||
$model->id = app(UniqueIdentifierGenerator::class)->generate($model);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -1,117 +0,0 @@
|
|||
<?php
|
||||
|
||||
// todo move namespace one dir above
|
||||
namespace Stancl\Tenancy\Database\Models\Concerns;
|
||||
|
||||
// todo rename
|
||||
trait HasADataColumn
|
||||
{
|
||||
public static $priorityListeners = [];
|
||||
|
||||
/**
|
||||
* We need this property, because both created & saved event listeners
|
||||
* decode the data (to take precedence before other created & saved)
|
||||
* listeners, but we don't want the dadta to be decoded twice.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $dataEncodingStatus = 'decoded'; // todo write tests for this
|
||||
|
||||
public static function bootHasADataColumn()
|
||||
{
|
||||
$encode = function (self $model) {
|
||||
if ($model->dataEncodingStatus === 'encoded') {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($model->getAttributes() as $key => $value) {
|
||||
if (! in_array($key, static::getCustomColums())) {
|
||||
$current = $model->getAttribute(static::getDataColumn()) ?? [];
|
||||
|
||||
$model->setAttribute(static::getDataColumn(), array_merge($current, [
|
||||
$key => $value,
|
||||
]));
|
||||
|
||||
unset($model->attributes[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
$model->dataEncodingStatus = 'encoded';
|
||||
};
|
||||
|
||||
$decode = function (self $model) {
|
||||
if ($model->dataEncodingStatus === 'decoded') {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($model->getAttribute(static::getDataColumn()) ?? [] as $key => $value) {
|
||||
$model->setAttribute($key, $value);
|
||||
}
|
||||
|
||||
$model->setAttribute(static::getDataColumn(), null);
|
||||
|
||||
$model->dataEncodingStatus = 'decoded';
|
||||
};
|
||||
|
||||
static::registerPriorityListener('retrieved', $decode);
|
||||
|
||||
static::registerPriorityListener('saving', $encode);
|
||||
static::registerPriorityListener('creating', $encode);
|
||||
static::registerPriorityListener('updating', $encode);
|
||||
static::registerPriorityListener('saved', $decode);
|
||||
static::registerPriorityListener('created', $decode);
|
||||
static::registerPriorityListener('updated', $decode);
|
||||
}
|
||||
|
||||
protected function fireModelEvent($event, $halt = true)
|
||||
{
|
||||
$this->runPriorityListeners($event, $halt);
|
||||
|
||||
return parent::fireModelEvent($event, $halt);
|
||||
}
|
||||
|
||||
public function runPriorityListeners($event, $halt = true)
|
||||
{
|
||||
$listeners = static::$priorityListeners[$event] ?? [];
|
||||
|
||||
if (! $event) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($listeners as $listener) {
|
||||
if (is_string($listener)) {
|
||||
$listener = app($listener);
|
||||
$handle = [$listener, 'handle'];
|
||||
} else {
|
||||
$handle = $listener;
|
||||
}
|
||||
|
||||
$handle($this);
|
||||
}
|
||||
}
|
||||
|
||||
public static function registerPriorityListener(string $event, callable $callback)
|
||||
{
|
||||
static::$priorityListeners[$event][] = $callback;
|
||||
}
|
||||
|
||||
public function getCasts()
|
||||
{
|
||||
return array_merge(parent::getCasts(), [
|
||||
static::getDataColumn() => 'array',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of the column that stores additional data.
|
||||
*/
|
||||
public static function getDataColumn(): string
|
||||
{
|
||||
return 'data';
|
||||
}
|
||||
|
||||
public static function getCustomColums(): array
|
||||
{
|
||||
return array_merge(['id'], config('tenancy.custom_columns'));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Database\Models\Concerns;
|
||||
|
||||
trait HasDomains
|
||||
{
|
||||
public function domains()
|
||||
{
|
||||
return $this->hasMany(config('tenancy.domain_model'));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Database\Models\Concerns;
|
||||
|
||||
/**
|
||||
* @property-read string $primary_domain_hostname
|
||||
*/
|
||||
trait PrimaryDomain
|
||||
{
|
||||
// This string should usually come from a relationship implemented by you.
|
||||
abstract public function getPrimaryDomainHostnameAttribute(): string;
|
||||
|
||||
public function route($route, $parameters = [], $absolute = true)
|
||||
{
|
||||
return tenant_route($this->primary_domain_hostname, $route, $parameters, $absolute);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Database\Models\Concerns;
|
||||
|
||||
use Stancl\Tenancy\Contracts\Syncable;
|
||||
use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
|
||||
use Stancl\Tenancy\Events\SyncedResourceSaved;
|
||||
|
||||
trait ResourceSyncing
|
||||
{
|
||||
public static function bootResourceSyncing()
|
||||
{
|
||||
static::saved(function (Syncable $model) {
|
||||
/** @var ResourceSyncing $model */
|
||||
|
||||
$model->triggerSyncEvent();
|
||||
});
|
||||
|
||||
static::creating(function (self $model) {
|
||||
if (! $model->getAttribute($model->getGlobalIdentifierKeyName()) && app()->bound(UniqueIdentifierGenerator::class)) {
|
||||
$model->setAttribute(
|
||||
$model->getGlobalIdentifierKeyName(),
|
||||
app(UniqueIdentifierGenerator::class)->generate($model)
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function triggerSyncEvent()
|
||||
{
|
||||
/** @var Syncable $this */
|
||||
event(new SyncedResourceSaved($this, tenant()));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Database\Models\Concerns;
|
||||
|
||||
trait TenantConnection
|
||||
{
|
||||
public function getConnectionName()
|
||||
{
|
||||
return 'tenant';
|
||||
}
|
||||
}
|
||||
|
|
@ -2,18 +2,30 @@
|
|||
|
||||
namespace Stancl\Tenancy\Database\Models;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Stancl\Tenancy\DatabaseConfig;
|
||||
use Stancl\Tenancy\Events;
|
||||
use Stancl\Tenancy\Contracts;
|
||||
use Stancl\Tenancy\Database\Concerns;
|
||||
|
||||
// todo @property
|
||||
class Tenant extends Model implements Contracts\TenantWithDatabase
|
||||
/**
|
||||
* @property string|int $id
|
||||
* @property Carbon $created_at
|
||||
* @property Carbon $updated_at
|
||||
* @property array $data
|
||||
*/
|
||||
class Tenant extends Model implements Contracts\TenantWithDatabase // todo base model that isn't TenantWithDatabase & domains
|
||||
{
|
||||
use Concerns\CentralConnection, Concerns\HasADataColumn, Concerns\GeneratesIds, Concerns\HasADataColumn {
|
||||
use Concerns\CentralConnection,
|
||||
Concerns\HasADataColumn,
|
||||
Concerns\GeneratesIds,
|
||||
Concerns\HasADataColumn,
|
||||
Concerns\HasDomains {
|
||||
Concerns\HasADataColumn::getCasts as dataColumnCasts;
|
||||
}
|
||||
|
||||
protected $table = 'tenants';
|
||||
public $primaryKey = 'id';
|
||||
public $guarded = [];
|
||||
|
||||
|
|
@ -34,11 +46,6 @@ class Tenant extends Model implements Contracts\TenantWithDatabase
|
|||
]);
|
||||
}
|
||||
|
||||
public function getIncrementing()
|
||||
{
|
||||
return config('tenancy.id_generator') === null;
|
||||
}
|
||||
|
||||
public static function internalPrefix(): string
|
||||
{
|
||||
return config('tenancy.internal_prefix');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue