From b7155719278edca13b912edf22483a6c35cf4d1d Mon Sep 17 00:00:00 2001 From: lukinovec Date: Fri, 24 Feb 2023 09:17:24 +0100 Subject: [PATCH] Prefix all cache stores specified in `$tenantCacheStores` --- .../PrefixCacheTenancyBootstrapper.php | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/Bootstrappers/PrefixCacheTenancyBootstrapper.php b/src/Bootstrappers/PrefixCacheTenancyBootstrapper.php index d6ab298e..fea1a6a7 100644 --- a/src/Bootstrappers/PrefixCacheTenancyBootstrapper.php +++ b/src/Bootstrappers/PrefixCacheTenancyBootstrapper.php @@ -14,7 +14,6 @@ use Stancl\Tenancy\Contracts\Tenant; class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper { protected string|null $originalPrefix = null; - protected string $storeName; public static array $tenantCacheStores = []; public function __construct( @@ -26,16 +25,14 @@ class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper public function bootstrap(Tenant $tenant): void { $this->originalPrefix = $this->config->get('cache.prefix'); - $this->storeName = $this->config->get('cache.default'); - if (in_array($this->storeName, static::$tenantCacheStores)) { - $this->setCachePrefix($this->originalPrefix . $this->config->get('tenancy.cache.prefix_base') . $tenant->getTenantKey()); - } + $this->setCachePrefix($this->originalPrefix . $this->config->get('tenancy.cache.prefix_base') . $tenant->getTenantKey()); } public function revert(): void { $this->setCachePrefix($this->originalPrefix); + $this->originalPrefix = null; } @@ -43,15 +40,25 @@ class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper { $this->config->set('cache.prefix', $prefix); - $newStore = $this->cacheManager->resolve($this->storeName ?? $this->cacheManager->getDefaultDriver())->getStore(); - - /** @var Repository $repository */ - $repository = $this->cacheManager->driver($this->storeName); - - $repository->setStore($newStore); + foreach (static::$tenantCacheStores as $driver) { + // Refresh driver's store to make the driver use the current prefix + $this->refreshStore($driver); + } // 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 Cache::clearResolvedInstances(); } + + /** + * Refresh cache driver's store. + */ + protected function refreshStore(string $driver): void + { + $newStore = $this->cacheManager->resolve($driver)->getStore(); + /** @var Repository $repository */ + $repository = $this->cacheManager->driver($driver); + + $repository->setStore($newStore); + } }