mirror of
https://github.com/archtechx/tenancy.git
synced 2026-05-06 22:44:04 +00:00
The feature was pretty much a soft-bootstrapper -- it listened to both Bootstrapped and Reverted. Bootstrappers have a few more protections in terms of error handling and safe reverting, so there's no point in (badly) re-implementing bootstrapper functionality within TenantConfig just so it could be a Feature. Going forward, all Features should be things that are mostly agnostic of the tenant state, and especially they should not use bootstrapped/ reverted events. Bootstrappers are simply more appropriate and safe.
54 lines
1.6 KiB
PHP
54 lines
1.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Stancl\Tenancy\Bootstrappers;
|
|
|
|
use Illuminate\Config\Repository;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Arr;
|
|
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
|
use Stancl\Tenancy\Contracts\Tenant;
|
|
|
|
class TenantConfigBootstrapper implements TenancyBootstrapper
|
|
{
|
|
public array $originalConfig = [];
|
|
|
|
/** @var array<string, string|array> */
|
|
public static array $storageToConfigMap = [
|
|
// 'paypal_api_key' => 'services.paypal.api_key',
|
|
];
|
|
|
|
public function __construct(
|
|
protected Repository $config,
|
|
) {}
|
|
|
|
public function bootstrap(Tenant $tenant): void
|
|
{
|
|
foreach (static::$storageToConfigMap as $storageKey => $configKey) {
|
|
/** @var Tenant&Model $tenant */
|
|
$override = Arr::get($tenant, $storageKey);
|
|
|
|
if (! is_null($override)) {
|
|
if (is_array($configKey)) {
|
|
foreach ($configKey as $key) {
|
|
$this->originalConfig[$key] = $this->originalConfig[$key] ?? $this->config->get($key);
|
|
|
|
$this->config->set($key, $override);
|
|
}
|
|
} else {
|
|
$this->originalConfig[$configKey] = $this->originalConfig[$configKey] ?? $this->config->get($configKey);
|
|
|
|
$this->config->set($configKey, $override);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public function revert(): void
|
|
{
|
|
foreach ($this->originalConfig as $key => $value) {
|
|
$this->config->set($key, $value);
|
|
}
|
|
}
|
|
}
|