From 74a79b211712b8428dc0d3453da545867402a37f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Aur=C3=A9lio=20Deleu?= Date: Tue, 27 Apr 2021 22:16:03 +0200 Subject: [PATCH] [6.x] Fix Cache store with a name other than 'dynamodb' (#37145) * Fix Cache store with a name other than 'dynamodb' * StyleCI --- src/Illuminate/Cache/CacheManager.php | 28 ++++++++++++++++++- src/Illuminate/Cache/CacheServiceProvider.php | 22 +-------------- src/Illuminate/Cache/DynamoDbStore.php | 10 +++++++ tests/Integration/Cache/DynamoDbStoreTest.php | 3 +- 4 files changed, 40 insertions(+), 23 deletions(-) diff --git a/src/Illuminate/Cache/CacheManager.php b/src/Illuminate/Cache/CacheManager.php index 42bfc7271d59..1f514369d34e 100755 --- a/src/Illuminate/Cache/CacheManager.php +++ b/src/Illuminate/Cache/CacheManager.php @@ -2,10 +2,12 @@ namespace Illuminate\Cache; +use Aws\DynamoDb\DynamoDbClient; use Closure; use Illuminate\Contracts\Cache\Factory as FactoryContract; use Illuminate\Contracts\Cache\Store; use Illuminate\Contracts\Events\Dispatcher as DispatcherContract; +use Illuminate\Support\Arr; use InvalidArgumentException; /** @@ -224,9 +226,11 @@ protected function createDatabaseDriver(array $config) */ protected function createDynamodbDriver(array $config) { + $client = $this->newDynamodbClient($config); + return $this->repository( new DynamoDbStore( - $this->app['cache.dynamodb.client'], + $client, $config['table'], $config['attributes']['key'] ?? 'key', $config['attributes']['value'] ?? 'value', @@ -236,6 +240,28 @@ protected function createDynamodbDriver(array $config) ); } + /** + * Create new DynamoDb Client instance. + * + * @return DynamoDbClient + */ + protected function newDynamodbClient(array $config) + { + $dynamoConfig = [ + 'region' => $config['region'], + 'version' => 'latest', + 'endpoint' => $config['endpoint'] ?? null, + ]; + + if (isset($config['key']) && isset($config['secret'])) { + $dynamoConfig['credentials'] = Arr::only( + $config, ['key', 'secret', 'token'] + ); + } + + return new DynamoDbClient($dynamoConfig); + } + /** * Create a new cache repository with the given implementation. * diff --git a/src/Illuminate/Cache/CacheServiceProvider.php b/src/Illuminate/Cache/CacheServiceProvider.php index b8208eb4f549..46fa0ae2615c 100755 --- a/src/Illuminate/Cache/CacheServiceProvider.php +++ b/src/Illuminate/Cache/CacheServiceProvider.php @@ -2,9 +2,7 @@ namespace Illuminate\Cache; -use Aws\DynamoDb\DynamoDbClient; use Illuminate\Contracts\Support\DeferrableProvider; -use Illuminate\Support\Arr; use Illuminate\Support\ServiceProvider; use Symfony\Component\Cache\Adapter\Psr16Adapter; @@ -32,24 +30,6 @@ public function register() $this->app->singleton('memcached.connector', function () { return new MemcachedConnector; }); - - $this->app->singleton('cache.dynamodb.client', function ($app) { - $config = $app['config']->get('cache.stores.dynamodb'); - - $dynamoConfig = [ - 'region' => $config['region'], - 'version' => 'latest', - 'endpoint' => $config['endpoint'] ?? null, - ]; - - if ($config['key'] && $config['secret']) { - $dynamoConfig['credentials'] = Arr::only( - $config, ['key', 'secret', 'token'] - ); - } - - return new DynamoDbClient($dynamoConfig); - }); } /** @@ -60,7 +40,7 @@ public function register() public function provides() { return [ - 'cache', 'cache.store', 'cache.psr6', 'memcached.connector', 'cache.dynamodb.client', + 'cache', 'cache.store', 'cache.psr6', 'memcached.connector', ]; } } diff --git a/src/Illuminate/Cache/DynamoDbStore.php b/src/Illuminate/Cache/DynamoDbStore.php index 4e663db4108a..aa28a789fa36 100644 --- a/src/Illuminate/Cache/DynamoDbStore.php +++ b/src/Illuminate/Cache/DynamoDbStore.php @@ -525,4 +525,14 @@ public function setPrefix($prefix) { $this->prefix = ! empty($prefix) ? $prefix.':' : ''; } + + /** + * Get the DynamoDb Client instance. + * + * @return DynamoDbClient + */ + public function getClient() + { + return $this->dynamo; + } } diff --git a/tests/Integration/Cache/DynamoDbStoreTest.php b/tests/Integration/Cache/DynamoDbStoreTest.php index f7aeae6a3deb..318db8a1ce51 100644 --- a/tests/Integration/Cache/DynamoDbStoreTest.php +++ b/tests/Integration/Cache/DynamoDbStoreTest.php @@ -4,6 +4,7 @@ use Aws\DynamoDb\DynamoDbClient; use Aws\Exception\AwsException; +use Illuminate\Contracts\Cache\Repository; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Str; use Orchestra\Testbench\TestCase; @@ -85,7 +86,7 @@ protected function getEnvironmentSetUp($app) $config = $app['config']->get('cache.stores.dynamodb'); /** @var \Aws\DynamoDb\DynamoDbClient $client */ - $client = $app['cache.dynamodb.client']; + $client = $app->make(Repository::class)->getStore()->getClient(); if ($this->dynamoTableExists($client, $config['table'])) { return;