From daaaad34f45f2068a29afdae1a40769ae083c89f Mon Sep 17 00:00:00 2001 From: Li Zhineng Date: Fri, 8 Dec 2023 21:45:40 +0800 Subject: [PATCH 1/2] extract method --- phpstan.neon | 2 +- src/TablestoreServiceProvider.php | 40 +++++++++++++++++++------------ 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 26c658d..96f0973 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -7,7 +7,7 @@ parameters: - src ignoreErrors: - - message: '#^Cannot access offset ''config'' on Illuminate\\Contracts\\Foundation\\Application\.$#' + message: '#^Call to an undefined method Dew\\TablestoreDriver\\TablestoreServiceProvider::getPrefix\(\)\.$#' path: src/TablestoreServiceProvider.php - message: '#should be contravariant with .* Illuminate\\Contracts\\Cache\\Store::get\(\)$#' diff --git a/src/TablestoreServiceProvider.php b/src/TablestoreServiceProvider.php index d68ff36..4aa982c 100644 --- a/src/TablestoreServiceProvider.php +++ b/src/TablestoreServiceProvider.php @@ -18,29 +18,39 @@ final class TablestoreServiceProvider extends ServiceProvider public function register() { $this->app->booting(function () { - Cache::extend('tablestore', function ($app, $config) { - $client = new Tablestore( - $config['key'], $config['secret'], - $config['endpoint'], $config['instance'] ?? null - ); + $this->registerCacheDriver(); + }); + } + + /** + * Register the Tablestore cache driver. + */ + private function registerCacheDriver(): void + { + Cache::extend('tablestore', function ($app, $config) { + $client = new Tablestore( + $config['key'], $config['secret'], + $config['endpoint'], $config['instance'] ?? null + ); - if (isset($config['token'])) { - $client->tokenUsing($config['token']); - } + if (isset($config['token'])) { + $client->tokenUsing($config['token']); + } - if (isset($config['http'])) { - $client->optionsUsing($config['http']); - } + if (isset($config['http'])) { + $client->optionsUsing($config['http']); + } - return Cache::repository(new TablestoreStore( + return Cache::repository( + new TablestoreStore( $client, $config['table'], $config['attributes']['key'] ?? 'key', $config['attributes']['value'] ?? 'value', $config['attributes']['expiration'] ?? 'expires_at', - $config['prefix'] ?? $this->app['config']['cache.prefix'] - )); - }); + $this->getPrefix($config) + ) + ); }); } } From e14efc371af0fcf865af8160ad882ed6e7d1034e Mon Sep 17 00:00:00 2001 From: Li Zhineng Date: Fri, 8 Dec 2023 22:10:56 +0800 Subject: [PATCH 2/2] register session driver --- phpstan.neon | 3 +++ src/TablestoreServiceProvider.php | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/phpstan.neon b/phpstan.neon index 96f0973..85228af 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -9,6 +9,9 @@ parameters: - message: '#^Call to an undefined method Dew\\TablestoreDriver\\TablestoreServiceProvider::getPrefix\(\)\.$#' path: src/TablestoreServiceProvider.php + - + message: '#^Call to an undefined method Dew\\TablestoreDriver\\TablestoreServiceProvider::createCacheHandler\(\)\.$#' + path: src/TablestoreServiceProvider.php - message: '#should be contravariant with .* Illuminate\\Contracts\\Cache\\Store::get\(\)$#' path: src/TablestoreStore.php diff --git a/src/TablestoreServiceProvider.php b/src/TablestoreServiceProvider.php index 4aa982c..400f1f9 100644 --- a/src/TablestoreServiceProvider.php +++ b/src/TablestoreServiceProvider.php @@ -19,6 +19,7 @@ public function register() { $this->app->booting(function () { $this->registerCacheDriver(); + $this->registerSessionDriver(); }); } @@ -53,4 +54,17 @@ private function registerCacheDriver(): void ); }); } + + /** + * Register the Tablestore session driver. + */ + private function registerSessionDriver(): void + { + /** @var \Illuminate\Session\SessionManager */ + $manager = $this->app->make('session'); + + $handler = fn ($app) => $this->createCacheHandler('tablestore'); + + $manager->extend('tablestore', $handler->bindTo($manager, $manager)); + } }