mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-13 09:04:04 +00:00
Refactor models & config
This commit is contained in:
parent
f0ff8248e7
commit
2839f45196
9 changed files with 123 additions and 97 deletions
19
src/Database/Concerns/EnsuresDomainIsNotOccupied.php
Normal file
19
src/Database/Concerns/EnsuresDomainIsNotOccupied.php
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Database\Concerns;
|
||||
|
||||
use Stancl\Tenancy\Exceptions\DomainOccupiedByOtherTenantException;
|
||||
|
||||
trait EnsuresDomainIsNotOccupied
|
||||
{
|
||||
public static function bootEnsuresDomainIsNotOccupied()
|
||||
{
|
||||
static::saving(function ($self) {
|
||||
if ($domain = $self->newQuery()->where('domain', $self->domain)->first()) {
|
||||
if ($domain->getKey() !== $self->getKey()) {
|
||||
throw new DomainOccupiedByOtherTenantException($self->domain);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -11,7 +11,7 @@ trait GeneratesIds
|
|||
public static function bootGeneratesIds()
|
||||
{
|
||||
static::creating(function (self $model) {
|
||||
if (! $model->getTenantKey() && app()->bound(UniqueIdentifierGenerator::class)) {
|
||||
if (! $model->getTenantKey() && $model->shouldGenerateId()) {
|
||||
$model->setAttribute($model->getTenantKeyName(), app(UniqueIdentifierGenerator::class)->generate($model));
|
||||
}
|
||||
});
|
||||
|
|
@ -19,6 +19,11 @@ trait GeneratesIds
|
|||
|
||||
public function getIncrementing()
|
||||
{
|
||||
return ! app()->bound(UniqueIdentifierGenerator::class);
|
||||
return ! $this->shouldGenerateId();
|
||||
}
|
||||
|
||||
public function shouldGenerateId(): bool
|
||||
{
|
||||
return app()->bound(UniqueIdentifierGenerator::class);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
32
src/Database/Concerns/HasInternalKeys.php
Normal file
32
src/Database/Concerns/HasInternalKeys.php
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Database\Concerns;
|
||||
|
||||
trait HasInternalKeys
|
||||
{
|
||||
/**
|
||||
* Get the internal prefix.
|
||||
*/
|
||||
public static function internalPrefix(): string
|
||||
{
|
||||
return 'tenancy_';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an internal key.
|
||||
*/
|
||||
public function getInternal(string $key)
|
||||
{
|
||||
return $this->getAttribute(static::internalPrefix() . $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set internal key.
|
||||
*/
|
||||
public function setInternal(string $key, $value)
|
||||
{
|
||||
$this->setAttribute(static::internalPrefix() . $key, $value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
26
src/Database/Concerns/TenantRun.php
Normal file
26
src/Database/Concerns/TenantRun.php
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Database\Concerns;
|
||||
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
|
||||
trait TenantRun
|
||||
{
|
||||
public function run(callable $callback)
|
||||
{
|
||||
/** @var Tenant $this */
|
||||
|
||||
$originalTenant = tenant();
|
||||
|
||||
tenancy()->initialize($this);
|
||||
$result = $callback($this);
|
||||
|
||||
if ($originalTenant) {
|
||||
tenancy()->initialize($originalTenant);
|
||||
} else {
|
||||
tenancy()->end();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue