diff --git a/README.md b/README.md index 77218892..f893f82c 100644 --- a/README.md +++ b/README.md @@ -217,6 +217,32 @@ You can use the `tenancy()` and `tenant()` helpers to resolve `Stancl\Tenancy\Te ] ``` +You can also put data into the storage during the tenant creation process: + +```php +>>> tenant()->create('dev.localhost', [ + 'plan' => 'basic' +]) +=> [ + "uuid" => "49670df0-1a87-11e9-b7ba-cf5353777957", + "domain" => "dev.localhost", + "plan" => "basic", + ] +``` + +If you want to specify the tenant's database name, set the `tenancy.database_name_key` configuration key to the name of the key that is used to specify the database name in the tenant storage. You must use a name that you won't use for storing other data, so it's recommended to avoid names like `database` and use names like `_stancl_tenancy_database_name` instead. Then just give the key a value during the tenant creation process: + +```php +>>> tenant()->create('example.com', [ + '_stancl_tenancy_database_name' => 'example_com' +]) +=> [ + "uuid" => "49670df0-1a87-11e9-b7ba-cf5353777957", + "domain" => "example.com", + "_stancl_tenancy_database_name" => "example_com", + ] +``` + When you create a new tenant, you can [migrate](#tenant-migrations) their database like this: ```php diff --git a/src/TenantManager.php b/src/TenantManager.php index b76a84a8..bbb9897b 100644 --- a/src/TenantManager.php +++ b/src/TenantManager.php @@ -70,7 +70,14 @@ class TenantManager return $tenant; } - public function create(string $domain = null): array + /** + * Create a tenant. + * + * @param string $domain + * @param array $data + * @return array + */ + public function create(string $domain = null, array $data = []): array { $domain = $domain ?: $this->currentDomain(); @@ -79,6 +86,13 @@ class TenantManager } $tenant = $this->jsonDecodeArrayValues($this->storage->createTenant($domain, (string) \Webpatser\Uuid\Uuid::generate(1, $domain))); + + if ($data) { + $this->put($data, null, $tenant['uuid']); + + $tenant = array_merge($tenant, $data); + } + $this->database->create($this->getDatabaseName($tenant)); return $tenant; @@ -168,6 +182,12 @@ class TenantManager { $tenant = $tenant ?: $this->tenant; + if ($key = $this->app['config']['tenancy.database_name_key']) { + if (isset($tenant[$key])) { + return $tenant[$key]; + } + } + return $this->app['config']['tenancy.database.prefix'] . $tenant['uuid'] . $this->app['config']['tenancy.database.suffix']; } diff --git a/src/config/tenancy.php b/src/config/tenancy.php index c33f8f63..6ebd7251 100644 --- a/src/config/tenancy.php +++ b/src/config/tenancy.php @@ -43,4 +43,5 @@ return [ ], 'queue_database_creation' => false, 'queue_database_deletion' => false, + 'database_name_key' => null, ];