diff --git a/src/Interfaces/TenantModel.php b/src/Interfaces/TenantModel.php new file mode 100644 index 00000000..135929af --- /dev/null +++ b/src/Interfaces/TenantModel.php @@ -0,0 +1,39 @@ +tenant = $tenant; + } + public function identifyTenant(string $domain): array { $id = $this->getTenantIdByDomain($domain); @@ -26,50 +31,50 @@ class DatabaseStorageDriver implements StorageDriver */ public function getTenantById(string $uuid, array $fields = []): array { - return Tenant::find($uuid)->only($fields)->toArray(); + return $this->tenant->find($uuid)->only($fields); } public function getTenantIdByDomain(string $domain): ?string { - return Tenant::where('domain', $domain)->first()->uuid ?? null; + return $this->tenant->where('domain', $domain)->first()->uuid ?? null; } public function createTenant(string $domain, string $uuid): array { - return Tenant::create(['uuid' => $uuid, 'domain' => $domain])->toArray(); + return $this->tenant->create(['uuid' => $uuid, 'domain' => $domain])->toArray(); } public function deleteTenant(string $id): bool { - return Tenant::find($id)->delete(); + return $this->tenant->find($id)->delete(); } public function getAllTenants(array $uuids = []): array { - return Tenant::all()->map(function ($model) { + return $this->tenant->all()->map(function ($model) { return $model->toArray(); })->toArray(); } public function get(string $uuid, string $key) { - return Tenant::find($uuid)->get($key); + return $this->tenant->find($uuid)->get($key); } public function getMany(string $uuid, array $keys): array { - return Tenant::getMany($keys); + return $this->tenant->getMany($keys); } public function put(string $uuid, string $key, $value) { - return Tenant::find($uuid)->put($key, $value); + return $this->tenant->find($uuid)->put($key, $value); } public function putMany(string $uuid, array $values): array { foreach ($values as $key => $value) { - Tenant::find($uuid)->put($key, $value); + $this->tenant->find($uuid)->put($key, $value); } return $values; diff --git a/src/TenancyServiceProvider.php b/src/TenancyServiceProvider.php index 1c174ea3..9491f46f 100644 --- a/src/TenancyServiceProvider.php +++ b/src/TenancyServiceProvider.php @@ -9,6 +9,7 @@ use Illuminate\Support\Facades\Route; use Stancl\Tenancy\Commands\Rollback; use Illuminate\Support\ServiceProvider; use Stancl\Tenancy\Commands\TenantList; +use Stancl\Tenancy\Interfaces\TenantModel; use Stancl\Tenancy\Interfaces\StorageDriver; use Stancl\Tenancy\Interfaces\ServerConfigManager; @@ -57,6 +58,7 @@ class TenancyServiceProvider extends ServiceProvider $this->mergeConfigFrom(__DIR__ . '/assets/config.php', 'tenancy'); $this->app->bind(StorageDriver::class, $this->app['config']['tenancy.storage_driver']); + $this->app->bind(TenantModel::class, $this->app['config']['tenancy.tenant_model']); $this->app->bind(ServerConfigManager::class, $this->app['config']['tenancy.server.manager']); $this->app->singleton(DatabaseManager::class); $this->app->singleton(TenantManager::class, function ($app) { diff --git a/src/Tenant.php b/src/Tenant.php index a7349a26..65d967a3 100644 --- a/src/Tenant.php +++ b/src/Tenant.php @@ -4,12 +4,12 @@ namespace Stancl\Tenancy; use Illuminate\Database\Eloquent\Model; -class Tenant extends Model +class Tenant extends Model implements TenantModel { protected $dataColumn = 'data'; protected $specialColumns = []; protected $guarded = []; - protected $publicKey = 'uuid'; + protected $primaryKey = 'uuid'; public $incrementing = false; /** @@ -19,12 +19,6 @@ class Tenant extends Model */ private $dataObject; - /** - * Get data from the data column. - * - * @param string $key - * @return mixed - */ public function getFromData(string $key) { $this->dataObject = $this->dataObject ?? json_decode($this->{$this->dataColumn}); @@ -32,23 +26,11 @@ class Tenant extends Model return $this->dataObject->$key; } - /** - * Get data from tenant storage. - * - * @param string $key - * @return mixed - */ public function get(string $key) { return $this->$key ?? $this->getFromData($key) ?? null; } - /** - * Get multiple values from tenant storage. - * - * @param array $keys - * @return array - */ public function getMany(array $keys): array { return array_reduce($keys, function ($keys, $key) { @@ -58,13 +40,6 @@ class Tenant extends Model }, []); } - /** - * Put data into tenant storage. - * - * @param string $key - * @param mixed $value - * @return mixed - */ public function put(string $key, $value) { if (array_key_exists($key, $this->specialColumns)) { diff --git a/src/assets/config.php b/src/assets/config.php index c33f8f63..c742da84 100644 --- a/src/assets/config.php +++ b/src/assets/config.php @@ -2,6 +2,7 @@ return [ 'storage_driver' => 'Stancl\Tenancy\StorageDrivers\RedisStorageDriver', + 'tenant_model' => 'Stancl\Tenancy\Tenant', 'tenant_route_namespace' => 'App\Http\Controllers', 'exempt_domains' => [ // 'localhost',