diff --git a/.changes/nextrelease/standard_connection_session_data_type.json b/.changes/nextrelease/standard_connection_session_data_type.json new file mode 100644 index 0000000000..3df33472e8 --- /dev/null +++ b/.changes/nextrelease/standard_connection_session_data_type.json @@ -0,0 +1,7 @@ +[ + { + "type": "enhancement", + "category": "DynamoDb", + "description": "Added support configuring data attribute type as 'binary', defaults to 'string'." + } + ] \ No newline at end of file diff --git a/src/DynamoDb/SessionConnectionConfigTrait.php b/src/DynamoDb/SessionConnectionConfigTrait.php index f0dbedaedb..aee8ef28dd 100644 --- a/src/DynamoDb/SessionConnectionConfigTrait.php +++ b/src/DynamoDb/SessionConnectionConfigTrait.php @@ -12,6 +12,9 @@ trait SessionConnectionConfigTrait /** @var string Name of the data attribute in table. Default: "data" */ protected $dataAttribute = 'data'; + /** @var string Type of the data attribute in table. Default: "string" */ + protected $dataAttributeType = 'string'; + /** @var integer Lifetime of inactive sessions expiration */ protected $sessionLifetime; @@ -114,6 +117,22 @@ public function setDataAttribute($dataAttribute) $this->dataAttribute = $dataAttribute; } + /** + * @return string + */ + public function getDataAttributeType() + { + return $this->dataAttributeType; + } + + /** + * @param string $dataAttributeType + */ + public function setDataAttributeType($dataAttributeType) + { + $this->dataAttributeType = $dataAttributeType; + } + /** * @return number */ diff --git a/src/DynamoDb/StandardSessionConnection.php b/src/DynamoDb/StandardSessionConnection.php index a9883727a6..cf64f18abb 100644 --- a/src/DynamoDb/StandardSessionConnection.php +++ b/src/DynamoDb/StandardSessionConnection.php @@ -56,7 +56,13 @@ public function write($id, $data, $isChanged) ]; if ($isChanged) { if ($data != '') { - $attributes[$this->getDataAttribute()] = ['Value' => ['S' => $data]]; + $type = $this->getDataAttributeType(); + if ($type == 'binary') { + $attributes[$this->getDataAttribute()] = ['Value' => ['B' => $data]]; + } else { + $attributes[$this->getDataAttribute()] = ['Value' => ['S' => $data]]; + } + } else { $attributes[$this->getDataAttribute()] = ['Action' => 'DELETE']; } diff --git a/tests/DynamoDb/SessionConnectionConfigTraitTest.php b/tests/DynamoDb/SessionConnectionConfigTraitTest.php index c1475bf167..d1ae0b8d63 100644 --- a/tests/DynamoDb/SessionConnectionConfigTraitTest.php +++ b/tests/DynamoDb/SessionConnectionConfigTraitTest.php @@ -18,6 +18,7 @@ public function testStandardConfig() $this->assertEquals('sessions', $scc->getTableName()); $this->assertEquals('id', $scc->getHashKey()); $this->assertEquals('data', $scc->getDataAttribute()); + $this->assertEquals('string', $scc->getDataAttributeType()); $this->assertEquals((int) ini_get('session.gc_maxlifetime'), $scc->getSessionLifetime()); $this->assertEquals('expires', $scc->getSessionLifetimeAttribute()); $this->assertTrue($scc->isConsistentRead()); @@ -34,6 +35,7 @@ public function testCustomConfig() 'table_name' => 'sessions_custom', 'hash_key' => 'id_custom', 'data_attribute' => 'data_custom', + 'data_attribute_type' => 'binary', 'session_lifetime' => 2019, 'session_lifetime_attribute' => 'expires_custom', 'consistent_read' => false, @@ -48,6 +50,7 @@ public function testCustomConfig() $this->assertEquals('sessions_custom', $scc->getTableName()); $this->assertEquals('id_custom', $scc->getHashKey()); $this->assertEquals('data_custom', $scc->getDataAttribute()); + $this->assertEquals('binary', $scc->getDataAttributeType()); $this->assertEquals(2019, $scc->getSessionLifetime()); $this->assertEquals('expires_custom', $scc->getSessionLifetimeAttribute()); $this->assertFalse($scc->isConsistentRead()); diff --git a/tests/DynamoDb/StandardSessionConnectionTest.php b/tests/DynamoDb/StandardSessionConnectionTest.php index 64f4e7ad1a..3a4e317280 100644 --- a/tests/DynamoDb/StandardSessionConnectionTest.php +++ b/tests/DynamoDb/StandardSessionConnectionTest.php @@ -22,6 +22,7 @@ public function testStandardConfig() $this->assertEquals('sessions', $scc->getTableName()); $this->assertEquals('id', $scc->getHashKey()); $this->assertEquals('data', $scc->getDataAttribute()); + $this->assertEquals('string', $scc->getDataAttributeType()); $this->assertEquals((int) ini_get('session.gc_maxlifetime'), $scc->getSessionLifetime()); $this->assertEquals('expires', $scc->getSessionLifetimeAttribute()); $this->assertTrue($scc->isConsistentRead()); @@ -38,6 +39,7 @@ public function testCustomConfig() 'table_name' => 'sessions_custom', 'hash_key' => 'id_custom', 'data_attribute' => 'data_custom', + 'data_attribute_type' => 'binary', 'session_lifetime' => 2019, 'session_lifetime_attribute' => 'expires_custom', 'consistent_read' => false, @@ -51,6 +53,7 @@ public function testCustomConfig() $this->assertEquals('sessions_custom', $scc->getTableName()); $this->assertEquals('id_custom', $scc->getHashKey()); $this->assertEquals('data_custom', $scc->getDataAttribute()); + $this->assertEquals('binary', $scc->getDataAttributeType()); $this->assertEquals(2019, $scc->getSessionLifetime()); $this->assertEquals('expires_custom', $scc->getSessionLifetimeAttribute()); $this->assertFalse($scc->isConsistentRead()); @@ -66,7 +69,8 @@ public function testReadRetrievesItemData() $this->addMockResults($client, [ new Result(['Item' => [ 'sessionid' => ['S' => 'session1'], - 'otherkey' => ['S' => 'foo'] + 'otherkey' => ['S' => 'foo'], + 'binarykey' => ['B' => 'bar'] ]]), ]); @@ -83,7 +87,7 @@ public function testReadRetrievesItemData() $data = $connection->read('session1'); $this->assertEquals( - ['sessionid' => 'session1', 'otherkey' => 'foo'], + ['sessionid' => 'session1', 'otherkey' => 'foo', 'binarykey' => 'bar'], $data ); } @@ -109,12 +113,29 @@ public function testWriteUpdatesItemData() $this->assertArrayHasKey('expires', $updates); $this->assertArrayHasKey('lock', $updates); $this->assertArrayHasKey('data', $updates); + $this->assertArrayHasKey('S', $updates['data']['Value']); })); $connection = new StandardSessionConnection($client); $return = $connection->write('s1', serialize(['foo' => 'bar']), true); $this->assertTrue($return); } + public function testWriteUpdatesItemDataAsBinary() + { + $client = $this->getTestSdk()->createDynamoDb(); + $this->addMockResults($client, [new Result([])]); + $client->getHandlerList()->appendBuild(Middleware::tap(function ($command) { + $updates = $command['AttributeUpdates']; + $this->assertArrayHasKey('data', $updates); + $this->assertArrayHasKey('B', $updates['data']['Value']); + })); + $connection = new StandardSessionConnection($client, [ + 'data_attribute_type' => 'binary', + ]); + $return = $connection->write('s1', serialize(['foo' => 'bar']), true); + $this->assertTrue($return); + } + public function testWriteReturnsFalseOnFailure() { $client = $this->getTestSdk()->createDynamoDb();