@@ -18,35 +18,30 @@ class MysqlSessionHandler implements SessionHandlerInterface
18
18
19
19
use SmartObject;
20
20
21
- /** @var string */
22
- private $ tableName ;
23
21
24
- /** @var integer */
25
- private $ lockTimeout = 5 ;
22
+ private Context $ context ;
26
23
27
- /** @var integer */
28
- private $ unchangedUpdateDelay = 300 ;
24
+ private ?StaticKeyEncryption $ encryptionService = null ;
29
25
30
- /** @var Context */
31
- private $ context ;
26
+ private string $ tableName ;
32
27
33
- /** @var string */
34
- private $ lockId ;
28
+ private int $ lockTimeout = 5 ;
29
+
30
+ private int $ unchangedUpdateDelay = 300 ;
31
+
32
+ private ?string $ lockId = null ;
35
33
36
34
/** @var string[] */
37
- private $ idHashes = [];
35
+ private array $ idHashes = [];
38
36
39
- /** @var ActiveRow */
40
- private $ row ;
37
+ /** @var ActiveRow{data:string, timestamp:int}|null */
38
+ private ? ActiveRow $ row ;
41
39
42
40
/** @var string[] */
43
- private $ data = [];
41
+ private array $ data = [];
44
42
45
43
/** @var mixed[] */
46
- private $ additionalData = [];
47
-
48
- /** @var StaticKeyEncryption */
49
- private $ encryptionService ;
44
+ private array $ additionalData = [];
50
45
51
46
/**
52
47
* Occurs before the data is written to session.
@@ -108,8 +103,11 @@ private function hash(string $id, bool $rawOutput = true): string
108
103
private function lock (): void
109
104
{
110
105
if ($ this ->lockId === null ) {
111
- $ this ->lockId = $ this ->hash (\session_id (), false );
112
- $ this ->context ->query ('SELECT GET_LOCK(?, ?) as `lock` ' , $ this ->lockId , $ this ->lockTimeout );
106
+ $ sessionId = \session_id ();
107
+ if ($ sessionId ) {
108
+ $ this ->lockId = $ this ->hash ($ sessionId , false );
109
+ $ this ->context ->query ('SELECT GET_LOCK(?, ?) as `lock` ' , $ this ->lockId , $ this ->lockTimeout );
110
+ }
113
111
}
114
112
}
115
113
@@ -128,7 +126,7 @@ private function unlock(): void
128
126
/**
129
127
* @param string $savePath
130
128
* @param string $name
131
- * @return boolean
129
+ * @return bool
132
130
*/
133
131
public function open ($ savePath , $ name ): bool
134
132
{
@@ -146,7 +144,7 @@ public function close(): bool
146
144
147
145
/**
148
146
* @param string $sessionId
149
- * @return boolean
147
+ * @return bool
150
148
*/
151
149
public function destroy ($ sessionId ): bool
152
150
{
@@ -178,7 +176,7 @@ public function read($sessionId): string
178
176
/**
179
177
* @param string $sessionId
180
178
* @param string $sessionData
181
- * @return boolean
179
+ * @return bool
182
180
*/
183
181
public function write ($ sessionId , $ sessionData ): bool
184
182
{
@@ -191,7 +189,8 @@ public function write($sessionId, $sessionData): bool
191
189
$ sessionData = $ this ->encryptionService ->encrypt ($ sessionData );
192
190
}
193
191
$ this ->onBeforeDataWrite ();
194
- if ($ row = $ this ->context ->table ($ this ->tableName )->get ($ hashedSessionId )) {
192
+ $ row = $ this ->context ->table ($ this ->tableName )->get ($ hashedSessionId );
193
+ if ($ row ) {
195
194
$ row ->update ([
196
195
'timestamp ' => $ time ,
197
196
'data ' => $ sessionData ,
@@ -203,7 +202,7 @@ public function write($sessionId, $sessionData): bool
203
202
'data ' => $ sessionData ,
204
203
] + $ this ->additionalData );
205
204
}
206
- } elseif ($ this ->unchangedUpdateDelay === 0 || $ time - $ this ->row ->timestamp > $ this ->unchangedUpdateDelay ) {
205
+ } elseif (( $ this ->unchangedUpdateDelay === 0 || $ time - $ this ->row ->timestamp > $ this ->unchangedUpdateDelay ) && $ this -> row ) {
207
206
// Optimization: When data has not been changed, only update
208
207
// the timestamp after a configured delay, if any.
209
208
$ this ->row ->update ([
@@ -216,8 +215,8 @@ public function write($sessionId, $sessionData): bool
216
215
217
216
218
217
/**
219
- * @param integer $maxLifeTime
220
- * @return boolean
218
+ * @param int $maxLifeTime
219
+ * @return bool
221
220
*/
222
221
public function gc ($ maxLifeTime ): bool
223
222
{
0 commit comments