mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 18:44:03 +00:00
parent
a0192a941e
commit
6dad6ce49a
2 changed files with 64 additions and 8 deletions
|
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||||
namespace Stancl\Tenancy\Features;
|
namespace Stancl\Tenancy\Features;
|
||||||
|
|
||||||
use Illuminate\Contracts\Config\Repository;
|
use Illuminate\Contracts\Config\Repository;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Support\Facades\Event;
|
||||||
use Stancl\Tenancy\Contracts\Feature;
|
use Stancl\Tenancy\Contracts\Feature;
|
||||||
use Stancl\Tenancy\Contracts\Tenant;
|
use Stancl\Tenancy\Contracts\Tenant;
|
||||||
|
|
@ -27,10 +28,6 @@ class TenantConfig implements Feature
|
||||||
public function __construct(Repository $config)
|
public function __construct(Repository $config)
|
||||||
{
|
{
|
||||||
$this->config = $config;
|
$this->config = $config;
|
||||||
|
|
||||||
foreach (static::$storageToConfigMap as $configKey) {
|
|
||||||
$this->originalConfig[$configKey] = $this->config[$configKey];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function bootstrap(Tenancy $tenancy): void
|
public function bootstrap(Tenancy $tenancy): void
|
||||||
|
|
@ -46,18 +43,31 @@ class TenantConfig implements Feature
|
||||||
|
|
||||||
public function setTenantConfig(Tenant $tenant): void
|
public function setTenantConfig(Tenant $tenant): void
|
||||||
{
|
{
|
||||||
|
/** @var Tenant|Model $tenant */
|
||||||
|
|
||||||
foreach (static::$storageToConfigMap as $storageKey => $configKey) {
|
foreach (static::$storageToConfigMap as $storageKey => $configKey) {
|
||||||
$override = $tenant->$storageKey ?? null;
|
$override = $tenant->getAttribute($storageKey);
|
||||||
|
|
||||||
if (! is_null($override)) {
|
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
|
public function unsetTenantConfig(): void
|
||||||
{
|
{
|
||||||
foreach (static::$storageToConfigMap as $configKey) {
|
foreach ($this->originalConfig as $key => $value) {
|
||||||
$this->config[$configKey] = $this->originalConfig[$configKey];
|
$this->config[$key] = $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,13 @@ use Stancl\Tenancy\Tests\TestCase;
|
||||||
|
|
||||||
class TenantConfigTest extends TestCase
|
class TenantConfigTest extends TestCase
|
||||||
{
|
{
|
||||||
|
public function tearDown(): void
|
||||||
|
{
|
||||||
|
TenantConfig::$storageToConfigMap = [];
|
||||||
|
|
||||||
|
parent::tearDown();
|
||||||
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function config_is_merged_and_removed()
|
public function config_is_merged_and_removed()
|
||||||
{
|
{
|
||||||
|
|
@ -45,4 +52,43 @@ class TenantConfigTest extends TestCase
|
||||||
'private' => null,
|
'private' => null,
|
||||||
], config('services.paypal'));
|
], 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'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue