mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-04 19:04:03 +00:00
get rid of TenantModel interface
This commit is contained in:
parent
9296b3806e
commit
ef718f26b3
4 changed files with 28 additions and 62 deletions
|
|
@ -1,39 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Interfaces;
|
|
||||||
|
|
||||||
interface TenantModel
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Get data from the data column.
|
|
||||||
*
|
|
||||||
* @param string $key
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function getFromData(string $key);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get data from tenant storage.
|
|
||||||
*
|
|
||||||
* @param string $key
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function get(string $key);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get multiple values from tenant storage.
|
|
||||||
*
|
|
||||||
* @param array $keys
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getMany(array $keys): array;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Put data into tenant storage.
|
|
||||||
*
|
|
||||||
* @param string $key
|
|
||||||
* @param mixed $value
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function put(string $key, $value);
|
|
||||||
}
|
|
||||||
|
|
@ -7,11 +7,6 @@ use Stancl\Tenancy\Interfaces\StorageDriver;
|
||||||
|
|
||||||
class DatabaseStorageDriver implements StorageDriver
|
class DatabaseStorageDriver implements StorageDriver
|
||||||
{
|
{
|
||||||
public function __construct(TenantModel $tenant)
|
|
||||||
{
|
|
||||||
$this->tenant = $tenant;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function identifyTenant(string $domain): array
|
public function identifyTenant(string $domain): array
|
||||||
{
|
{
|
||||||
$id = $this->getTenantIdByDomain($domain);
|
$id = $this->getTenantIdByDomain($domain);
|
||||||
|
|
@ -32,53 +27,53 @@ class DatabaseStorageDriver implements StorageDriver
|
||||||
public function getTenantById(string $uuid, array $fields = []): array
|
public function getTenantById(string $uuid, array $fields = []): array
|
||||||
{
|
{
|
||||||
if ($fields) {
|
if ($fields) {
|
||||||
return $this->tenant->find($uuid)->only($fields);
|
return Tenant::find($uuid)->only($fields);
|
||||||
} else {
|
} else {
|
||||||
return $this->tenant->find($uuid)->toArray();
|
return Tenant::find($uuid)->toArray();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getTenantIdByDomain(string $domain): ?string
|
public function getTenantIdByDomain(string $domain): ?string
|
||||||
{
|
{
|
||||||
return $this->tenant->where('domain', $domain)->first()->uuid ?? null;
|
return Tenant::where('domain', $domain)->first()->uuid ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function createTenant(string $domain, string $uuid): array
|
public function createTenant(string $domain, string $uuid): array
|
||||||
{
|
{
|
||||||
return $this->tenant->create(['uuid' => $uuid, 'domain' => $domain])->toArray();
|
return Tenant::create(['uuid' => $uuid, 'domain' => $domain])->toArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function deleteTenant(string $id): bool
|
public function deleteTenant(string $id): bool
|
||||||
{
|
{
|
||||||
return $this->tenant->find($id)->delete();
|
return Tenant::find($id)->delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getAllTenants(array $uuids = []): array
|
public function getAllTenants(array $uuids = []): array
|
||||||
{
|
{
|
||||||
return $this->tenant->all()->map(function ($model) {
|
return Tenant::all()->map(function ($model) {
|
||||||
return $model->toArray();
|
return $model->toArray();
|
||||||
})->toArray();
|
})->toArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get(string $uuid, string $key)
|
public function get(string $uuid, string $key)
|
||||||
{
|
{
|
||||||
return $this->tenant->find($uuid)->get($key);
|
return Tenant::find($uuid)->get($key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getMany(string $uuid, array $keys): array
|
public function getMany(string $uuid, array $keys): array
|
||||||
{
|
{
|
||||||
return $this->tenant->getMany($keys);
|
return Tenant::getMany($keys);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function put(string $uuid, string $key, $value)
|
public function put(string $uuid, string $key, $value)
|
||||||
{
|
{
|
||||||
return $this->tenant->find($uuid)->put($key, $value);
|
return Tenant::find($uuid)->put($key, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function putMany(string $uuid, array $values): array
|
public function putMany(string $uuid, array $values): array
|
||||||
{
|
{
|
||||||
foreach ($values as $key => $value) {
|
foreach ($values as $key => $value) {
|
||||||
$this->tenant->find($uuid)->put($key, $value);
|
Tenant::find($uuid)->put($key, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $values;
|
return $values;
|
||||||
|
|
|
||||||
|
|
@ -3,12 +3,9 @@
|
||||||
namespace Stancl\Tenancy;
|
namespace Stancl\Tenancy;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Stancl\Tenancy\Interfaces\TenantModel;
|
|
||||||
|
|
||||||
class Tenant extends Model implements TenantModel
|
class Tenant extends Model
|
||||||
{
|
{
|
||||||
protected $dataColumn = 'data'; // todo load this from config
|
|
||||||
protected $specialColumns = []; // todo load this from config
|
|
||||||
protected $guarded = [];
|
protected $guarded = [];
|
||||||
protected $primaryKey = 'uuid';
|
protected $primaryKey = 'uuid';
|
||||||
public $incrementing = false;
|
public $incrementing = false;
|
||||||
|
|
@ -21,9 +18,19 @@ class Tenant extends Model implements TenantModel
|
||||||
*/
|
*/
|
||||||
private $dataObject;
|
private $dataObject;
|
||||||
|
|
||||||
|
public function dataColumn()
|
||||||
|
{
|
||||||
|
return config('tenancy.db_storage.data_column');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function customColumns()
|
||||||
|
{
|
||||||
|
return config('tenancy.db_storage.custom_columns');
|
||||||
|
}
|
||||||
|
|
||||||
public function getFromData(string $key)
|
public function getFromData(string $key)
|
||||||
{
|
{
|
||||||
$this->dataObject = $this->dataObject ?? json_decode($this->{$this->dataColumn});
|
$this->dataObject = $this->dataObject ?? json_decode($this->{$this->dataColumn()});
|
||||||
|
|
||||||
return $this->dataObject->$key;
|
return $this->dataObject->$key;
|
||||||
}
|
}
|
||||||
|
|
@ -44,10 +51,10 @@ class Tenant extends Model implements TenantModel
|
||||||
|
|
||||||
public function put(string $key, $value)
|
public function put(string $key, $value)
|
||||||
{
|
{
|
||||||
if (array_key_exists($key, $this->specialColumns)) {
|
if (array_key_exists($key, $this->customColumns())) {
|
||||||
$this->update([$key => $value]);
|
$this->update([$key => $value]);
|
||||||
} else {
|
} else {
|
||||||
$obj = json_decode($this->{$this->dataColumn});
|
$obj = json_decode($this->{$this->dataColumn()});
|
||||||
$obj->$key = $value;
|
$obj->$key = $value;
|
||||||
|
|
||||||
$this->update([$this->getDataColumn() => json_encode($obj)]);
|
$this->update([$this->getDataColumn() => json_encode($obj)]);
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,10 @@
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'storage_driver' => 'Stancl\Tenancy\StorageDrivers\DatabaseStorageDriver',
|
'storage_driver' => 'Stancl\Tenancy\StorageDrivers\DatabaseStorageDriver',
|
||||||
'tenant_model' => 'Stancl\Tenancy\Tenant',
|
'db_storage' => [
|
||||||
|
'data_column' => 'data',
|
||||||
|
'custom_columns' => [],
|
||||||
|
],
|
||||||
'tenant_route_namespace' => 'App\Http\Controllers',
|
'tenant_route_namespace' => 'App\Http\Controllers',
|
||||||
'exempt_domains' => [
|
'exempt_domains' => [
|
||||||
// 'localhost',
|
// 'localhost',
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue