Skip to content

Commit 025e5f3

Browse files
authored
Doc types, param names (#9)
- Add variable to docblock otherwise it's ignored - Replace a deprecated call - Replace docblocks with type declarations - Document `onBeforeDataWrite()` added by `SmartObject` due to `$onBeforeDataWrite` - Rename parameters to what's used on the `SessionHandlerInterface` interface
2 parents 80e4ee4 + 7c06f8d commit 025e5f3

File tree

2 files changed

+22
-42
lines changed

2 files changed

+22
-42
lines changed

src/DI/MysqlSessionHandlerExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function loadConfiguration(): void
3333
$builder = $this->getContainerBuilder();
3434

3535
$definition = $builder->addDefinition($this->prefix('sessionHandler'))
36-
->setClass('Spaze\Session\MysqlSessionHandler')
36+
->setType('Spaze\Session\MysqlSessionHandler')
3737
->addSetup('setTableName', [$this->config->tableName])
3838
->addSetup('setLockTimeout', [$this->config->lockTimeout])
3939
->addSetup('setUnchangedUpdateDelay', [$this->config->unchangedUpdateDelay]);
@@ -42,7 +42,7 @@ public function loadConfiguration(): void
4242
$definition->addSetup('setEncryptionService', [$this->config->encryptionService]);
4343
}
4444

45-
/** @var ServiceDefinition */
45+
/** @var ServiceDefinition $sessionDefinition */
4646
$sessionDefinition = $builder->getDefinition('session');
4747
$sessionSetup = $sessionDefinition->getSetup();
4848
# Prepend setHandler method to other possible setups (setExpiration) which would start session prematurely

src/MysqlSessionHandler.php

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
/**
1313
* Storing session to database.
1414
* Inspired by: https://github.com/JedenWeb/SessionStorage/
15+
*
16+
* @method onBeforeDataWrite()
1517
*/
1618
class MysqlSessionHandler implements SessionHandlerInterface
1719
{
@@ -46,7 +48,7 @@ class MysqlSessionHandler implements SessionHandlerInterface
4648
*
4749
* @var callable[] function ()
4850
*/
49-
public $onBeforeDataWrite;
51+
public array $onBeforeDataWrite;
5052

5153

5254
public function __construct(
@@ -79,11 +81,7 @@ public function setEncryptionService(StaticKeyEncryption $encryptionService): vo
7981
}
8082

8183

82-
/**
83-
* @param string $key
84-
* @param mixed $value
85-
*/
86-
public function setAdditionalData(string $key, $value): void
84+
public function setAdditionalData(string $key, mixed $value): void
8785
{
8886
$this->additionalData[$key] = $value;
8987
}
@@ -121,12 +119,7 @@ private function unlock(): void
121119
}
122120

123121

124-
/**
125-
* @param string $savePath
126-
* @param string $name
127-
* @return bool
128-
*/
129-
public function open($savePath, $name): bool
122+
public function open(string $path, string $name): bool
130123
{
131124
$this->lock();
132125
return true;
@@ -140,64 +133,51 @@ public function close(): bool
140133
}
141134

142135

143-
/**
144-
* @param string $sessionId
145-
* @return bool
146-
*/
147-
public function destroy($sessionId): bool
136+
public function destroy(string $id): bool
148137
{
149-
$hashedSessionId = $this->hash($sessionId);
138+
$hashedSessionId = $this->hash($id);
150139
$this->explorer->table($this->tableName)->where('id', $hashedSessionId)->delete();
151140
$this->unlock();
152141
return true;
153142
}
154143

155144

156-
/**
157-
* @param string $sessionId
158-
* @return string
159-
*/
160-
public function read($sessionId): string
145+
public function read(string $id): string
161146
{
162147
$this->lock();
163-
$hashedSessionId = $this->hash($sessionId);
148+
$hashedSessionId = $this->hash($id);
164149
$this->row = $this->explorer->table($this->tableName)->get($hashedSessionId);
165150

166151
if ($this->row) {
167-
$this->data[$sessionId] = ($this->encryptionService ? $this->encryptionService->decrypt($this->row->data) : $this->row->data);
168-
return $this->data[$sessionId];
152+
$this->data[$id] = ($this->encryptionService ? $this->encryptionService->decrypt($this->row->data) : $this->row->data);
153+
return $this->data[$id];
169154
}
170155
return '';
171156
}
172157

173158

174-
/**
175-
* @param string $sessionId
176-
* @param string $sessionData
177-
* @return bool
178-
*/
179-
public function write($sessionId, $sessionData): bool
159+
public function write(string $id, string $data): bool
180160
{
181161
$this->lock();
182-
$hashedSessionId = $this->hash($sessionId);
162+
$hashedSessionId = $this->hash($id);
183163
$time = \time();
184164

185-
if (!isset($this->data[$sessionId]) || $this->data[$sessionId] !== $sessionData) {
165+
if (!isset($this->data[$id]) || $this->data[$id] !== $data) {
186166
if ($this->encryptionService) {
187-
$sessionData = $this->encryptionService->encrypt($sessionData);
167+
$data = $this->encryptionService->encrypt($data);
188168
}
189169
$this->onBeforeDataWrite();
190170
$row = $this->explorer->table($this->tableName)->get($hashedSessionId);
191171
if ($row) {
192172
$row->update([
193173
'timestamp' => $time,
194-
'data' => $sessionData,
174+
'data' => $data,
195175
] + $this->additionalData);
196176
} else {
197177
$this->explorer->table($this->tableName)->insert([
198178
'id' => $hashedSessionId,
199179
'timestamp' => $time,
200-
'data' => $sessionData,
180+
'data' => $data,
201181
] + $this->additionalData);
202182
}
203183
} elseif ($this->row && ($this->unchangedUpdateDelay === 0 || $time - $this->row->timestamp > $this->unchangedUpdateDelay)) {
@@ -212,9 +192,9 @@ public function write($sessionId, $sessionData): bool
212192
}
213193

214194

215-
public function gc(int $maxLifeTime): int|false
195+
public function gc(int $max_lifetime): int|false
216196
{
217-
$maxTimestamp = \time() - $maxLifeTime;
197+
$maxTimestamp = \time() - $max_lifetime;
218198

219199
// Try to avoid a conflict when running garbage collection simultaneously on two
220200
// MySQL servers at a very busy site in a master-master replication setup by
@@ -226,7 +206,7 @@ public function gc(int $maxLifeTime): int|false
226206
// subtraction on server 2.
227207
$row = $this->explorer->query('SELECT @@server_id as `serverId`')->fetch();
228208
if ($row && $row->serverId > 1 && $row->serverId < 10) {
229-
$maxTimestamp -= ($row->serverId - 1) * \max(86400, $maxLifeTime / 10);
209+
$maxTimestamp -= ($row->serverId - 1) * \max(86400, $max_lifetime / 10);
230210
}
231211

232212
return $this->explorer->table($this->tableName)

0 commit comments

Comments
 (0)