1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 22:34:03 +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

@ -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).
*