1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 19:34:04 +00:00

[1.7.0] Add DB storage driver (#82)

This commit is contained in:
Samuel Štancl 2019-08-16 14:36:49 +02:00 committed by GitHub
parent 674f4b3f9a
commit 9df78eb9c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 375 additions and 25 deletions

View file

@ -6,6 +6,7 @@ interface StorageDriver
{
public function identifyTenant(string $domain): array;
/** @return array[] */
public function getAllTenants(array $uuids = []): array;
public function getTenantById(string $uuid, array $fields = []): array;

View file

@ -0,0 +1,84 @@
<?php
namespace Stancl\Tenancy\StorageDrivers;
use Stancl\Tenancy\Tenant;
use Stancl\Tenancy\Interfaces\StorageDriver;
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 \Exception("Tenant could not be identified on domain {$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
{
return Tenant::create(['uuid' => $uuid, 'domain' => $domain])->toArray();
}
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;
}
}

View file

@ -11,7 +11,7 @@ class RedisStorageDriver implements StorageDriver
public function __construct()
{
$this->redis = Redis::connection('tenancy');
$this->redis = Redis::connection(config('tenancy.redis.connection', 'tenancy'));
}
public function identifyTenant(string $domain): array
@ -33,8 +33,6 @@ class RedisStorageDriver implements StorageDriver
*/
public function getTenantById(string $uuid, array $fields = []): array
{
$fields = (array) $fields;
if (! $fields) {
return $this->redis->hgetall("tenants:$uuid");
}

View file

@ -33,9 +33,13 @@ class TenancyServiceProvider extends ServiceProvider
]);
$this->publishes([
__DIR__ . '/config/tenancy.php' => config_path('tenancy.php'),
__DIR__ . '/../assets/config.php' => config_path('tenancy.php'),
], 'config');
$this->publishes([
__DIR__ . '/../assets/migrations/' => database_path('migrations'),
], 'migrations');
$this->loadRoutesFrom(__DIR__ . '/routes.php');
Route::middlewareGroup('tenancy', [
@ -52,7 +56,7 @@ class TenancyServiceProvider extends ServiceProvider
*/
public function register()
{
$this->mergeConfigFrom(__DIR__ . '/config/tenancy.php', 'tenancy');
$this->mergeConfigFrom(__DIR__ . '/../assets/config.php', 'tenancy');
$this->app->bind(StorageDriver::class, $this->app['config']['tenancy.storage_driver']);
$this->app->bind(ServerConfigManager::class, $this->app['config']['tenancy.server.manager']);

102
src/Tenant.php Normal file
View file

@ -0,0 +1,102 @@
<?php
namespace Stancl\Tenancy;
use Illuminate\Database\Eloquent\Model;
class Tenant extends Model
{
protected $guarded = [];
protected $primaryKey = 'uuid';
public $incrementing = false;
public $timestamps = false;
/**
* Decoded data from the data column.
*
* @var object
*/
private $dataObject;
public static function dataColumn()
{
return config('tenancy.storage.db.data_column', 'data');
}
public static function customColumns()
{
return config('tenancy.storage.db.custom_columns', []);
}
public function getConnectionName()
{
return config('tenancy.storage.db.connection', 'central');
}
public static function getAllTenants(array $uuids)
{
$tenants = $uuids ? static::findMany($uuids) : static::all();
return $tenants->map([__CLASS__, 'decodeData'])->toBase();
}
public function decoded()
{
return static::decodeData($this);
}
/**
* Return a tenant array with data decoded into separate keys.
*
* @param Tenant|array $tenant
* @return array
*/
public static function decodeData($tenant)
{
$tenant = $tenant instanceof self ? (array) $tenant->attributes : $tenant;
$decoded = json_decode($tenant[$dataColumn = static::dataColumn()], true);
foreach ($decoded as $key => $value) {
$tenant[$key] = $value;
}
// If $tenant[$dataColumn] has been overriden by a value, don't delete the key.
if (! array_key_exists($dataColumn, $decoded)) {
unset($tenant[$dataColumn]);
}
return $tenant;
}
public function getFromData(string $key)
{
$this->dataArray = $this->dataArray ?? json_decode($this->{$this->dataColumn()}, true);
return $this->dataArray[$key] ?? null;
}
public function get(string $key)
{
return $this->$key ?? $this->getFromData($key) ?? null;
}
/** @todo In v2, this should return an associative array. */
public function getMany(array $keys): array
{
return array_map([$this, 'get'], $keys);
}
public function put(string $key, $value)
{
if (array_key_exists($key, $this->customColumns())) {
$this->update([$key => $value]);
} else {
$obj = json_decode($this->{$this->dataColumn()});
$obj->$key = $value;
$this->update([$this->dataColumn() => json_encode($obj)]);
}
return $value;
}
}

View file

@ -23,7 +23,7 @@ final class TenantManager
*
* @var StorageDriver
*/
protected $storage;
public $storage;
/**
* Database manager.
@ -86,7 +86,10 @@ final class TenantManager
throw new \Exception("Domain $domain is already occupied by tenant $id.");
}
$tenant = $this->jsonDecodeArrayValues($this->storage->createTenant($domain, (string) \Webpatser\Uuid\Uuid::generate(1, $domain)));
$tenant = $this->storage->createTenant($domain, (string) \Webpatser\Uuid\Uuid::generate(1, $domain));
if ($this->useJson()) {
$tenant = $this->jsonDecodeArrayValues($tenant);
}
if ($data) {
$this->put($data, null, $tenant['uuid']);
@ -115,7 +118,12 @@ final class TenantManager
{
$fields = (array) $fields;
return $this->jsonDecodeArrayValues($this->storage->getTenantById($uuid, $fields));
$tenant = $this->storage->getTenantById($uuid, $fields);
if ($this->useJson()) {
$tenant = $this->jsonDecodeArrayValues($tenant);
}
return $tenant;
}
/**
@ -200,7 +208,9 @@ final class TenantManager
*/
public function setTenant(array $tenant): array
{
$tenant = $this->jsonDecodeArrayValues($tenant);
if ($this->useJson()) {
$tenant = $this->jsonDecodeArrayValues($tenant);
}
$this->tenant = $tenant;
@ -227,10 +237,15 @@ final class TenantManager
public function all($uuids = [])
{
$uuids = (array) $uuids;
$tenants = $this->storage->getAllTenants($uuids);
return collect(array_map(function ($tenant_array) {
return $this->jsonDecodeArrayValues($tenant_array);
}, $this->storage->getAllTenants($uuids)));
if ($this->useJson()) {
$tenants = array_map(function ($tenant_array) {
return $this->jsonDecodeArrayValues($tenant_array);
}, $tenants);
}
return collect($tenants);
}
/**
@ -336,6 +351,15 @@ final class TenantManager
return $array;
}
public function useJson()
{
if (property_exists($this->storage, 'useJson') && $this->storage->useJson === false) {
return false;
}
return true;
}
/**
* Return the identified tenant's attribute(s).
*

View file

@ -1,47 +0,0 @@
<?php
return [
'storage_driver' => 'Stancl\Tenancy\StorageDrivers\RedisStorageDriver',
'tenant_route_namespace' => 'App\Http\Controllers',
'exempt_domains' => [
// 'localhost',
],
'database' => [
'based_on' => 'mysql',
'prefix' => 'tenant',
'suffix' => '',
],
'redis' => [
'tenancy' => false,
'prefix_base' => 'tenant',
'prefixed_connections' => [
'default',
'cache',
],
],
'cache' => [
'tag_base' => 'tenant',
],
'filesystem' => [
'suffix_base' => 'tenant',
// Disks which should be suffixed with the suffix_base + tenant UUID.
'disks' => [
'local',
'public',
// 's3',
],
'root_override' => [
// Disks whose roots should be overriden after storage_path() is suffixed.
'local' => '%storage_path%/app/',
'public' => '%storage_path%/app/public/',
],
],
'database_managers' => [
'sqlite' => 'Stancl\Tenancy\TenantDatabaseManagers\SQLiteDatabaseManager',
'mysql' => 'Stancl\Tenancy\TenantDatabaseManagers\MySQLDatabaseManager',
'pgsql' => 'Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLDatabaseManager',
],
'queue_database_creation' => false,
'queue_database_deletion' => false,
'database_name_key' => null,
];