@@ -34,6 +34,7 @@ class CacheSessionPersistence implements SessionPersistenceInterface
34
34
private CacheItemPoolInterface $ cache ;
35
35
36
36
private bool $ persistent ;
37
+ private bool $ autoRegenerate ;
37
38
38
39
/**
39
40
* Prepare session cache and default HTTP caching headers.
@@ -67,6 +68,8 @@ class CacheSessionPersistence implements SessionPersistenceInterface
67
68
* be accessed by client-side apis.
68
69
* @param string $cookieSameSite The same-site rule to apply to the persisted
69
70
* cookie. Options include "Lax", "Strict", and "None".
71
+ * @param bool $autoRegenerate Whether or not the session ID should be
72
+ * regenerated on session data changes
70
73
* @todo reorder the constructor arguments
71
74
*/
72
75
public function __construct (
@@ -80,7 +83,8 @@ public function __construct(
80
83
?string $ cookieDomain = null ,
81
84
bool $ cookieSecure = false ,
82
85
bool $ cookieHttpOnly = false ,
83
- string $ cookieSameSite = 'Lax '
86
+ string $ cookieSameSite = 'Lax ' ,
87
+ bool $ autoRegenerate = true
84
88
) {
85
89
$ this ->cache = $ cache ;
86
90
@@ -112,6 +116,8 @@ public function __construct(
112
116
: $ this ->getLastModified ();
113
117
114
118
$ this ->persistent = $ persistent ;
119
+
120
+ $ this ->autoRegenerate = $ autoRegenerate ;
115
121
}
116
122
117
123
public function initializeSessionFromRequest (ServerRequestInterface $ request ): SessionInterface
@@ -139,8 +145,8 @@ public function persistSession(SessionInterface $session, ResponseInterface $res
139
145
// Regenerate the session if:
140
146
// - we have no session identifier
141
147
// - the session is marked as regenerated
142
- // - the session has changed (data is different)
143
- if ('' === $ id || $ session ->isRegenerated () || $ session ->hasChanged ()) {
148
+ // - the session has changed (data is different) and autoRegenerate is turned on (default) in the configuration
149
+ if ('' === $ id || $ session ->isRegenerated () || ( $ this -> autoRegenerate && $ session ->hasChanged () )) {
144
150
$ id = $ this ->regenerateSession ($ id );
145
151
}
146
152
0 commit comments