diff --git a/src/Contracts/StorageDriver.php b/src/Contracts/StorageDriver.php index 4db34358..027ff8d9 100644 --- a/src/Contracts/StorageDriver.php +++ b/src/Contracts/StorageDriver.php @@ -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; diff --git a/src/DatabaseManager.php b/src/DatabaseManager.php index 02ff054e..002cb3df 100644 --- a/src/DatabaseManager.php +++ b/src/DatabaseManager.php @@ -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 diff --git a/src/Tenant.php b/src/Tenant.php index 95408e0e..480422f7 100644 --- a/src/Tenant.php +++ b/src/Tenant.php @@ -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); } }