mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 15:54:03 +00:00
[2.x] Tenant config (#145)
* TenantConfig first draft * Apply fixes from StyleCI * Add unsetTenantConfig * Fix DB storage driver bug, add regression test (tenant_data_can_be_set_during_creation) * Add tests & config keys * Apply fixes from StyleCI
This commit is contained in:
parent
d70e561106
commit
56a2bdf5af
5 changed files with 109 additions and 1 deletions
|
|
@ -79,9 +79,13 @@ return [
|
||||||
// Features are classes that provide additional functionality
|
// Features are classes that provide additional functionality
|
||||||
// not needed for tenancy to be bootstrapped. They are run
|
// not needed for tenancy to be bootstrapped. They are run
|
||||||
// regardless of whether tenancy has been initialized.
|
// regardless of whether tenancy has been initialized.
|
||||||
|
Stancl\Tenancy\Features\TenantConfig::class,
|
||||||
Stancl\Tenancy\Features\TelescopeTags::class,
|
Stancl\Tenancy\Features\TelescopeTags::class,
|
||||||
Stancl\Tenancy\Features\TenantRedirect::class,
|
Stancl\Tenancy\Features\TenantRedirect::class,
|
||||||
],
|
],
|
||||||
|
'storage_to_config_map' => [
|
||||||
|
// 'paypal_api_key' => 'services.paypal.api_key',
|
||||||
|
],
|
||||||
'home_url' => '/app',
|
'home_url' => '/app',
|
||||||
'migrate_after_creation' => false, // run migrations after creating a tenant
|
'migrate_after_creation' => false, // run migrations after creating a tenant
|
||||||
'delete_database_after_tenant_deletion' => false, // delete the tenant's database after deleting the tenant
|
'delete_database_after_tenant_deletion' => false, // delete the tenant's database after deleting the tenant
|
||||||
|
|
|
||||||
58
src/Features/TenantConfig.php
Normal file
58
src/Features/TenantConfig.php
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Features;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Application;
|
||||||
|
use Stancl\Tenancy\Contracts\Feature;
|
||||||
|
use Stancl\Tenancy\Tenant;
|
||||||
|
use Stancl\Tenancy\TenantManager;
|
||||||
|
|
||||||
|
class TenantConfig implements Feature
|
||||||
|
{
|
||||||
|
/** @var Application */
|
||||||
|
protected $app;
|
||||||
|
|
||||||
|
/** @var array */
|
||||||
|
public $originalConfig = [];
|
||||||
|
|
||||||
|
public function __construct(Application $app)
|
||||||
|
{
|
||||||
|
$this->app = $app;
|
||||||
|
|
||||||
|
foreach ($this->getStorageToConfigMap() as $configKey) {
|
||||||
|
$this->originalConfig[$configKey] = $this->app['config'][$configKey];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function bootstrap(TenantManager $tenantManager): void
|
||||||
|
{
|
||||||
|
$tenantManager->eventListener('bootstrapped', function (TenantManager $manager) {
|
||||||
|
$this->setTenantConfig($manager->getTenant());
|
||||||
|
});
|
||||||
|
|
||||||
|
$tenantManager->eventListener('ended', function () {
|
||||||
|
$this->unsetTenantConfig();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setTenantConfig(Tenant $tenant): void
|
||||||
|
{
|
||||||
|
foreach ($this->getStorageToConfigMap() as $storageKey => $configKey) {
|
||||||
|
$this->app['config'][$configKey] = $tenant->get($storageKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function unsetTenantConfig(): void
|
||||||
|
{
|
||||||
|
foreach ($this->getStorageToConfigMap() as $configKey) {
|
||||||
|
$this->app['config'][$configKey] = $this->originalConfig[$configKey];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getStorageToConfigMap(): array
|
||||||
|
{
|
||||||
|
return $this->app['config']['tenancy.storage_to_config_map'] ?? [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -80,7 +80,7 @@ class DatabaseStorageDriver implements StorageDriver
|
||||||
public function createTenant(Tenant $tenant): void
|
public function createTenant(Tenant $tenant): void
|
||||||
{
|
{
|
||||||
$this->centralDatabase->transaction(function () use ($tenant) {
|
$this->centralDatabase->transaction(function () use ($tenant) {
|
||||||
Tenants::create(['id' => $tenant->id, 'data' => '{}'])->toArray();
|
Tenants::create(['id' => $tenant->id, 'data' => json_encode($tenant->data)])->toArray();
|
||||||
|
|
||||||
$domainData = [];
|
$domainData = [];
|
||||||
foreach ($tenant->domains as $domain) {
|
foreach ($tenant->domains as $domain) {
|
||||||
|
|
|
||||||
|
|
@ -94,4 +94,15 @@ class TenantClassTest extends TestCase
|
||||||
$this->expectException(\BadMethodCallException::class);
|
$this->expectException(\BadMethodCallException::class);
|
||||||
$tenant->sdjigndfgnjdfgj();
|
$tenant->sdjigndfgnjdfgj();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function tenant_data_can_be_set_during_creation()
|
||||||
|
{
|
||||||
|
Tenant::new()->withData(['foo' => 'bar'])->save();
|
||||||
|
|
||||||
|
$data = tenancy()->all()->first()->data;
|
||||||
|
unset($data['id']);
|
||||||
|
|
||||||
|
$this->assertSame(['foo' => 'bar'], $data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
35
tests/TenantConfigTest.php
Normal file
35
tests/TenantConfigTest.php
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Tests;
|
||||||
|
|
||||||
|
class TenantConfigTest extends TestCase
|
||||||
|
{
|
||||||
|
public $autoInitTenancy = false;
|
||||||
|
public $autoCreateTenant = false;
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function config_is_merged_and_removed()
|
||||||
|
{
|
||||||
|
$this->assertSame(null, config('services.paypal'));
|
||||||
|
config(['tenancy.storage_to_config_map' => [
|
||||||
|
'paypal_api_public' => 'services.paypal.public',
|
||||||
|
'paypal_api_private' => 'services.paypal.private',
|
||||||
|
]]);
|
||||||
|
|
||||||
|
tenancy()->create('foo.localhost', [
|
||||||
|
'paypal_api_public' => 'foo',
|
||||||
|
'paypal_api_private' => 'bar',
|
||||||
|
]);
|
||||||
|
|
||||||
|
tenancy()->init('foo.localhost');
|
||||||
|
$this->assertSame(['public' => 'foo', 'private' => 'bar'], config('services.paypal'));
|
||||||
|
|
||||||
|
tenancy()->end();
|
||||||
|
$this->assertSame([
|
||||||
|
'public' => null,
|
||||||
|
'private' => null,
|
||||||
|
], config('services.paypal'));
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue