diff --git a/psalm-baseline.xml b/psalm-baseline.xml
index 4d68d07..7008dfd 100644
--- a/psalm-baseline.xml
+++ b/psalm-baseline.xml
@@ -3,6 +3,7 @@
$sessionData
+ toArray()]]>
array
@@ -14,6 +15,9 @@
$persistent
+
+ getId
+
@@ -70,5 +74,10 @@
cacheHeaders
validCacheLimiters
+
+ getId
+ getId
+ getId
+
diff --git a/src/CacheSessionPersistence.php b/src/CacheSessionPersistence.php
index 13f3c27..c27c77a 100644
--- a/src/CacheSessionPersistence.php
+++ b/src/CacheSessionPersistence.php
@@ -4,6 +4,7 @@
namespace Mezzio\Session\Cache;
+use Mezzio\Session\InitializePersistenceIdInterface;
use Mezzio\Session\Persistence\CacheHeadersGeneratorTrait;
use Mezzio\Session\Persistence\Http;
use Mezzio\Session\Persistence\SessionCookieAwareTrait;
@@ -26,7 +27,7 @@
* During persistence, if the session regeneration flag is true, a new session
* identifier is created, and the session re-started.
*/
-class CacheSessionPersistence implements SessionPersistenceInterface
+class CacheSessionPersistence implements InitializePersistenceIdInterface, SessionPersistenceInterface
{
use CacheHeadersGeneratorTrait;
use SessionCookieAwareTrait;
@@ -197,4 +198,13 @@ private function persistSessionDataToCache(string $id, array $data): void
$item->expiresAfter($this->cacheExpire);
$this->cache->save($item);
}
+
+ public function initializeId(SessionInterface $session): SessionInterface
+ {
+ if ($session->getId() === '' || $session->isRegenerated()) {
+ $session = new Session($session->toArray(), $this->generateSessionId());
+ }
+
+ return $session;
+ }
}
diff --git a/test/CacheSessionPersistenceTest.php b/test/CacheSessionPersistenceTest.php
index 8d88d25..472fc3d 100644
--- a/test/CacheSessionPersistenceTest.php
+++ b/test/CacheSessionPersistenceTest.php
@@ -956,4 +956,45 @@ public function testPersistenceDurationInSessionDataWithValueOfZeroIgnoresGlobal
$this->assertNotSame($response, $result);
$this->assertCookieHasNoExpiryDirective($result);
}
+
+ public function testInitializeIdReturnsSessionWithId(): void
+ {
+ $persistence = new CacheSessionPersistence(
+ $this->cachePool,
+ 'test',
+ );
+ $session = new Session(['foo' => 'bar']);
+ $actual = $persistence->initializeId($session);
+
+ $this->assertNotSame($session, $actual);
+ $this->assertNotEmpty($actual->getId());
+ $this->assertSame(['foo' => 'bar'], $actual->toArray());
+ }
+
+ public function testInitializeIdRegeneratesSessionId(): void
+ {
+ $persistence = new CacheSessionPersistence(
+ $this->cachePool,
+ 'test',
+ );
+ $session = new Session(['foo' => 'bar'], 'original-id');
+ $session = $session->regenerate();
+ $actual = $persistence->initializeId($session);
+
+ $this->assertNotEmpty($actual->getId());
+ $this->assertNotSame('original-id', $actual->getId());
+ $this->assertFalse($actual->isRegenerated());
+ }
+
+ public function testInitializeIdReturnsSessionUnaltered(): void
+ {
+ $persistence = new CacheSessionPersistence(
+ $this->cachePool,
+ 'test',
+ );
+ $session = new Session(['foo' => 'bar'], 'original-id');
+ $actual = $persistence->initializeId($session);
+
+ $this->assertSame($session, $actual);
+ }
}