1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-14 16:44:03 +00:00
This commit is contained in:
Abrar Ahmad 2022-11-22 11:28:27 +05:00
parent c59a514490
commit 90c54ac733

View file

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Bootstrappers; namespace Stancl\Tenancy\Bootstrappers;
use Illuminate\Cache\Repository; use Illuminate\Cache\Repository;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Stancl\Tenancy\Contracts\TenancyBootstrapper; use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant; use Stancl\Tenancy\Contracts\Tenant;
@ -14,10 +15,15 @@ class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper
protected null|string $originalPrefix = null; protected null|string $originalPrefix = null;
protected string $storeName; protected string $storeName;
public function __construct(
protected Application $app
) {
}
public function bootstrap(Tenant $tenant): void public function bootstrap(Tenant $tenant): void
{ {
$this->originalPrefix = config('cache.prefix'); $this->originalPrefix = $this->app['config']['cache.prefix'];
$this->storeName = config('cache.default'); $this->storeName = $this->app['config']['cache.default'];
$this->setCachePrefix('tenant_id_' . $tenant->id); $this->setCachePrefix('tenant_id_' . $tenant->id);
} }
@ -32,20 +38,20 @@ class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper
protected function setCachePrefix(string $prefix): void protected function setCachePrefix(string $prefix): void
{ {
config()->set('cache.prefix', $prefix); $this->app['config']['cache.prefix'] = $prefix;
app('cache')->forgetDriver($this->storeName); $this->app['cache']->forgetDriver($this->storeName);
// This is important because the `CacheManager` will have the `$app['config']` array cached // This is important because the `CacheManager` will have the `$app['config']` array cached
// with old prefixes on the `cache` instance. Simply calling `forgetDriver` only removes // with old prefixes on the `cache` instance. Simply calling `forgetDriver` only removes
// the `$store` but doesn't update the `$app['config']`. // the `$store` but doesn't update the `$app['config']`.
app()->forgetInstance('cache'); $this->app->forgetInstance('cache');
//This is important because the Cache Repository is using an old version of the CacheManager //This is important because the Cache Repository is using an old version of the CacheManager
app()->forgetInstance('cache.store'); $this->app->forgetInstance('cache.store');
// Forget the cache repository in the container // Forget the cache repository in the container
app()->forgetInstance(Repository::class); $this->app->forgetInstance(Repository::class);
Cache::clearResolvedInstances(); Cache::clearResolvedInstances();
} }