From 670320a7ce2de9ba59cd92b72b7bfe9dd441efb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Rancoud?= Date: Tue, 8 Dec 2020 15:59:15 +0100 Subject: [PATCH] Limit count calls of setupAndStart * add attribute `$hasChanged` * on each modifications Session informations `$hasChanged` is reset to `true` * `$hasChanged` is put back to false in `setupAndStart` --- src/Session.php | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Session.php b/src/Session.php index cfeb70a..e299921 100644 --- a/src/Session.php +++ b/src/Session.php @@ -16,6 +16,9 @@ class Session extends DriverManager /** @var bool */ protected static bool $hasStarted = false; + /** @var bool */ + protected static bool $hasChanged = true; + /** @var array */ protected static array $options = [ 'read_and_close' => true, @@ -46,7 +49,7 @@ public static function start(array $options = []): void */ protected static function populateOptions(array $options = []): void { - self::validateOptions($options); + static::validateOptions($options); static::setOptions($options); } @@ -56,6 +59,12 @@ protected static function populateOptions(array $options = []): void */ protected static function setupAndStart(): void { + if (static::$hasChanged === false) { + return; + } + + static::$hasChanged = false; + static::configureDriver(); static::setupSessionParameters(); static::startSession(); @@ -67,7 +76,6 @@ protected static function setupAndStart(): void if (static::$hasStarted === false && !empty(static::$flashData) && static::isReadOnly()) { static::setReadWrite(); - static::setupSessionParameters(); static::startSession(); } unset($_SESSION['flash_data']); @@ -203,6 +211,7 @@ public static function regenerate(): bool */ public static function destroy(): bool { + static::$hasChanged = true; \session_unset(); return \session_destroy(); @@ -212,6 +221,7 @@ public static function commit(): void { static::$hasStarted = false; static::$flashData = []; + static::$hasChanged = true; \session_write_close(); } @@ -231,6 +241,7 @@ public static function unsaved(): bool { static::$hasStarted = false; static::$flashData = []; + static::$hasChanged = true; return \session_abort(); } @@ -258,6 +269,7 @@ public static function getId(): string */ public static function setId(string $id): string { + static::$hasChanged = true; return \session_id($id); } @@ -294,11 +306,13 @@ protected static function startSessionIfNotHasStartedForceWrite(): void public static function setReadOnly(): void { + static::$hasChanged = true; static::$options['read_and_close'] = true; } public static function setReadWrite(): void { + static::$hasChanged = true; static::$options['read_and_close'] = false; } @@ -325,6 +339,7 @@ protected static function getLifetimeForRedis(): int */ public static function setOption(string $key, $value): void { + static::$hasChanged = true; static::validateOptions([$key => $value]); static::$options[$key] = $value; } @@ -336,6 +351,7 @@ public static function setOption(string $key, $value): void */ public static function setOptions(array $options): void { + static::$hasChanged = true; static::validateOptions($options); static::$options = $options + static::$options; }