From 4129b2d95613762d8ac67e3dff20ab820f3fbe61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Fri, 8 Aug 2025 00:00:41 +0200 Subject: [PATCH] Finalize GlobalCache-related changes --- .github/workflows/ci.yml | 2 +- src/Bootstrappers/DatabaseCacheBootstrapper.php | 15 ++++++++------- src/TenancyServiceProvider.php | 3 +++ 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7cdf2ae6..91699f08 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,7 +16,7 @@ jobs: strategy: matrix: include: - - laravel: "12.x-dev" # todo0 revert + - laravel: "^12.0" php: "8.4" steps: diff --git a/src/Bootstrappers/DatabaseCacheBootstrapper.php b/src/Bootstrappers/DatabaseCacheBootstrapper.php index 6e331e4b..7a45b199 100644 --- a/src/Bootstrappers/DatabaseCacheBootstrapper.php +++ b/src/Bootstrappers/DatabaseCacheBootstrapper.php @@ -6,6 +6,7 @@ namespace Stancl\Tenancy\Bootstrappers; use Exception; use Illuminate\Cache\CacheManager; +use Illuminate\Cache\DatabaseStore; use Illuminate\Config\Repository; use Illuminate\Support\Facades\DB; use Stancl\Tenancy\Contracts\TenancyBootstrapper; @@ -13,10 +14,10 @@ use Stancl\Tenancy\Contracts\Tenant; use Stancl\Tenancy\TenancyServiceProvider; /** - * This bootstrapper allows cache to be stored in the tenant databases by switching - * the database cache store's (and cache locks) connection. + * This bootstrapper allows cache to be stored in tenant databases by switching the database + * connection used by cache stores that use the database driver. * - * Intended to be used with database driver-based cache stores, instead of CacheTenancyBootstrapper. + * Can be used instead of CacheTenancyBootstrapper. * * On bootstrap(), all database cache stores' connections are set to 'tenant' * and the database cache stores are purged from the CacheManager's resolved stores. @@ -69,11 +70,11 @@ class DatabaseCacheBootstrapper implements TenancyBootstrapper TenancyServiceProvider::$adjustCacheManagerUsing = static function (CacheManager $manager) use ($originalConnections) { foreach ($originalConnections as $storeName => $connections) { - // @phpstan-ignore-next-line method.notFound - $manager->store($storeName)->getStore()->setConnection(DB::connection($connections['connection'])); + /** @var DatabaseStore $store */ + $store = $manager->store($storeName)->getStore(); - // @phpstan-ignore-next-line method.notFound - $manager->store($storeName)->getStore()->setLockConnection(DB::connection($connections['lockConnection'])); + $store->setConnection(DB::connection($connections['connection'])); + $store->setLockConnection(DB::connection($connections['lockConnection'])); } }; } diff --git a/src/TenancyServiceProvider.php b/src/TenancyServiceProvider.php index 888fbb1b..557306b2 100644 --- a/src/TenancyServiceProvider.php +++ b/src/TenancyServiceProvider.php @@ -97,6 +97,9 @@ class TenancyServiceProvider extends ServiceProvider // When we use the DatabaseTenancyBootstrapper, it changes the default connection, // and therefore the connection of the database store that will be created when // this new CacheManager is instantiated again. + // + // For that reason, we also adjust the relevant stores on this new CacheManager + // using the callback below. It is set by DatabaseCacheBootstrapper. $manager = new CacheManager($app); if (static::$adjustCacheManagerUsing !== null) {