Skip to content

Commit b5afd48

Browse files
authored
Limit count calls of setupAndStart (#16)
* add attribute `$hasChanged` * on each modifications Session informations `$hasChanged` is reset to `true` * `$hasChanged` is put back to false in `setupAndStart`
1 parent 1ff66c0 commit b5afd48

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

‎src/Session.php‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ class Session extends DriverManager
1616
/** @var bool */
1717
protected static bool $hasStarted = false;
1818

19+
/** @var bool */
20+
protected static bool $hasChanged = true;
21+
1922
/** @var array */
2023
protected static array $options = [
2124
'read_and_close' => true,
@@ -46,7 +49,7 @@ public static function start(array $options = []): void
4649
*/
4750
protected static function populateOptions(array $options = []): void
4851
{
49-
self::validateOptions($options);
52+
static::validateOptions($options);
5053

5154
static::setOptions($options);
5255
}
@@ -56,6 +59,12 @@ protected static function populateOptions(array $options = []): void
5659
*/
5760
protected static function setupAndStart(): void
5861
{
62+
if (static::$hasChanged === false) {
63+
return;
64+
}
65+
66+
static::$hasChanged = false;
67+
5968
static::configureDriver();
6069
static::setupSessionParameters();
6170
static::startSession();
@@ -67,7 +76,6 @@ protected static function setupAndStart(): void
6776

6877
if (static::$hasStarted === false && !empty(static::$flashData) && static::isReadOnly()) {
6978
static::setReadWrite();
70-
static::setupSessionParameters();
7179
static::startSession();
7280
}
7381
unset($_SESSION['flash_data']);
@@ -203,6 +211,7 @@ public static function regenerate(): bool
203211
*/
204212
public static function destroy(): bool
205213
{
214+
static::$hasChanged = true;
206215
\session_unset();
207216

208217
return \session_destroy();
@@ -212,6 +221,7 @@ public static function commit(): void
212221
{
213222
static::$hasStarted = false;
214223
static::$flashData = [];
224+
static::$hasChanged = true;
215225

216226
\session_write_close();
217227
}
@@ -231,6 +241,7 @@ public static function unsaved(): bool
231241
{
232242
static::$hasStarted = false;
233243
static::$flashData = [];
244+
static::$hasChanged = true;
234245

235246
return \session_abort();
236247
}
@@ -258,6 +269,7 @@ public static function getId(): string
258269
*/
259270
public static function setId(string $id): string
260271
{
272+
static::$hasChanged = true;
261273
return \session_id($id);
262274
}
263275

@@ -294,11 +306,13 @@ protected static function startSessionIfNotHasStartedForceWrite(): void
294306

295307
public static function setReadOnly(): void
296308
{
309+
static::$hasChanged = true;
297310
static::$options['read_and_close'] = true;
298311
}
299312

300313
public static function setReadWrite(): void
301314
{
315+
static::$hasChanged = true;
302316
static::$options['read_and_close'] = false;
303317
}
304318

@@ -325,6 +339,7 @@ protected static function getLifetimeForRedis(): int
325339
*/
326340
public static function setOption(string $key, $value): void
327341
{
342+
static::$hasChanged = true;
328343
static::validateOptions([$key => $value]);
329344
static::$options[$key] = $value;
330345
}
@@ -336,6 +351,7 @@ public static function setOption(string $key, $value): void
336351
*/
337352
public static function setOptions(array $options): void
338353
{
354+
static::$hasChanged = true;
339355
static::validateOptions($options);
340356
static::$options = $options + static::$options;
341357
}

0 commit comments

Comments
 (0)