From 90c54ac7331703a54e17942664f15bfefe9e9a3b Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Tue, 22 Nov 2022 11:28:27 +0500 Subject: [PATCH] DI app --- .../PrefixCacheTenancyBootstrapper.php | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Bootstrappers/PrefixCacheTenancyBootstrapper.php b/src/Bootstrappers/PrefixCacheTenancyBootstrapper.php index 88feffff..d409cce8 100644 --- a/src/Bootstrappers/PrefixCacheTenancyBootstrapper.php +++ b/src/Bootstrappers/PrefixCacheTenancyBootstrapper.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Stancl\Tenancy\Bootstrappers; use Illuminate\Cache\Repository; +use Illuminate\Contracts\Foundation\Application; use Illuminate\Support\Facades\Cache; use Stancl\Tenancy\Contracts\TenancyBootstrapper; use Stancl\Tenancy\Contracts\Tenant; @@ -14,10 +15,15 @@ class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper protected null|string $originalPrefix = null; protected string $storeName; + public function __construct( + protected Application $app + ) { + } + public function bootstrap(Tenant $tenant): void { - $this->originalPrefix = config('cache.prefix'); - $this->storeName = config('cache.default'); + $this->originalPrefix = $this->app['config']['cache.prefix']; + $this->storeName = $this->app['config']['cache.default']; $this->setCachePrefix('tenant_id_' . $tenant->id); } @@ -32,20 +38,20 @@ class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper 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 // with old prefixes on the `cache` instance. Simply calling `forgetDriver` only removes // 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 - app()->forgetInstance('cache.store'); + $this->app->forgetInstance('cache.store'); // Forget the cache repository in the container - app()->forgetInstance(Repository::class); + $this->app->forgetInstance(Repository::class); Cache::clearResolvedInstances(); }