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

Delete CacheTenancyBootstrapper

This commit is contained in:
lukinovec 2023-03-31 14:10:56 +02:00
parent 7bbd5350c7
commit 840cd831d6
5 changed files with 9 additions and 63 deletions

View file

@ -98,7 +98,6 @@ return [
*/ */
'bootstrappers' => [ 'bootstrappers' => [
Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper::class, Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper::class,
Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper::class,
Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper::class, Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper::class,
Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper::class, Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper::class,
Stancl\Tenancy\Bootstrappers\BatchTenancyBootstrapper::class, Stancl\Tenancy\Bootstrappers\BatchTenancyBootstrapper::class,
@ -179,7 +178,7 @@ return [
], ],
/** /**
* Cache tenancy config. Used by CacheTenancyBootstrapper. * Cache tenancy config. Used by the custom CacheManager and the PrefixCacheTenancyBootstrapper.
* *
* This works for all Cache facade calls, cache() helper * This works for all Cache facade calls, cache() helper
* calls and direct calls to injected cache stores. * calls and direct calls to injected cache stores.

View file

@ -1,45 +0,0 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Bootstrappers;
use Illuminate\Cache\CacheManager;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\Facades\Cache;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant;
class CacheTenancyBootstrapper implements TenancyBootstrapper
{
protected ?CacheManager $originalCache = null;
public function __construct(
protected Application $app
) {
}
public function bootstrap(Tenant $tenant): void
{
$this->resetFacadeCache();
$this->originalCache = $this->originalCache ?? $this->app['cache'];
}
public function revert(): void
{
$this->resetFacadeCache();
$this->originalCache = null;
}
/**
* This wouldn't be necessary, but is needed when a call to the
* facade has been made prior to bootstrapping tenancy. The
* facade has its own cache, separate from the container.
*/
public function resetFacadeCache(): void
{
Cache::clearResolvedInstances();
}
}

View file

@ -31,7 +31,6 @@ use Stancl\Tenancy\Listeners\DeleteTenantStorage;
use Stancl\Tenancy\Listeners\RevertToCentralContext; use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Stancl\Tenancy\Bootstrappers\UrlTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\UrlTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\MailTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\MailTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper;
use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain; use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain;
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
@ -84,11 +83,13 @@ test('database data is separated', function () {
expect(DB::table('users')->first()->name)->toBe('Foo'); expect(DB::table('users')->first()->name)->toBe('Foo');
}); });
test('cache data is separated', function (string $bootstrapper) { test('cache data is separated', function () {
CacheManager::$addTags = true; CacheManager::$addTags = true;
config([ config([
'tenancy.bootstrappers' => [$bootstrapper], 'tenancy.bootstrappers' => [
PrefixCacheTenancyBootstrapper::class,
],
'cache.default' => 'redis', 'cache.default' => 'redis',
]); ]);
@ -123,10 +124,7 @@ test('cache data is separated', function (string $bootstrapper) {
// Asset central is still the same // Asset central is still the same
expect(Cache::get('foo'))->toBe('central'); expect(Cache::get('foo'))->toBe('central');
})->with([ });
'CacheTenancyBootstrapper' => CacheTenancyBootstrapper::class,
'PrefixCacheTenancyBootstrapper' => PrefixCacheTenancyBootstrapper::class,
])->group('bootstrapper');
test('redis data is separated', function () { test('redis data is separated', function () {
config(['tenancy.bootstrappers' => [ config(['tenancy.bootstrappers' => [

View file

@ -7,13 +7,11 @@ use Stancl\Tenancy\Tests\Etc\Tenant;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Events\TenancyInitialized; use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper;
beforeEach(function () { beforeEach(function () {
config(['tenancy.bootstrappers' => [ config(['tenancy.bootstrappers' => []]);
CacheTenancyBootstrapper::class,
]]);
CacheManager::$addTags = true;
Event::listen(TenancyInitialized::class, BootstrapTenancy::class); Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
}); });

View file

@ -3,8 +3,6 @@
declare(strict_types=1); declare(strict_types=1);
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper;
use Stancl\Tenancy\CacheManager;
use Stancl\Tenancy\Events\TenancyEnded; use Stancl\Tenancy\Events\TenancyEnded;
use Stancl\Tenancy\Events\TenancyInitialized; use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Facades\GlobalCache; use Stancl\Tenancy\Facades\GlobalCache;
@ -13,9 +11,7 @@ use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Stancl\Tenancy\Tests\Etc\Tenant; use Stancl\Tenancy\Tests\Etc\Tenant;
beforeEach(function () { beforeEach(function () {
config(['tenancy.bootstrappers' => [ config(['tenancy.bootstrappers' => []]);
CacheTenancyBootstrapper::class,
]]);
Event::listen(TenancyInitialized::class, BootstrapTenancy::class); Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
Event::listen(TenancyEnded::class, RevertToCentralContext::class); Event::listen(TenancyEnded::class, RevertToCentralContext::class);