1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 23:14:03 +00:00

Add array support to the storage. (#27)

* Add array support to the storage.
(Existing tests have been modified, but only in a way that makes them more correct than they were, making this a backwards compatible feature.)
This commit is contained in:
Samuel Štancl 2019-02-10 21:45:36 +01:00 committed by GitHub
parent cd803cbb47
commit 83d272007d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 24 deletions

View file

@ -20,7 +20,7 @@ class RedisStorageDriver implements StorageDriver
if (! $id) { if (! $id) {
throw new \Exception("Tenant could not be identified on domain {$domain}"); throw new \Exception("Tenant could not be identified on domain {$domain}");
} }
return array_merge(['uuid' => $id], $this->getTenantById($id)); return $this->getTenantById($id);
} }
/** /**
@ -49,7 +49,7 @@ class RedisStorageDriver implements StorageDriver
public function createTenant(string $domain, string $uuid): array public function createTenant(string $domain, string $uuid): array
{ {
$this->redis->hmset("domains:$domain", 'tenant_id', $uuid); $this->redis->hmset("domains:$domain", 'tenant_id', $uuid);
$this->redis->hmset("tenants:$uuid", 'uuid', $uuid, 'domain', $domain); $this->redis->hmset("tenants:$uuid", 'uuid', json_encode($uuid), 'domain', json_encode($domain));
return $this->redis->hgetall("tenants:$uuid"); return $this->redis->hgetall("tenants:$uuid");
} }

View file

@ -16,21 +16,21 @@ class TenantManager
* *
* @var Application * @var Application
*/ */
private $app; protected $app;
/** /**
* Storage driver for tenant metadata. * Storage driver for tenant metadata.
* *
* @var StorageDriver * @var StorageDriver
*/ */
private $storage; protected $storage;
/** /**
* Database manager. * Database manager.
* *
* @var DatabaseManager * @var DatabaseManager
*/ */
private $database; protected $database;
/** /**
* Current tenant. * Current tenant.
@ -78,7 +78,7 @@ class TenantManager
throw new \Exception("Domain $domain is already occupied by tenant $id."); throw new \Exception("Domain $domain is already occupied by tenant $id.");
} }
$tenant = $this->storage->createTenant($domain, \Webpatser\Uuid\Uuid::generate(1, $domain)); $tenant = $this->jsonDecodeArrayValues($this->storage->createTenant($domain, \Webpatser\Uuid\Uuid::generate(1, $domain)));
$this->database->create($this->getDatabaseName($tenant)); $this->database->create($this->getDatabaseName($tenant));
return $tenant; return $tenant;
@ -99,7 +99,7 @@ class TenantManager
public function getTenantById(string $uuid, $fields = []) public function getTenantById(string $uuid, $fields = [])
{ {
$fields = (array) $fields; $fields = (array) $fields;
return $this->storage->getTenantById($uuid, $fields); return $this->jsonDecodeArrayValues($this->storage->getTenantById($uuid, $fields));
} }
/** /**
@ -163,8 +163,16 @@ class TenantManager
return config('tenancy.database.prefix') . $tenant['uuid'] . config('tenancy.database.suffix'); return config('tenancy.database.prefix') . $tenant['uuid'] . config('tenancy.database.suffix');
} }
/**
* Set the tenant property to a JSON decoded version of the tenant's data obtained from storage.
*
* @param array $tenant
* @return array
*/
public function setTenant(array $tenant): array public function setTenant(array $tenant): array
{ {
$tenant = $this->jsonDecodeArrayValues($tenant);
$this->tenant = $tenant; $this->tenant = $tenant;
return $tenant; return $tenant;
@ -189,7 +197,10 @@ class TenantManager
public function all($uuids = []) public function all($uuids = [])
{ {
$uuid = (array) $uuids; $uuid = (array) $uuids;
return collect($this->storage->getAllTenants($uuids));
return collect(array_map(function ($tenant_array) {
return $this->jsonDecodeArrayValues($tenant_array);
}, $this->storage->getAllTenants($uuids)));
} }
/** /**
@ -217,10 +228,10 @@ class TenantManager
$uuid = $uuid ?: $this->tenant['uuid']; $uuid = $uuid ?: $this->tenant['uuid'];
if (is_array($key)) { if (is_array($key)) {
return $this->storage->getMany($uuid, $key); return $this->jsonDecodeArrayValues($this->storage->getMany($uuid, $key));
} }
return $this->storage->get($uuid, $key); return json_decode($this->storage->get($uuid, $key));
} }
/** /**
@ -248,18 +259,19 @@ class TenantManager
} }
if (! is_null($value)) { if (! is_null($value)) {
return $target[$key] = $this->storage->put($uuid, $key, $value); return $target[$key] = json_decode($this->storage->put($uuid, $key, json_encode($value)));
} }
if (! is_array($key)) { if (! is_array($key)) {
throw new \Exception("No value supplied for key $key."); throw new \Exception("No value supplied for key $key.");
} }
foreach ($this->storage->putMany($uuid, $key) as $key => $value) { foreach ($key as $k => $v) {
$target[$key] = $value; $target[$k] = $v;
$key[$k] = json_encode($v);
} }
return $key; return $this->jsonDecodeArrayValues($this->storage->putMany($uuid, $key));
} }
/** /**
@ -275,6 +287,15 @@ class TenantManager
return $this->put($key, $value, $uuid); return $this->put($key, $value, $uuid);
} }
protected function jsonDecodeArrayValues(array $array)
{
array_walk($array, function (&$value, $key) {
$value = json_decode($value);
});
return $array;
}
/** /**
* Return the identified tenant's attribute(s). * Return the identified tenant's attribute(s).
* *

View file

@ -6,14 +6,7 @@ use Stancl\Tenancy\Interfaces\StorageDriver;
class TenantStorageTest extends TestCase class TenantStorageTest extends TestCase
{ {
public function setUp() // todo find a way to run this for each storage driver (once there are more of them)
{
parent::setUp();
// todo find a way to run this for each storage driver (once there are more of them)
$this->storage = app(StorageDriver::class);
}
/** @test */ /** @test */
public function deleting_a_tenant_works() public function deleting_a_tenant_works()
@ -32,7 +25,7 @@ class TenantStorageTest extends TestCase
{ {
tenant()->set('foo', 'bar'); tenant()->set('foo', 'bar');
$this->assertSame('bar', $this->storage->get(tenant('uuid'), 'foo')); $this->assertSame('bar', tenant()->get('foo'));
} }
/** @test */ /** @test */
@ -40,7 +33,7 @@ class TenantStorageTest extends TestCase
{ {
tenancy()->put('foo', 'bar'); tenancy()->put('foo', 'bar');
$this->assertSame('bar', $this->storage->get(tenant('uuid'), 'foo')); $this->assertSame('bar', tenant()->get('foo'));
} }
/** @test */ /** @test */
@ -91,4 +84,26 @@ class TenantStorageTest extends TestCase
$this->assertNotSame($vals, tenancy()->get($keys)); $this->assertNotSame($vals, tenancy()->get($keys));
$this->assertFalse(array_intersect($data, tenant()->tenant) == $data); // assert array not subset $this->assertFalse(array_intersect($data, tenant()->tenant) == $data); // assert array not subset
} }
/** @test */
public function arrays_can_be_stored()
{
tenant()->put('foo', [1, 2]);
$this->assertSame([1, 2], tenant()->get('foo'));
}
/** @test */
public function put_returns_the_value_when_two_arguments_are_used()
{
$this->assertSame('bar', tenant()->put('foo', 'bar'));
}
/** @test */
public function put_returns_the_key_value_pairs_when_a_single_argument_is_used()
{
$value = ['foo' => 'bar', 'abc' => 'xyz'];
$this->assertSame($value, tenancy()->put($value));
}
} }