mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-04 17:04:03 +00:00
add TenantModel interface, fix issues pointed out by @carlos-mora
This commit is contained in:
parent
de025a6a4d
commit
4d7c6b6683
5 changed files with 59 additions and 37 deletions
39
src/Interfaces/TenantModel.php
Normal file
39
src/Interfaces/TenantModel.php
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
<?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);
|
||||
}
|
||||
|
|
@ -2,11 +2,16 @@
|
|||
|
||||
namespace Stancl\Tenancy\StorageDrivers;
|
||||
|
||||
use Stancl\Tenancy\Tenant;
|
||||
use Stancl\Tenancy\Interfaces\StorageDriver;
|
||||
use Stancl\Tenancy\Interfaces\TenantModel;
|
||||
|
||||
class DatabaseStorageDriver implements StorageDriver
|
||||
{
|
||||
public function __construct(TenantModel $tenant)
|
||||
{
|
||||
$this->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;
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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)) {
|
||||
|
|
|
|||
|
|
@ -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',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue