diff --git a/assets/config.php b/assets/config.php index 79dec526..ce1727db 100644 --- a/assets/config.php +++ b/assets/config.php @@ -57,6 +57,5 @@ return [ ], 'queue_database_creation' => false, 'queue_database_deletion' => false, - 'database_name_key' => null, 'unique_id_generator' => 'Stancl\Tenancy\UUIDGenerator', ]; diff --git a/src/Commands/Migrate.php b/src/Commands/Migrate.php index 30290335..cb98d7e2 100644 --- a/src/Commands/Migrate.php +++ b/src/Commands/Migrate.php @@ -54,7 +54,7 @@ class Migrate extends MigrateCommand // See Illuminate\Database\Migrations\DatabaseMigrationRepository::getConnection. // Database connections are cached by Illuminate\Database\ConnectionResolver. - $connectionName = "tenant{$tenant['uuid']}"; + $connectionName = "tenant{$tenant['uuid']}"; // todo use Illuminate DatabaseManager reconnect()? $this->input->setOption('database', $connectionName); $this->database->connectToTenant($tenant, $connectionName); diff --git a/src/DatabaseManagerv2.php b/src/DatabaseManagerv2.php new file mode 100644 index 00000000..1301c8f5 --- /dev/null +++ b/src/DatabaseManagerv2.php @@ -0,0 +1,51 @@ +database = $database; + } + + public function connect(Tenant $tenant) + { + $connection = $tenant->getConnectionName(); // todo + + $this->createTenantConnection($tenant->getDatabaseName(), $connection); + $this->switchConnection($connection); + } + + public function reconnect() + { + $this->switchConnection($this->originalDefaultConnection); + } + + public function createTenantConnection(string $databaseName, string $connectionName = null) + { + $connectionName = $connectionName ?: $this->defaultTenantConnectionName; + + // Create the database connection. + $based_on = config('tenancy.database.based_on') ?: config('database.default'); + config()->set([ + "database.connections.$connectionName" => config('database.connections.' . $based_on), + ]); + + // Change DB name + $databaseName = $this->getDriver($connectionName) === 'sqlite' ? database_path($databaseName) : $databaseName; + config()->set(["database.connections.$connectionName.database" => $databaseName]); + } + + public function switchConnection($connection) + { + $this->database->purge(); + $this->database->reconnect($connection); + $this->database->setDefaultConnection($connection); + } +} \ No newline at end of file diff --git a/src/Interfaces/StorageDriver.php b/src/Interfaces/StorageDriver.php deleted file mode 100644 index 22fae59b..00000000 --- a/src/Interfaces/StorageDriver.php +++ /dev/null @@ -1,29 +0,0 @@ -persisted) { $this->manager->addTenant($this); } else { $this->manager->updateTenant($this); } + + return $this; + } + + public function getDatabaseName() + { + return $this['_tenancy_database'] ?? $this->app['config']['tenancy.database.prefix'] . $this->uuid . $this->app['config']['tenancy.database.suffix']; } public function __get($name) { - return $this->data[$name]; + return $this->data[$name] ?? null; } } \ No newline at end of file diff --git a/src/TenantManagerv2.php b/src/TenantManagerv2.php index 92a9faab..2a4160c0 100644 --- a/src/TenantManagerv2.php +++ b/src/TenantManagerv2.php @@ -4,8 +4,32 @@ declare(strict_types=1); namespace Stancl\Tenancy; -final class TenantManager +use Illuminate\Foundation\Application; + +/** + * @internal Class is subject to breaking changes in minor and patch versions. + */ +class TenantManagerv2 { + /** + * The current tenant. + * + * @var Tenant + */ + public $tenant; + + /** @var Application */ + private $app; + + /** @var Contracts\StorageDriver */ + private $storage; + + public function __construct(Application $app, Contracts\StorageDriver $storage) + { + $this->app = $app; + $this->storage = $storage; + } + public function addTenant(Tenant $tenant): self { $this->storage->addTenant($tenant); @@ -19,4 +43,38 @@ final class TenantManager return $this; } + + public function initializeTenancy(Tenant $tenant): self + { + $this->bootstrapTenancy($tenant); + $this->setTenant($tenant); + + return $this; + } + + public function bootstrapTenancy(Tenant $tenant): self + { + foreach($this->tenancyBootstrappers() as $bootstrapper) { + $bootstrapper::start($tenant); + } + + return $this; + } + + public function setTenant(Tenant $tenant): self + { + $this->app->instance(Tenant::class, $tenant); + + return $this; + } + + /** + * Return a list of TenancyBoostrappers. + * + * @return Contracts\TenancyBootstrapper[] + */ + public function tenancyBootstrappers(): array + { + return config('tenancy.bootstrappers'); + } } \ No newline at end of file