From 39e62c7dcf2e613a309d133af0e457076290bf62 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Wed, 12 Apr 2023 08:23:31 +0200 Subject: [PATCH] Make original prefixes customizable --- .../PrefixCacheTenancyBootstrapper.php | 14 +++++++------- tests/PrefixCacheBootstrapperTest.php | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/Bootstrappers/PrefixCacheTenancyBootstrapper.php b/src/Bootstrappers/PrefixCacheTenancyBootstrapper.php index 7a84b39d..e9c4fe01 100644 --- a/src/Bootstrappers/PrefixCacheTenancyBootstrapper.php +++ b/src/Bootstrappers/PrefixCacheTenancyBootstrapper.php @@ -14,7 +14,7 @@ use Stancl\Tenancy\Contracts\Tenant; class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper { - protected array $originalPrefixes = []; // E.g. 'redis' => 'redis_prefix_' + public static array $originalPrefixes = []; // E.g. 'redis' => 'redis_prefix_' public static array $tenantCacheStores = []; // E.g. 'redis' public static array $prefixGenerators = [ // driverName => Closure(Tenant $tenant) @@ -28,22 +28,22 @@ class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper public function bootstrap(Tenant $tenant): void { - $originalPrefix = $this->config->get('cache.prefix'); + foreach (static::$tenantCacheStores as $store) { + static::$originalPrefixes[$store] ??= $this->config->get('cache.prefix'); + } foreach (static::$tenantCacheStores as $store) { - $this->originalPrefixes[$store] = $originalPrefix; - $this->setCachePrefix($store, $this->getStorePrefix($store, $tenant)); } } public function revert(): void { - foreach ($this->originalPrefixes as $driver => $prefix) { + foreach (static::$originalPrefixes as $driver => $prefix) { $this->setCachePrefix($driver, $prefix); } - $this->originalPrefixes = []; + static::$originalPrefixes = []; } protected function setCachePrefix(string $driver, string|null $prefix): void @@ -64,7 +64,7 @@ class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper return static::$prefixGenerators[$store]($tenant); } - return $this->originalPrefixes[$store] . $this->config->get('tenancy.cache.prefix_base') . $tenant->getTenantKey(); + return static::$originalPrefixes[$store] . $this->config->get('tenancy.cache.prefix_base') . $tenant->getTenantKey(); } public static function generatePrefixUsing(string $store, Closure $prefixGenerator): void diff --git a/tests/PrefixCacheBootstrapperTest.php b/tests/PrefixCacheBootstrapperTest.php index d9a0afb5..d64502f7 100644 --- a/tests/PrefixCacheBootstrapperTest.php +++ b/tests/PrefixCacheBootstrapperTest.php @@ -23,6 +23,7 @@ beforeEach(function () { PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver]; PrefixCacheTenancyBootstrapper::$prefixGenerators = []; + PrefixCacheTenancyBootstrapper::$originalPrefixes = []; TenancyCacheManager::$addTags = false; @@ -33,6 +34,7 @@ beforeEach(function () { afterEach(function () { PrefixCacheTenancyBootstrapper::$tenantCacheStores = []; PrefixCacheTenancyBootstrapper::$prefixGenerators = []; + PrefixCacheTenancyBootstrapper::$originalPrefixes = []; TenancyCacheManager::$addTags = true; }); @@ -339,3 +341,18 @@ test('stores get prefixed using the default way if the store does not have a cor expect(cache()->store('redis')->getPrefix())->toBe($expectedPrefix . ':'); tenancy()->end(); }); + +test('stores can have different original prefixes', function() { + config(['cache.default' => 'redis']); + config(['cache.stores.redis2' => config('cache.stores.redis')]); + config(['cache.prefix' => $defaultOriginalPrefix = 'default_prefix_']); + + // The prefix specified for a store in PrefixCacheTenancyBootstrapper::$originalPrefixes + // Will be used as the original prefix for that store instead of `config('cache.prefix')` + PrefixCacheTenancyBootstrapper::$originalPrefixes = ['redis2' => $customOriginalPrefix = 'redis2_prefix_']; + PrefixCacheTenancyBootstrapper::$tenantCacheStores = ['redis', 'redis2']; + + tenancy()->initialize(Tenant::create()); + expect(cache()->store('redis')->getPrefix())->toStartWith($defaultOriginalPrefix); + expect(cache()->store('redis2')->getPrefix())->toStartWith($customOriginalPrefix); +});