1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 15:34:04 +00:00

Merge branch '1.x' into db-storage-driver

This commit is contained in:
Samuel Štancl 2019-08-13 22:23:36 +02:00
commit 23dd42899a
6 changed files with 120 additions and 10 deletions

View file

@ -0,0 +1,8 @@
<?php
namespace Stancl\Tenancy\Exceptions;
class CannotChangeUuidOrDomainException extends \Exception
{
protected $message = 'Uuid and domain cannot be changed.';
}

View file

@ -22,15 +22,13 @@ class TenancyServiceProvider extends ServiceProvider
*/
public function boot()
{
if ($this->app->runningInConsole()) {
$this->commands([
Run::class,
Seed::class,
Migrate::class,
Rollback::class,
TenantList::class,
]);
}
$this->commands([
Run::class,
Seed::class,
Migrate::class,
Rollback::class,
TenantList::class,
]);
$this->publishes([
__DIR__ . '/assets/config.php' => config_path('tenancy.php'),

View file

@ -5,6 +5,7 @@ namespace Stancl\Tenancy;
use Stancl\Tenancy\Interfaces\StorageDriver;
use Stancl\Tenancy\Traits\BootstrapsTenancy;
use Illuminate\Contracts\Foundation\Application;
use Stancl\Tenancy\Exceptions\CannotChangeUuidOrDomainException;
class TenantManager
{
@ -70,7 +71,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 +87,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 +183,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'];
}
@ -254,6 +275,15 @@ class TenantManager
*/
public function put($key, $value = null, string $uuid = null)
{
if (in_array($key, ['uuid', 'domain'], true) || (
is_array($key) && (
in_array('uuid', array_keys($key), true) ||
in_array('domain', array_keys($key), true)
)
)) {
throw new CannotChangeUuidOrDomainException;
}
if (\is_null($uuid)) {
if (! isset($this->tenant['uuid'])) {
throw new \Exception('No UUID supplied (and no tenant is currently identified).');

View file

@ -47,4 +47,5 @@ return [
],
'queue_database_creation' => false,
'queue_database_deletion' => false,
'database_name_key' => null,
];