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

Make Tenancy override CacheManager

This commit is contained in:
lukinovec 2022-12-12 17:05:53 +01:00
parent dc9c8fcf68
commit 9ec0b6dae3
3 changed files with 37 additions and 18 deletions

View file

@ -7,7 +7,6 @@ namespace Stancl\Tenancy\Bootstrappers;
use Illuminate\Cache\CacheManager; use Illuminate\Cache\CacheManager;
use Illuminate\Contracts\Foundation\Application; use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Stancl\Tenancy\CacheManager as TenantCacheManager;
use Stancl\Tenancy\Contracts\TenancyBootstrapper; use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant; use Stancl\Tenancy\Contracts\Tenant;
@ -25,19 +24,12 @@ class CacheTenancyBootstrapper implements TenancyBootstrapper
$this->resetFacadeCache(); $this->resetFacadeCache();
$this->originalCache = $this->originalCache ?? $this->app['cache']; $this->originalCache = $this->originalCache ?? $this->app['cache'];
$this->app->extend('cache', function () {
return new TenantCacheManager($this->app);
});
} }
public function revert(): void public function revert(): void
{ {
$this->resetFacadeCache(); $this->resetFacadeCache();
$this->app->extend('cache', function () {
return $this->originalCache;
});
$this->originalCache = null; $this->originalCache = null;
} }

View file

@ -4,16 +4,17 @@ declare(strict_types=1);
namespace Stancl\Tenancy; namespace Stancl\Tenancy;
use Stancl\Tenancy\Enums\LogMode;
use Illuminate\Cache\CacheManager; use Illuminate\Cache\CacheManager;
use Illuminate\Database\Console\Migrations\FreshCommand;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
use Stancl\Tenancy\Contracts\Domain; use Stancl\Tenancy\Contracts\Domain;
use Stancl\Tenancy\Contracts\Tenant; use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Enums\LogMode; use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;
use Stancl\Tenancy\Events\Contracts\TenancyEvent; use Stancl\Tenancy\Events\Contracts\TenancyEvent;
use Stancl\Tenancy\Resolvers\DomainTenantResolver; use Stancl\Tenancy\Resolvers\DomainTenantResolver;
use Stancl\Tenancy\CacheManager as TenantCacheManager;
use Illuminate\Database\Console\Migrations\FreshCommand;
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
class TenancyServiceProvider extends ServiceProvider class TenancyServiceProvider extends ServiceProvider
{ {
@ -97,6 +98,10 @@ class TenancyServiceProvider extends ServiceProvider
return new Commands\MigrateFreshOverride; return new Commands\MigrateFreshOverride;
}); });
$this->app->singleton('cache', function ($app) {
return new TenantCacheManager($app);
});
$this->publishes([ $this->publishes([
__DIR__ . '/../assets/config.php' => config_path('tenancy.php'), __DIR__ . '/../assets/config.php' => config_path('tenancy.php'),
], 'config'); ], 'config');

View file

@ -2,13 +2,15 @@
declare(strict_types=1); declare(strict_types=1);
use Illuminate\Cache\CacheManager;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper;
use Stancl\Tenancy\Events\TenancyEnded; use Stancl\Tenancy\Events\TenancyEnded;
use Stancl\Tenancy\Tests\Etc\CacheService;
use Stancl\Tenancy\Events\TenancyInitialized; use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Listeners\RevertToCentralContext; use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Stancl\Tenancy\Tests\Etc\CacheService; use Stancl\Tenancy\CacheManager as TenancyCacheManager;
use Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper;
beforeEach(function () { beforeEach(function () {
config([ config([
@ -22,6 +24,26 @@ beforeEach(function () {
Event::listen(TenancyEnded::class, RevertToCentralContext::class); Event::listen(TenancyEnded::class, RevertToCentralContext::class);
}); });
test('Tenancy overrides CacheManager', function() {
expect(app('cache')::class)->toBe(TenancyCacheManager::class);
expect(app(CacheManager::class)::class)->toBe(TenancyCacheManager::class);
tenancy()->initialize(Tenant::create(['id' => 'first']));
expect(app('cache')::class)->toBe(TenancyCacheManager::class);
expect(app(CacheManager::class)::class)->toBe(TenancyCacheManager::class);
tenancy()->initialize(Tenant::create(['id' => 'second']));
expect(app('cache')::class)->toBe(TenancyCacheManager::class);
expect(app(CacheManager::class)::class)->toBe(TenancyCacheManager::class);
tenancy()->end();
expect(app('cache')::class)->toBe(TenancyCacheManager::class);
expect(app(CacheManager::class)::class)->toBe(TenancyCacheManager::class);
});
test('cache prefix is separate for each tenant', function () { test('cache prefix is separate for each tenant', function () {
$originalPrefix = config('cache.prefix'); $originalPrefix = config('cache.prefix');
$prefixBase = config('tenancy.cache.prefix_base'); $prefixBase = config('tenancy.cache.prefix_base');
@ -45,21 +67,21 @@ test('cache prefix is separate for each tenant', function () {
$tenantTwoPrefix = $originalPrefix . $prefixBase . $tenant2->getTenantKey(); $tenantTwoPrefix = $originalPrefix . $prefixBase . $tenant2->getTenantKey();
tenancy()->initialize($tenant2); tenancy()->initialize($tenant2);
cache()->set('key', 'tenanttwo-value'); cache()->set('key', 'tenanttwo-value');
expect($tenantTwoPrefix . ':') expect($tenantTwoPrefix . ':')
->toBe(app('cache')->getPrefix()) ->toBe(app('cache')->getPrefix())
->toBe(app('cache.store')->getPrefix()); ->toBe(app('cache.store')->getPrefix());
// Assert tenants' data is accessible using the prefix from the central context // Assert tenants' data is accessible using the prefix from the central context tenancy()->end();
tenancy()->end();
config(['cache.prefix' => null]); // stop prefixing cache keys in central so we can provide prefix manually config(['cache.prefix' => null]); // stop prefixing cache keys in central so we can provide prefix manually
app('cache')->forgetDriver(config('cache.default')); app('cache')->forgetDriver(config('cache.default'));
expect(cache($tenantOnePrefix . ':key'))->toBe('tenantone-value'); expect(cache($tenantOnePrefix . ':key'))->toBe('tenantone-value');
expect(cache($tenantTwoPrefix . ':key'))->toBe('tenanttwo-value'); expect(cache($tenantTwoPrefix . ':key'))->toBe('tenanttwo-value');
}); })->group('prefix');
test('cache is persisted when reidentification is used', function () { test('cache is persisted when reidentification is used', function () {
$tenant1 = Tenant::create(); $tenant1 = Tenant::create();