Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ 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: '#^Call to an undefined method Dew\\TablestoreDriver\\TablestoreServiceProvider::createCacheHandler\(\)\.$#'
path: src/TablestoreServiceProvider.php
-
message: '#should be contravariant with .* Illuminate\\Contracts\\Cache\\Store::get\(\)$#'
Expand Down
54 changes: 39 additions & 15 deletions src/TablestoreServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,53 @@ 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();
$this->registerSessionDriver();
});
}

if (isset($config['token'])) {
$client->tokenUsing($config['token']);
}
/**
* 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['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)
)
);
});
}

/**
* 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));
}
}