Skip to content

Commit

Permalink
Change sessions written/read trackers from scalars to id => scalar ma…
Browse files Browse the repository at this point in the history
…ppings
  • Loading branch information
jeskew committed Nov 13, 2015
1 parent 1625833 commit 1c5c92f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 19 deletions.
42 changes: 23 additions & 19 deletions src/DynamoDb/SessionHandler.php
Expand Up @@ -29,10 +29,10 @@ class SessionHandler implements \SessionHandlerInterface
/** @var string Session name. */
private $sessionName;

/** @var string Stores serialized data for tracking changes. */
/** @var array Stores serialized data for tracking changes. */
private $dataRead;

/** @var bool Keeps track of whether the session has been written. */
/** @var array Keeps track of whether the session has been written. */
private $sessionWritten;

/**
Expand Down Expand Up @@ -108,15 +108,15 @@ public function open($savePath, $sessionName)
*/
public function close()
{
$id = $this->formatId(session_id());
// Make sure the session is unlocked and the expiration time is updated,
// even if the write did not occur
if (!$this->sessionWritten) {
$id = $this->formatId(session_id());
if (!$this->sessionWritten[$id]) {
$result = $this->connection->write($id, '', false);
$this->sessionWritten = (bool) $result;
$this->sessionWritten[$id] = (bool) $result;
}

return $this->sessionWritten;
return $this->sessionWritten[$id];
}

/**
Expand All @@ -128,23 +128,24 @@ public function close()
*/
public function read($id)
{
$formattedId = $this->formatId($id);
// PHP expects an empty string to be returned from this method if no
// data is retrieved
$this->dataRead = '';
$this->dataRead[$formattedId] = '';

// Get session data using the selected locking strategy
$item = $this->connection->read($this->formatId($id));
$item = $this->connection->read($formattedId);

// Return the data if it is not expired. If it is expired, remove it
if (isset($item['expires']) && isset($item['data'])) {
$this->dataRead = $item['data'];
$this->dataRead[$formattedId] = $item['data'];
if ($item['expires'] <= time()) {
$this->dataRead = '';
$this->dataRead[$formattedId] = '';
$this->destroy($id);
}
}

return $this->dataRead;
return $this->dataRead[$formattedId];
}

/**
Expand All @@ -157,14 +158,15 @@ public function read($id)
*/
public function write($id, $data)
{
$formattedId = $this->formatId($id);
$changed = empty($this->dataRead[$formattedId])
|| ($data !== $this->dataRead[$formattedId]);

// Write the session data using the selected locking strategy
$this->sessionWritten = $this->connection->write(
$this->formatId($id),
$data,
($data !== $this->dataRead)
);
$this->sessionWritten[$formattedId] = $this->connection
->write($formattedId, $data, $changed);

return $this->sessionWritten;
return $this->sessionWritten[$formattedId];
}

/**
Expand All @@ -176,10 +178,12 @@ public function write($id, $data)
*/
public function destroy($id)
{
$formattedId = $this->formatId($id);
// Delete the session data using the selected locking strategy
$this->sessionWritten = $this->connection->delete($this->formatId($id));
$this->sessionWritten[$formattedId]
= $this->connection->delete($formattedId);

return $this->sessionWritten;
return $this->sessionWritten[$formattedId];
}

/**
Expand Down
26 changes: 26 additions & 0 deletions tests/DynamoDb/SessionHandlerTest.php
Expand Up @@ -69,4 +69,30 @@ public function testHandlerWhenNothingWritten()
$sh->open('', 'name');
$this->assertTrue($sh->close());
}

public function testSessionDataCanBeWrittenToNewIdWithNoChanges()
{
$data = 'serializedData';
$connection = $this->getMockForAbstractClass(
'Aws\DynamoDb\SessionConnectionInterface'
);
$connection->expects($this->once())
->method('read')
->with('name_test1')
->willReturn([
'expires' => time() + 1000,
'data' => $data,
]);
$connection->expects($this->once())
->method('write')
->with('name_test2', $data, true)
->willReturn(true);

$sh = new SessionHandler($connection);
session_id('test1');
$this->assertTrue($sh->open('', 'name'));
$this->assertSame($data, $sh->read(session_id()));
session_id('test2');
$this->assertTrue($sh->write(session_id(), $data));
}
}

0 comments on commit 1c5c92f

Please sign in to comment.