diff --git a/src/Interfaces/TenantModel.php b/src/Interfaces/TenantModel.php deleted file mode 100644 index 135929af..00000000 --- a/src/Interfaces/TenantModel.php +++ /dev/null @@ -1,39 +0,0 @@ -tenant = $tenant; - } - public function identifyTenant(string $domain): array { $id = $this->getTenantIdByDomain($domain); @@ -32,53 +27,53 @@ class DatabaseStorageDriver implements StorageDriver public function getTenantById(string $uuid, array $fields = []): array { if ($fields) { - return $this->tenant->find($uuid)->only($fields); + return Tenant::find($uuid)->only($fields); } else { - return $this->tenant->find($uuid)->toArray(); + return Tenant::find($uuid)->toArray(); } } 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 { - return $this->tenant->create(['uuid' => $uuid, 'domain' => $domain])->toArray(); + return Tenant::create(['uuid' => $uuid, 'domain' => $domain])->toArray(); } public function deleteTenant(string $id): bool { - return $this->tenant->find($id)->delete(); + return Tenant::find($id)->delete(); } public function getAllTenants(array $uuids = []): array { - return $this->tenant->all()->map(function ($model) { + return Tenant::all()->map(function ($model) { return $model->toArray(); })->toArray(); } 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 { - return $this->tenant->getMany($keys); + return Tenant::getMany($keys); } 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 { foreach ($values as $key => $value) { - $this->tenant->find($uuid)->put($key, $value); + Tenant::find($uuid)->put($key, $value); } return $values; diff --git a/src/Tenant.php b/src/Tenant.php index 7b24e4dc..71f47590 100644 --- a/src/Tenant.php +++ b/src/Tenant.php @@ -3,12 +3,9 @@ namespace Stancl\Tenancy; 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 $primaryKey = 'uuid'; public $incrementing = false; @@ -21,9 +18,19 @@ class Tenant extends Model implements TenantModel */ 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) { - $this->dataObject = $this->dataObject ?? json_decode($this->{$this->dataColumn}); + $this->dataObject = $this->dataObject ?? json_decode($this->{$this->dataColumn()}); return $this->dataObject->$key; } @@ -44,10 +51,10 @@ class Tenant extends Model implements TenantModel public function put(string $key, $value) { - if (array_key_exists($key, $this->specialColumns)) { + if (array_key_exists($key, $this->customColumns())) { $this->update([$key => $value]); } else { - $obj = json_decode($this->{$this->dataColumn}); + $obj = json_decode($this->{$this->dataColumn()}); $obj->$key = $value; $this->update([$this->getDataColumn() => json_encode($obj)]); diff --git a/src/assets/config.php b/src/assets/config.php index 0c2944aa..1843bac1 100644 --- a/src/assets/config.php +++ b/src/assets/config.php @@ -2,7 +2,10 @@ return [ 'storage_driver' => 'Stancl\Tenancy\StorageDrivers\DatabaseStorageDriver', - 'tenant_model' => 'Stancl\Tenancy\Tenant', + 'db_storage' => [ + 'data_column' => 'data', + 'custom_columns' => [], + ], 'tenant_route_namespace' => 'App\Http\Controllers', 'exempt_domains' => [ // 'localhost',