From c7416ad63de0d835c2fa7a1a45ea6ed2ed011e94 Mon Sep 17 00:00:00 2001 From: Christopher Castaneira Date: Tue, 30 Jul 2024 14:55:16 -0600 Subject: [PATCH 1/3] Add `dynamodb_client` as new option --- .../Integrations/DynamoDbFeatureRequester.php | 2 +- src/LaunchDarkly/Integrations/DynamoDb.php | 23 +++++++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/LaunchDarkly/Impl/Integrations/DynamoDbFeatureRequester.php b/src/LaunchDarkly/Impl/Integrations/DynamoDbFeatureRequester.php index baefc23..7b9536c 100644 --- a/src/LaunchDarkly/Impl/Integrations/DynamoDbFeatureRequester.php +++ b/src/LaunchDarkly/Impl/Integrations/DynamoDbFeatureRequester.php @@ -26,7 +26,7 @@ public function __construct(string $baseUri, string $sdkKey, array $options) $dynamoDbOptions = $options['dynamodb_options'] ?? []; $dynamoDbOptions['version'] = '2012-08-10'; // in the AWS SDK for PHP, this is how you specify the API version - $this->_client = new DynamoDbClient($dynamoDbOptions); + $this->_client = $options['dynamodb_client'] ?? new DynamoDbClient($dynamoDbOptions); $prefix = $options['dynamodb_prefix'] ?? ''; $this->_prefix = ($prefix != null && $prefix != '') ? ($prefix . ':') : ''; diff --git a/src/LaunchDarkly/Integrations/DynamoDb.php b/src/LaunchDarkly/Integrations/DynamoDb.php index 8a436a0..18eed98 100644 --- a/src/LaunchDarkly/Integrations/DynamoDb.php +++ b/src/LaunchDarkly/Integrations/DynamoDb.php @@ -1,7 +1,7 @@ "my-table" ]); - * $config = [ "feature_requester" => $fr ]; - * $client = new LDClient("sdk_key", $config); + * $fr = LaunchDarkly\Integrations\DynamoDb::featureRequester([ 'dynamodb_table' => 'my-table' ]); + * $config = [ 'feature_requester' => $fr ]; + * $client = new LDClient('sdk_key', $config); + * + * Or if you already have a client instance: + * + * $dynamoClient = new Aws\DynamoDb\DynamoDbClient($settings); + * $fr = LaunchDarkly\Integrations\DynamoDb::featureRequester([ 'dynamo_client' => $dynamoClient ]); + * $config = [ 'feature_requester' => $fr ]; + * $client = new LDClient('sdk_key', $config); * * For more about using LaunchDarkly with databases, see the * [SDK reference guide](https://docs.launchdarkly.com/v2.0/docs/using-a-persistent-feature-store). @@ -22,12 +29,14 @@ class DynamoDb * - `dynamodb_options`: can include any settings supported by the AWS SDK client * - `dynamodb_prefix`: a string to be prepended to all database keys; corresponds to the prefix * setting in ld-relay + * - `dynamodb_client`: an already-configured DynamoDb client instance if you wish to reuse one; if + * specified, this will cause all other options except `dynamodb_prefix` and `dynamodb_table` to be ignored * - `apc_expiration`: expiration time in seconds for local caching, if `APCu` is installed - * @return mixed an object to be stored in the `feature_requester` configuration property + * @return \Closure an object to be stored in the `feature_requester` configuration property */ - public static function featureRequester(array $options = array()) + public static function featureRequester(array $options = []) { - return function ($baseUri, $sdkKey, $baseOptions) use ($options) { + return static function ($baseUri, $sdkKey, $baseOptions) use ($options) { return new DynamoDbFeatureRequester($baseUri, $sdkKey, array_merge($baseOptions, $options)); }; } From 79b663d4e744bf6115a8a813f159085dec995841 Mon Sep 17 00:00:00 2001 From: Christopher Castaneira Date: Wed, 31 Jul 2024 09:12:27 -0600 Subject: [PATCH 2/3] Remove `static` and add client instance validation --- .../Impl/Integrations/DynamoDbFeatureRequester.php | 4 ++++ src/LaunchDarkly/Integrations/DynamoDb.php | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/LaunchDarkly/Impl/Integrations/DynamoDbFeatureRequester.php b/src/LaunchDarkly/Impl/Integrations/DynamoDbFeatureRequester.php index 7b9536c..583310c 100644 --- a/src/LaunchDarkly/Impl/Integrations/DynamoDbFeatureRequester.php +++ b/src/LaunchDarkly/Impl/Integrations/DynamoDbFeatureRequester.php @@ -28,6 +28,10 @@ public function __construct(string $baseUri, string $sdkKey, array $options) $dynamoDbOptions['version'] = '2012-08-10'; // in the AWS SDK for PHP, this is how you specify the API version $this->_client = $options['dynamodb_client'] ?? new DynamoDbClient($dynamoDbOptions); + if (!($this->_client instanceof DynamoDbClient)) { + throw new \InvalidArgumentException('dynamodb_client must be an instance of Aws\DynamoDb\DynamoDbClient'); + } + $prefix = $options['dynamodb_prefix'] ?? ''; $this->_prefix = ($prefix != null && $prefix != '') ? ($prefix . ':') : ''; } diff --git a/src/LaunchDarkly/Integrations/DynamoDb.php b/src/LaunchDarkly/Integrations/DynamoDb.php index 18eed98..4f4ddb1 100644 --- a/src/LaunchDarkly/Integrations/DynamoDb.php +++ b/src/LaunchDarkly/Integrations/DynamoDb.php @@ -36,7 +36,7 @@ class DynamoDb */ public static function featureRequester(array $options = []) { - return static function ($baseUri, $sdkKey, $baseOptions) use ($options) { + return function ($baseUri, $sdkKey, $baseOptions) use ($options) { return new DynamoDbFeatureRequester($baseUri, $sdkKey, array_merge($baseOptions, $options)); }; } From 2f8e71dd4e550310abd3f36d2df41b01b36c0e9c Mon Sep 17 00:00:00 2001 From: Christopher Castaneira Date: Wed, 31 Jul 2024 09:28:00 -0600 Subject: [PATCH 3/3] Ternary operator for `dynamodb_client` Co-authored-by: Matthew M. Keeler --- .../Impl/Integrations/DynamoDbFeatureRequester.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/LaunchDarkly/Impl/Integrations/DynamoDbFeatureRequester.php b/src/LaunchDarkly/Impl/Integrations/DynamoDbFeatureRequester.php index 583310c..9dc077a 100644 --- a/src/LaunchDarkly/Impl/Integrations/DynamoDbFeatureRequester.php +++ b/src/LaunchDarkly/Impl/Integrations/DynamoDbFeatureRequester.php @@ -26,11 +26,8 @@ public function __construct(string $baseUri, string $sdkKey, array $options) $dynamoDbOptions = $options['dynamodb_options'] ?? []; $dynamoDbOptions['version'] = '2012-08-10'; // in the AWS SDK for PHP, this is how you specify the API version - $this->_client = $options['dynamodb_client'] ?? new DynamoDbClient($dynamoDbOptions); - - if (!($this->_client instanceof DynamoDbClient)) { - throw new \InvalidArgumentException('dynamodb_client must be an instance of Aws\DynamoDb\DynamoDbClient'); - } + $client = $options['dynamodb_client'] ?? null; + $this->_client = $client instanceof DynamoDbClient ? $client : new DynamoDbClient($dynamoDbOptions); $prefix = $options['dynamodb_prefix'] ?? ''; $this->_prefix = ($prefix != null && $prefix != '') ? ($prefix . ':') : '';