Skip to content

Commit b99e723

Browse files
authored
Merge pull request #52 from SmartFrame-Technologies/initialize-persistence-id
Implement InitializePersistenceIdInterface
2 parents 0118d65 + c601591 commit b99e723

3 files changed

+61
-1
lines changed

psalm-baseline.xml

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<file src="src/CacheSessionPersistence.php">
44
<MixedArgumentTypeCoercion>
55
<code>$sessionData</code>
6+
<code><![CDATA[$session->toArray()]]></code>
67
</MixedArgumentTypeCoercion>
78
<MixedInferredReturnType>
89
<code>array</code>
@@ -14,6 +15,9 @@
1415
<UnusedProperty>
1516
<code>$persistent</code>
1617
</UnusedProperty>
18+
<UndefinedInterfaceMethod>
19+
<code>getId</code>
20+
</UndefinedInterfaceMethod>
1721
</file>
1822
<file src="src/CacheSessionPersistenceFactory.php">
1923
<MixedArgument>
@@ -70,5 +74,10 @@
7074
<code>cacheHeaders</code>
7175
<code>validCacheLimiters</code>
7276
</PossiblyUnusedMethod>
77+
<UndefinedInterfaceMethod>
78+
<code>getId</code>
79+
<code>getId</code>
80+
<code>getId</code>
81+
</UndefinedInterfaceMethod>
7382
</file>
7483
</files>

src/CacheSessionPersistence.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Mezzio\Session\Cache;
66

7+
use Mezzio\Session\InitializePersistenceIdInterface;
78
use Mezzio\Session\Persistence\CacheHeadersGeneratorTrait;
89
use Mezzio\Session\Persistence\Http;
910
use Mezzio\Session\Persistence\SessionCookieAwareTrait;
@@ -26,7 +27,7 @@
2627
* During persistence, if the session regeneration flag is true, a new session
2728
* identifier is created, and the session re-started.
2829
*/
29-
class CacheSessionPersistence implements SessionPersistenceInterface
30+
class CacheSessionPersistence implements InitializePersistenceIdInterface, SessionPersistenceInterface
3031
{
3132
use CacheHeadersGeneratorTrait;
3233
use SessionCookieAwareTrait;
@@ -197,4 +198,13 @@ private function persistSessionDataToCache(string $id, array $data): void
197198
$item->expiresAfter($this->cacheExpire);
198199
$this->cache->save($item);
199200
}
201+
202+
public function initializeId(SessionInterface $session): SessionInterface
203+
{
204+
if ($session->getId() === '' || $session->isRegenerated()) {
205+
$session = new Session($session->toArray(), $this->generateSessionId());
206+
}
207+
208+
return $session;
209+
}
200210
}

test/CacheSessionPersistenceTest.php

+41
Original file line numberDiff line numberDiff line change
@@ -956,4 +956,45 @@ public function testPersistenceDurationInSessionDataWithValueOfZeroIgnoresGlobal
956956
$this->assertNotSame($response, $result);
957957
$this->assertCookieHasNoExpiryDirective($result);
958958
}
959+
960+
public function testInitializeIdReturnsSessionWithId(): void
961+
{
962+
$persistence = new CacheSessionPersistence(
963+
$this->cachePool,
964+
'test',
965+
);
966+
$session = new Session(['foo' => 'bar']);
967+
$actual = $persistence->initializeId($session);
968+
969+
$this->assertNotSame($session, $actual);
970+
$this->assertNotEmpty($actual->getId());
971+
$this->assertSame(['foo' => 'bar'], $actual->toArray());
972+
}
973+
974+
public function testInitializeIdRegeneratesSessionId(): void
975+
{
976+
$persistence = new CacheSessionPersistence(
977+
$this->cachePool,
978+
'test',
979+
);
980+
$session = new Session(['foo' => 'bar'], 'original-id');
981+
$session = $session->regenerate();
982+
$actual = $persistence->initializeId($session);
983+
984+
$this->assertNotEmpty($actual->getId());
985+
$this->assertNotSame('original-id', $actual->getId());
986+
$this->assertFalse($actual->isRegenerated());
987+
}
988+
989+
public function testInitializeIdReturnsSessionUnaltered(): void
990+
{
991+
$persistence = new CacheSessionPersistence(
992+
$this->cachePool,
993+
'test',
994+
);
995+
$session = new Session(['foo' => 'bar'], 'original-id');
996+
$actual = $persistence->initializeId($session);
997+
998+
$this->assertSame($session, $actual);
999+
}
9591000
}

0 commit comments

Comments
 (0)