1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 19:14:04 +00:00

Add defaultPrefix property

This commit is contained in:
lukinovec 2023-04-12 09:10:23 +02:00
parent e894c782ab
commit 9d9e2b32ce

View file

@ -14,6 +14,7 @@ use Stancl\Tenancy\Contracts\Tenant;
class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper
{ {
protected string|null $defaultPrefix = null;
public static array $originalPrefixes = []; // E.g. 'redis' => 'redis_prefix_' (if not specified, use config('cache.prefix') as the default) public static array $originalPrefixes = []; // E.g. 'redis' => 'redis_prefix_' (if not specified, use config('cache.prefix') as the default)
public static array $tenantCacheStores = []; // E.g. 'redis' public static array $tenantCacheStores = []; // E.g. 'redis'
public static array $prefixGenerators = [ public static array $prefixGenerators = [
@ -28,8 +29,10 @@ class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper
public function bootstrap(Tenant $tenant): void public function bootstrap(Tenant $tenant): void
{ {
$this->defaultPrefix = $this->config->get('cache.prefix');
foreach (static::$tenantCacheStores as $store) { foreach (static::$tenantCacheStores as $store) {
static::$originalPrefixes[$store] ??= $this->config->get('cache.prefix'); static::$originalPrefixes[$store] ??= $this->defaultPrefix;
} }
foreach (static::$tenantCacheStores as $store) { foreach (static::$tenantCacheStores as $store) {
@ -56,6 +59,10 @@ class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper
// It is needed when a call to the facade has been made before bootstrapping tenancy // It is needed when a call to the facade has been made before bootstrapping tenancy
// The facade has its own cache, separate from the container // The facade has its own cache, separate from the container
Cache::clearResolvedInstances(); Cache::clearResolvedInstances();
// Now that the store uses the passed prefix
// Set the configured prefix back to the default one
$this->config->set('cache.prefix', $this->defaultPrefix);
} }
public function getStorePrefix(string $store, Tenant $tenant): string public function getStorePrefix(string $store, Tenant $tenant): string
@ -64,7 +71,7 @@ class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper
return static::$prefixGenerators[$store]($tenant); return static::$prefixGenerators[$store]($tenant);
} }
return static::$originalPrefixes[$store] . $this->config->get('tenancy.cache.prefix_base') . $tenant->getTenantKey(); return (static::$originalPrefixes[$store] ?? $this->defaultPrefix) . $this->config->get('tenancy.cache.prefix_base') . $tenant->getTenantKey();
} }
public static function generatePrefixUsing(string $store, Closure $prefixGenerator): void public static function generatePrefixUsing(string $store, Closure $prefixGenerator): void