mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 19:34:04 +00:00
StorageDrivers\Database domain
This commit is contained in:
parent
048a38a308
commit
23a4fd24b0
5 changed files with 113 additions and 94 deletions
84
src/StorageDrivers/Database/DatabaseStorageDriver.php
Normal file
84
src/StorageDrivers/Database/DatabaseStorageDriver.php
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\StorageDrivers\Database;
|
||||
|
||||
use Stancl\Tenancy\Contracts\StorageDriver;
|
||||
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
|
||||
use Stancl\Tenancy\Tenant;
|
||||
use Stancl\Tenancy\StorageDrivers\Database\Tenants as Tenants;
|
||||
use Stancl\Tenancy\StorageDrivers\Database\DomainModel as Domains;
|
||||
|
||||
class DatabaseStorageDriver implements StorageDriver
|
||||
{
|
||||
// todo write tests verifying that data is decoded and added to the array
|
||||
|
||||
public function findByDomain(string $domain): Tenant
|
||||
{
|
||||
$id = $this->getTenantIdByDomain($domain);
|
||||
if (! $id) {
|
||||
throw new TenantCouldNotBeIdentifiedException($domain);
|
||||
}
|
||||
|
||||
return $this->find($id);
|
||||
}
|
||||
|
||||
public function find(string $id): Tenant
|
||||
{
|
||||
return Tenant::fromStorage(Tenants::find($id)->decoded())
|
||||
->withDomains(Domains::where('tenant_id', $id)->all()->only('domain')->toArray());
|
||||
}
|
||||
|
||||
public function getTenantIdByDomain(string $domain): ?string
|
||||
{
|
||||
return Domains::where('domain', $domain)->first()->tenant_id ?? null;
|
||||
}
|
||||
|
||||
public function createTenant(string $domain, string $id): void
|
||||
{
|
||||
DB::transaction(function () use ($domain, $id) {
|
||||
Tenants::create(['id' => $id, 'data' => '{}'])->toArray();
|
||||
Domains::create(['domain' => $domain, 'tenant_id' => $id]);
|
||||
});
|
||||
}
|
||||
|
||||
public function updateTenant(Tenant $tenant): void
|
||||
{
|
||||
// todo
|
||||
}
|
||||
|
||||
public function deleteTenant(string $id): bool
|
||||
{
|
||||
return Tenants::find($id)->delete();
|
||||
}
|
||||
|
||||
public function all(array $ids = []): array
|
||||
{
|
||||
return Tenants::getAllTenants($ids)->toArray();
|
||||
}
|
||||
|
||||
public function get(string $id, string $key)
|
||||
{
|
||||
return Tenants::find($id)->get($key);
|
||||
}
|
||||
|
||||
public function getMany(string $id, array $keys): array
|
||||
{
|
||||
return Tenants::find($id)->getMany($keys);
|
||||
}
|
||||
|
||||
public function put(string $id, string $key, $value)
|
||||
{
|
||||
return Tenants::find($id)->put($key, $value);
|
||||
}
|
||||
|
||||
public function putMany(string $id, array $values): array
|
||||
{
|
||||
foreach ($values as $key => $value) { // todo performance
|
||||
Tenants::find($id)->put($key, $value);
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
}
|
||||
23
src/StorageDrivers/Database/DomainModel.php
Normal file
23
src/StorageDrivers/Database/DomainModel.php
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\StorageDrivers\Database;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* @internal Class is subject to breaking changes in minor and patch versions.
|
||||
*/
|
||||
class DomainModel extends Model
|
||||
{
|
||||
protected $guarded = [];
|
||||
protected $primaryKey = 'id';
|
||||
public $incrementing = false;
|
||||
public $timestamps = false;
|
||||
|
||||
public function getConnectionName()
|
||||
{
|
||||
return config('tenancy.storage.db.connection') ?? app(DatabaseManager::class)->originalDefaultConnectionName;
|
||||
}
|
||||
}
|
||||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy;
|
||||
namespace Stancl\Tenancy\StorageDrivers\Database;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
|
|
@ -31,9 +31,9 @@ class TenantModel extends Model
|
|||
return config('tenancy.storage.db.connection') ?? app(DatabaseManager::class)->originalDefaultConnectionName;
|
||||
}
|
||||
|
||||
public static function getAllTenants(array $uuids)
|
||||
public static function getAllTenants(array $ids)
|
||||
{
|
||||
$tenants = $uuids ? static::findMany($uuids) : static::all();
|
||||
$tenants = $ids ? static::findMany($ids) : static::all();
|
||||
|
||||
return $tenants->map([__CLASS__, 'decodeData'])->toBase();
|
||||
}
|
||||
|
|
@ -46,7 +46,7 @@ class TenantModel extends Model
|
|||
/**
|
||||
* Return a tenant array with data decoded into separate keys.
|
||||
*
|
||||
* @param Tenant|array $tenant
|
||||
* @param self|array $tenant
|
||||
* @return array
|
||||
*/
|
||||
public static function decodeData($tenant)
|
||||
|
|
@ -1,90 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\StorageDrivers;
|
||||
|
||||
use Stancl\Tenancy\Contracts\StorageDriver;
|
||||
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
|
||||
use Stancl\Tenancy\TenantModel as Tenant;
|
||||
|
||||
class DatabaseStorageDriver implements StorageDriver
|
||||
{
|
||||
public $useJson = false;
|
||||
|
||||
// todo use an instance of tenant model?
|
||||
// todo write tests verifying that data is decoded and added to the array
|
||||
|
||||
public function identifyTenant(string $domain): array
|
||||
{
|
||||
$id = $this->getTenantIdByDomain($domain);
|
||||
if (! $id) {
|
||||
throw new TenantCouldNotBeIdentifiedException($domain);
|
||||
}
|
||||
|
||||
return $this->getTenantById($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get information about the tenant based on his uuid.
|
||||
*
|
||||
* @param string $uuid
|
||||
* @param array $fields
|
||||
* @return array
|
||||
*/
|
||||
public function getTenantById(string $uuid, array $fields = []): array
|
||||
{
|
||||
if ($fields) {
|
||||
return Tenant::decodeData(Tenant::find($uuid)->only($fields));
|
||||
} else {
|
||||
return Tenant::find($uuid)->decoded();
|
||||
}
|
||||
}
|
||||
|
||||
public function getTenantIdByDomain(string $domain): ?string
|
||||
{
|
||||
return Tenant::where('domain', $domain)->first()->uuid ?? null;
|
||||
}
|
||||
|
||||
public function createTenant(string $domain, string $uuid): array
|
||||
{
|
||||
$tenant = Tenant::create(['uuid' => $uuid, 'domain' => $domain, 'data' => '{}'])->toArray();
|
||||
unset($tenant['data']);
|
||||
|
||||
return $tenant;
|
||||
}
|
||||
|
||||
public function deleteTenant(string $id): bool
|
||||
{
|
||||
return Tenant::find($id)->delete();
|
||||
}
|
||||
|
||||
public function getAllTenants(array $uuids = []): array
|
||||
{
|
||||
return Tenant::getAllTenants($uuids)->toArray();
|
||||
}
|
||||
|
||||
public function get(string $uuid, string $key)
|
||||
{
|
||||
return Tenant::find($uuid)->get($key);
|
||||
}
|
||||
|
||||
public function getMany(string $uuid, array $keys): array
|
||||
{
|
||||
return Tenant::find($uuid)->getMany($keys);
|
||||
}
|
||||
|
||||
public function put(string $uuid, string $key, $value)
|
||||
{
|
||||
return 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);
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
}
|
||||
|
|
@ -12,6 +12,8 @@ use Stancl\Tenancy\Tenant;
|
|||
|
||||
class RedisStorageDriver implements StorageDriver
|
||||
{
|
||||
// todo json encoding?
|
||||
|
||||
/** @var Application */
|
||||
protected $app;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue