Skip to content

Commit 3942353

Browse files
authored
Allow a custom Session Id separator (#792)
* Allow a custom Session Id separator * fix linting
1 parent 9ae413b commit 3942353

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/SessionHandler.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ class SessionHandler implements \SessionHandlerInterface
4646
* hash_key?: string,
4747
* session_lifetime?: int,
4848
* session_lifetime_attribute?: string,
49-
* table_name: string
49+
* table_name: string,
50+
* id_separator?: string
5051
* } $options
5152
*/
5253
public function __construct(DynamoDbClient $client, array $options)
@@ -56,6 +57,7 @@ public function __construct(DynamoDbClient $client, array $options)
5657
$this->options['data_attribute'] = $this->options['data_attribute'] ?? 'data';
5758
$this->options['hash_key'] = $this->options['hash_key'] ?? 'id';
5859
$this->options['session_lifetime_attribute'] = $this->options['session_lifetime_attribute'] ?? 'expires';
60+
$this->options['id_separator'] = $this->options['id_separator'] ?? '_';
5961
}
6062

6163
public function setUp(): void
@@ -193,7 +195,7 @@ private function doWrite(string $id, bool $updateData, string $data = ''): bool
193195

194196
private function formatId(string $id): string
195197
{
196-
return trim($this->sessionName . '_' . $id, '_');
198+
return trim($this->sessionName . $this->options['id_separator'] . $id, $this->options['id_separator']);
197199
}
198200

199201
private function formatKey(string $key): array

tests/SessionHandlerTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,4 +267,26 @@ public function testSetUp()
267267

268268
$this->handler->setUp();
269269
}
270+
271+
public function testCustomKeySeparator()
272+
{
273+
$handler = new SessionHandler($this->client, ['table_name' => 'testTable', 'session_lifetime' => 86400, 'id_separator' => '#']);
274+
$handler->open(null, 'PHPSESSID');
275+
276+
$this->client
277+
->expects(self::once())
278+
->method('updateItem')
279+
->with(self::equalTo([
280+
'TableName' => 'testTable',
281+
'Key' => [
282+
'id' => ['S' => 'PHPSESSID#123456789'],
283+
],
284+
'AttributeUpdates' => [
285+
'expires' => ['Value' => ['N' => time() + 86400]],
286+
'data' => ['Value' => ['S' => 'test data']],
287+
],
288+
], 10));
289+
290+
self::assertTrue($handler->write('123456789', 'test data'));
291+
}
270292
}

0 commit comments

Comments
 (0)