mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-13 01:14:04 +00:00
vague first draft of v3. TenantModelTest is passing
This commit is contained in:
parent
c2c90ff755
commit
bd9aad229b
56 changed files with 803 additions and 1366 deletions
11
src/Database/Models/Concerns/CentralConnection.php
Normal file
11
src/Database/Models/Concerns/CentralConnection.php
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Database\Models\Concerns;
|
||||
|
||||
trait CentralConnection
|
||||
{
|
||||
public function getConnectionName()
|
||||
{
|
||||
return config('tenancy.central_connection');
|
||||
}
|
||||
}
|
||||
15
src/Database/Models/Concerns/GeneratesIds.php
Normal file
15
src/Database/Models/Concerns/GeneratesIds.php
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Database\Models\Concerns;
|
||||
|
||||
trait GeneratesIds
|
||||
{
|
||||
public static function bootGeneratesIds()
|
||||
{
|
||||
static::creating(function (self $model) {
|
||||
if (! $model->id && config('tenancy.id_generator')) {
|
||||
$model->id = app(config('tenancy.id_generator'))->generate($model);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
55
src/Database/Models/Concerns/HasADataColumn.php
Normal file
55
src/Database/Models/Concerns/HasADataColumn.php
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Database\Models\Concerns;
|
||||
|
||||
trait HasADataColumn
|
||||
{
|
||||
public static function bootHasADataColumn()
|
||||
{
|
||||
$encode = function (self $model) {
|
||||
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]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
$decode = function (self $model) {
|
||||
foreach ($model->getAttribute(static::getDataColumn()) ?? [] as $key => $value) {
|
||||
$model->setAttribute($key, $value);
|
||||
}
|
||||
|
||||
$model->setAttribute(static::getDataColumn(), null);
|
||||
};
|
||||
|
||||
static::saving($encode);
|
||||
static::saved($decode);
|
||||
static::retrieved($decode);
|
||||
}
|
||||
|
||||
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'));
|
||||
}
|
||||
}
|
||||
24
src/Database/Models/Domain.php
Normal file
24
src/Database/Models/Domain.php
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Database\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Stancl\Tenancy\Events\DomainCreated;
|
||||
use Stancl\Tenancy\Events\DomainDeleted;
|
||||
use Stancl\Tenancy\Events\DomainSaved;
|
||||
use Stancl\Tenancy\Events\DomainUpdated;
|
||||
|
||||
class Domain extends Model
|
||||
{
|
||||
public function tenant()
|
||||
{
|
||||
return $this->belongsTo(Tenant::class);
|
||||
}
|
||||
|
||||
protected $dispatchEvents = [
|
||||
'saved' => DomainSaved::class,
|
||||
'created' => DomainCreated::class,
|
||||
'updated' => DomainUpdated::class,
|
||||
'deleted' => DomainDeleted::class,
|
||||
];
|
||||
}
|
||||
93
src/Database/Models/Tenant.php
Normal file
93
src/Database/Models/Tenant.php
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Database\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Stancl\Tenancy\DatabaseConfig;
|
||||
use Stancl\Tenancy\Events;
|
||||
|
||||
// todo use a contract
|
||||
class Tenant extends Model
|
||||
{
|
||||
use Concerns\CentralConnection, Concerns\HasADataColumn, Concerns\GeneratesIds, Concerns\HasADataColumn {
|
||||
Concerns\HasADataColumn::getCasts as dataColumnCasts;
|
||||
}
|
||||
|
||||
public $primaryKey = 'id';
|
||||
|
||||
public function getCasts()
|
||||
{
|
||||
return array_merge($this->dataColumnCasts(), [
|
||||
'id' => $this->getIncrementing() ? 'integer' : 'string',
|
||||
]);
|
||||
}
|
||||
|
||||
public function getIncrementing()
|
||||
{
|
||||
return config('tenancy.id_generator') === null;
|
||||
}
|
||||
|
||||
public $guarded = [];
|
||||
|
||||
public function domains() // todo not required
|
||||
{
|
||||
return $this->hasMany(Domain::class);
|
||||
}
|
||||
|
||||
public static function internalPrefix(): string
|
||||
{
|
||||
return config('tenancy.database_prefix');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an internal key.
|
||||
*
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function getInternal(string $key)
|
||||
{
|
||||
return $this->getAttribute(static::internalPrefix() . $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set internal key.
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @return $this
|
||||
*/
|
||||
public function setInternal(string $key, $value)
|
||||
{
|
||||
$this->setAttribute($key, $value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function database(): DatabaseConfig
|
||||
{
|
||||
return new DatabaseConfig($this);
|
||||
}
|
||||
|
||||
public function run(callable $callback)
|
||||
{
|
||||
$originalTenant = $this->manager->getTenant();
|
||||
|
||||
$this->manager->initializeTenancy($this);
|
||||
$result = $callback($this);
|
||||
$this->manager->endTenancy($this);
|
||||
|
||||
if ($originalTenant) {
|
||||
$this->manager->initializeTenancy($originalTenant);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
protected $dispatchesEvents = [
|
||||
'saved' => Events\TenantSaved::class,
|
||||
'created' => Events\TenantCreated::class,
|
||||
'updated' => Events\TenantUpdated::class,
|
||||
'deleted' => Events\TenantDeleted::class,
|
||||
];
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue