mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 17:44:04 +00:00
Use a single original prefix
This commit is contained in:
parent
c9ba00c1fc
commit
8ac4d87e94
2 changed files with 6 additions and 30 deletions
|
|
@ -14,8 +14,7 @@ use Stancl\Tenancy\Contracts\Tenant;
|
||||||
|
|
||||||
class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper
|
class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper
|
||||||
{
|
{
|
||||||
protected string|null $defaultPrefix = null;
|
protected string|null $originalPrefix = null;
|
||||||
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 = [
|
||||||
// driverName => Closure(Tenant $tenant)
|
// driverName => Closure(Tenant $tenant)
|
||||||
|
|
@ -29,11 +28,7 @@ class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper
|
||||||
|
|
||||||
public function bootstrap(Tenant $tenant): void
|
public function bootstrap(Tenant $tenant): void
|
||||||
{
|
{
|
||||||
$this->defaultPrefix = $this->config->get('cache.prefix');
|
$this->originalPrefix = $this->config->get('cache.prefix');
|
||||||
|
|
||||||
foreach (static::$tenantCacheStores as $store) {
|
|
||||||
static::$originalPrefixes[$store] ??= $this->defaultPrefix;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (static::$tenantCacheStores as $store) {
|
foreach (static::$tenantCacheStores as $store) {
|
||||||
$this->setCachePrefix($store, $this->getStorePrefix($store, $tenant));
|
$this->setCachePrefix($store, $this->getStorePrefix($store, $tenant));
|
||||||
|
|
@ -42,11 +37,9 @@ class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper
|
||||||
|
|
||||||
public function revert(): void
|
public function revert(): void
|
||||||
{
|
{
|
||||||
foreach (static::$originalPrefixes as $driver => $prefix) {
|
foreach (static::$tenantCacheStores as $store) {
|
||||||
$this->setCachePrefix($driver, $prefix);
|
$this->setCachePrefix($store, $this->originalPrefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
static::$originalPrefixes = [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function setCachePrefix(string $driver, string|null $prefix): void
|
protected function setCachePrefix(string $driver, string|null $prefix): void
|
||||||
|
|
@ -62,7 +55,7 @@ class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper
|
||||||
|
|
||||||
// Now that the store uses the passed prefix
|
// Now that the store uses the passed prefix
|
||||||
// Set the configured prefix back to the default one
|
// Set the configured prefix back to the default one
|
||||||
$this->config->set('cache.prefix', $this->defaultPrefix);
|
$this->config->set('cache.prefix', $this->originalPrefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getStorePrefix(string $store, Tenant $tenant): string
|
public function getStorePrefix(string $store, Tenant $tenant): string
|
||||||
|
|
@ -71,7 +64,7 @@ class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper
|
||||||
return static::$prefixGenerators[$store]($tenant);
|
return static::$prefixGenerators[$store]($tenant);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (static::$originalPrefixes[$store] ?? $this->defaultPrefix) . $this->config->get('tenancy.cache.prefix_base') . $tenant->getTenantKey();
|
return $this->originalPrefix . $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
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,6 @@ beforeEach(function () {
|
||||||
|
|
||||||
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver];
|
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver];
|
||||||
PrefixCacheTenancyBootstrapper::$prefixGenerators = [];
|
PrefixCacheTenancyBootstrapper::$prefixGenerators = [];
|
||||||
PrefixCacheTenancyBootstrapper::$originalPrefixes = [];
|
|
||||||
|
|
||||||
TenancyCacheManager::$addTags = false;
|
TenancyCacheManager::$addTags = false;
|
||||||
|
|
||||||
|
|
@ -34,7 +33,6 @@ beforeEach(function () {
|
||||||
afterEach(function () {
|
afterEach(function () {
|
||||||
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [];
|
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [];
|
||||||
PrefixCacheTenancyBootstrapper::$prefixGenerators = [];
|
PrefixCacheTenancyBootstrapper::$prefixGenerators = [];
|
||||||
PrefixCacheTenancyBootstrapper::$originalPrefixes = [];
|
|
||||||
|
|
||||||
TenancyCacheManager::$addTags = true;
|
TenancyCacheManager::$addTags = true;
|
||||||
});
|
});
|
||||||
|
|
@ -341,18 +339,3 @@ test('stores get prefixed using the default way if the store does not have a cor
|
||||||
expect(cache()->store('redis')->getPrefix())->toBe($expectedPrefix . ':');
|
expect(cache()->store('redis')->getPrefix())->toBe($expectedPrefix . ':');
|
||||||
tenancy()->end();
|
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);
|
|
||||||
});
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue