diff --git a/src/StorageDrivers/DatabaseStorageDriver.php b/src/StorageDrivers/DatabaseStorageDriver.php index 0e6bca9c..d4a20d9a 100644 --- a/src/StorageDrivers/DatabaseStorageDriver.php +++ b/src/StorageDrivers/DatabaseStorageDriver.php @@ -7,6 +7,10 @@ use Stancl\Tenancy\Interfaces\StorageDriver; class DatabaseStorageDriver implements StorageDriver { + public $useJson = false; + + // todo use an instance of tenant model? + public function identifyTenant(string $domain): array { $id = $this->getTenantIdByDomain($domain); @@ -50,13 +54,7 @@ class DatabaseStorageDriver implements StorageDriver public function getAllTenants(array $uuids = []): array { - if ($uuids) { - $tenants = Tenant::find($uuids); - } else { - $tenants = Tenant::all(); - } - - return $tenants->toArray(); + return Tenant::getAllTenants($uuids)->toArray(); } public function get(string $uuid, string $key) diff --git a/src/Tenant.php b/src/Tenant.php index 15f714f5..6405e31a 100644 --- a/src/Tenant.php +++ b/src/Tenant.php @@ -18,19 +18,34 @@ class Tenant extends Model */ private $dataObject; - public function dataColumn() + public static function dataColumn() { return config('tenancy.storage.db.data_column', 'data'); } - public function customColumns() + public static function customColumns() { return config('tenancy.storage.db.custom_columns', []); } public function getConnectionName() { - return config('tenancy.storage.db.connection'); + return config('tenancy.storage.db.connection', 'central'); + } + + public static function getAllTenants(array $uuids) + { + $tenants = $uuids ? static::findMany($uuids) : static::all(); + + return $tenants->map(function ($tenant) { + $tenant = (array) $tenant->attributes; + foreach (json_decode($tenant[static::dataColumn()], true) as $key => $value) { + $tenant[$key] = $value; + } + unset($tenant[static::dataColumn()]); // todo what if 'data' key is stored in tenant storage? + + return $tenant; + })->toBase(); } public function getFromData(string $key) diff --git a/src/TenantManager.php b/src/TenantManager.php index bc8ed11d..64fd4a99 100644 --- a/src/TenantManager.php +++ b/src/TenantManager.php @@ -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']); @@ -227,10 +230,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 +344,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). * diff --git a/tests/TestCase.php b/tests/TestCase.php index 500da97b..90243996 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -25,6 +25,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase Redis::connection('cache')->flushdb(); config(['database.default' => 'central']); + $this->loadLaravelMigrations(); $this->loadMigrationsFrom(realpath(__DIR__ . '/../src/assets/migrations')); if ($this->autoCreateTenant) {