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

TenantManager storage

This commit is contained in:
Samuel Štancl 2019-09-08 17:18:31 +02:00
parent de54b5708c
commit 4872197fb6
3 changed files with 45 additions and 4 deletions

View file

@ -7,6 +7,9 @@ namespace Stancl\Tenancy\Contracts;
use Stancl\Tenancy\Tenant;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
// todo this class now manages types (json encoding)
// make sure ids are not json encoded
interface StorageDriver
{
public function createTenant(Tenant $tenant): void;

View file

@ -45,7 +45,6 @@ final class DatabaseManager
/**
* Create a database.
* @todo Should this handle prefixes?
*
* @param string $name
* @param string $driver
@ -71,7 +70,6 @@ final class DatabaseManager
/**
* Delete a database.
* @todo Should this handle prefixes?
*
* @param string $name
* @param string $driver

View file

@ -16,7 +16,7 @@ class Tenant implements ArrayAccess
use Traits\HasArrayAccess;
/**
* Tenant data.
* Tenant data. A "cache" of tenant storage.
*
* @var array
*/
@ -99,8 +99,48 @@ class Tenant implements ArrayAccess
return $this['_tenancy_db_name'] ?? $this->app['config']['tenancy.database.prefix'] . $this->uuid . $this->app['config']['tenancy.database.suffix'];
}
/**
* Get a value from tenant storage.
*
* @param string|string[] $keys
* @return void
*/
public function get($keys)
{
if (is_array($keys)) {
if (array_intersect(array_keys($this->data), $keys)) { // if all keys are present in cache
return array_reduce($keys, function ($pairs, $key) {
$pairs[$key] = $this->data[$key];
return $pairs;
}, []);
}
return $this->storage->getMany($keys);
}
if (! isset($this->data[$keys])) {
$this->data[$keys] = $this->storage->get($keys);
}
return $this->data[$keys];
}
public function put($key, $value = null): self
{
if (is_array($key)) {
$this->storage->putMany($key);
foreach ($key as $k => $v) { // Add to cache
$this->data[$k] = $v;
}
} else {
$this->storage->put($key, $value);
$this->data[$key] = $value;
}
return $this;
}
public function __get($name)
{
return $this->data[$name] ?? null;
return $this->get($name);
}
}