mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 23:34:03 +00:00
Merge branch 'master' into shared-users
This commit is contained in:
commit
8915297c30
85 changed files with 880 additions and 3280 deletions
|
|
@ -6,9 +6,24 @@ 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()) ?? [];
|
||||
|
|
@ -20,19 +35,64 @@ trait HasADataColumn
|
|||
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::saving($encode);
|
||||
static::saved($decode);
|
||||
static::retrieved($decode);
|
||||
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()
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ use Stancl\Tenancy\Contracts;
|
|||
// todo @property
|
||||
class Tenant extends Model implements Contracts\TenantWithDatabase
|
||||
{
|
||||
use Concerns\CentralConnection, Concerns\HasADataColumn, Concerns\GeneratesIds, Concerns\HasADataColumn {
|
||||
use Concerns\CentralConnection, Concerns\HasADataColumn, Concerns\GeneratesIds, Concerns\HasADataColumn {
|
||||
Concerns\HasADataColumn::getCasts as dataColumnCasts;
|
||||
}
|
||||
|
||||
|
|
@ -41,14 +41,11 @@ class Tenant extends Model implements Contracts\TenantWithDatabase
|
|||
|
||||
public static function internalPrefix(): string
|
||||
{
|
||||
return config('tenancy.internal_column_prefix');
|
||||
return config('tenancy.internal_prefix');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an internal key.
|
||||
*
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function getInternal(string $key)
|
||||
{
|
||||
|
|
@ -57,14 +54,10 @@ class Tenant extends Model implements Contracts\TenantWithDatabase
|
|||
|
||||
/**
|
||||
* Set internal key.
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @return $this
|
||||
*/
|
||||
public function setInternal(string $key, $value)
|
||||
{
|
||||
$this->setAttribute($key, $value);
|
||||
$this->setAttribute(static::internalPrefix() . $key, $value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue