diff --git a/src/Features/TenantConfig.php b/src/Features/TenantConfig.php index 7d5340e8..db4eebc8 100644 --- a/src/Features/TenantConfig.php +++ b/src/Features/TenantConfig.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Stancl\Tenancy\Features; use Illuminate\Contracts\Config\Repository; +use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Event; use Stancl\Tenancy\Contracts\Feature; use Stancl\Tenancy\Contracts\Tenant; @@ -27,10 +28,6 @@ class TenantConfig implements Feature public function __construct(Repository $config) { $this->config = $config; - - foreach (static::$storageToConfigMap as $configKey) { - $this->originalConfig[$configKey] = $this->config[$configKey]; - } } public function bootstrap(Tenancy $tenancy): void @@ -46,18 +43,31 @@ class TenantConfig implements Feature public function setTenantConfig(Tenant $tenant): void { + /** @var Tenant|Model $tenant */ + foreach (static::$storageToConfigMap as $storageKey => $configKey) { - $override = $tenant->$storageKey ?? null; + $override = $tenant->getAttribute($storageKey); + if (! is_null($override)) { - $this->config[$configKey] = $override; + if (is_array($configKey)) { + foreach ($configKey as $key) { + $this->originalConfig[$key] = $this->originalConfig[$key] ?? $this->config[$key]; + + $this->config[$key] = $override; + } + } else { + $this->originalConfig[$configKey] = $this->originalConfig[$configKey] ?? $this->config[$configKey]; + + $this->config[$configKey] = $override; + } } } } public function unsetTenantConfig(): void { - foreach (static::$storageToConfigMap as $configKey) { - $this->config[$configKey] = $this->originalConfig[$configKey]; + foreach ($this->originalConfig as $key => $value) { + $this->config[$key] = $value; } } } diff --git a/tests/Features/TenantConfigTest.php b/tests/Features/TenantConfigTest.php index aebef4ad..37e26198 100644 --- a/tests/Features/TenantConfigTest.php +++ b/tests/Features/TenantConfigTest.php @@ -15,6 +15,13 @@ use Stancl\Tenancy\Tests\TestCase; class TenantConfigTest extends TestCase { + public function tearDown(): void + { + TenantConfig::$storageToConfigMap = []; + + parent::tearDown(); + } + /** @test */ public function config_is_merged_and_removed() { @@ -45,4 +52,43 @@ class TenantConfigTest extends TestCase 'private' => null, ], config('services.paypal')); } + + /** @test */ + public function the_value_can_be_set_to_multiple_config_keys() + { + $this->assertSame(null, config('services.paypal')); + config([ + 'tenancy.features' => [TenantConfig::class], + 'tenancy.bootstrappers' => [], + ]); + Event::listen(TenancyInitialized::class, BootstrapTenancy::class); + Event::listen(TenancyEnded::class, RevertToCentralContext::class); + + TenantConfig::$storageToConfigMap = [ + 'paypal_api_public' => [ + 'services.paypal.public1', + 'services.paypal.public2', + ], + 'paypal_api_private' => 'services.paypal.private', + ]; + + $tenant = Tenant::create([ + 'paypal_api_public' => 'foo', + 'paypal_api_private' => 'bar', + ]); + + tenancy()->initialize($tenant); + $this->assertSame([ + 'public1' => 'foo', + 'public2' => 'foo', + 'private' => 'bar', + ], config('services.paypal')); + + tenancy()->end(); + $this->assertSame([ + 'public1' => null, + 'public2' => null, + 'private' => null, + ], config('services.paypal')); + } }