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

Fix bug with JSON encoding

This commit is contained in:
Samuel Štancl 2019-08-19 10:55:48 +02:00
parent 8456280ac4
commit 314b2d3003
2 changed files with 37 additions and 5 deletions

View file

@ -273,16 +273,21 @@ final class TenantManager
{ {
$uuid = $uuid ?: $this->tenant['uuid']; $uuid = $uuid ?: $this->tenant['uuid'];
// todo make this cache work with arrays
if (\array_key_exists('uuid', $this->tenant) && $uuid === $this->tenant['uuid'] && if (\array_key_exists('uuid', $this->tenant) && $uuid === $this->tenant['uuid'] &&
! \is_array($key) && \array_key_exists($key, $this->tenant)) { ! \is_array($key) && \array_key_exists($key, $this->tenant)) {
return $this->tenant[$key]; return $this->tenant[$key];
} }
if (\is_array($key)) { if (\is_array($key)) {
return $this->jsonDecodeArrayValues($this->storage->getMany($uuid, $key)); $data = $this->storage->getMany($uuid, $key);
$data = $this->useJson() ? $this->jsonDecodeArrayValues($data) : $data;
return $data;
} }
return \json_decode($this->storage->get($uuid, $key), true); $data = $this->storage->get($uuid, $key);
$data = $this->useJson() ? \json_decode($data, true) : $data;
return $data;
} }
/** /**
@ -319,7 +324,13 @@ final class TenantManager
} }
if (! \is_null($value)) { if (! \is_null($value)) {
return $target[$key] = \json_decode($this->storage->put($uuid, $key, \json_encode($value)), true); if ($this->useJson()) {
$data = \json_decode($this->storage->put($uuid, $key, \json_encode($value)), true);
} else {
$data = $this->storage->put($uuid, $key, $value);
}
return $target[$key] = $data;
} }
if (! \is_array($key)) { if (! \is_array($key)) {
@ -328,10 +339,15 @@ final class TenantManager
foreach ($key as $k => $v) { foreach ($key as $k => $v) {
$target[$k] = $v; $target[$k] = $v;
$key[$k] = \json_encode($v);
$v = $this->useJson() ? \json_decode($v) : $v;
$key[$k] = $v;
} }
return $this->jsonDecodeArrayValues($this->storage->putMany($uuid, $key)); $data = $this->storage->putMany($uuid, $key);
$data = $this->useJson() ? $this->jsonDecodeArrayValues($data) : $data;
return $data;
} }
/** /**

View file

@ -148,4 +148,20 @@ class TenantStorageTest extends TestCase
config(['tenancy.storage.db.connection' => 'foo']); config(['tenancy.storage.db.connection' => 'foo']);
$this->assertSame('foo', (new Tenant)->getConnectionName()); $this->assertSame('foo', (new Tenant)->getConnectionName());
} }
/** @test */
public function retrieving_data_without_cache_works()
{
tenant()->create('foo.localhost');
tenancy()->init('foo.localhost');
tenancy()->put('foo', 'bar');
$this->assertSame('bar', tenancy()->get('foo'));
$this->assertSame(['bar'], tenancy()->get(['foo']));
tenancy()->end();
tenancy()->init('foo.localhost');
$this->assertSame('bar', tenancy()->get('foo'));
$this->assertSame(['bar'], tenancy()->get(['foo']));
}
} }