create-prisma-php-app 1.10.2 → 1.10.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -7,6 +7,7 @@ namespace Lib;
|
|
|
7
7
|
*/
|
|
8
8
|
class StateManager
|
|
9
9
|
{
|
|
10
|
+
private const APP_STATE = 'appState';
|
|
10
11
|
private $state;
|
|
11
12
|
private $listeners;
|
|
12
13
|
|
|
@@ -76,7 +77,7 @@ class StateManager
|
|
|
76
77
|
*/
|
|
77
78
|
private function saveState()
|
|
78
79
|
{
|
|
79
|
-
$_SESSION[
|
|
80
|
+
$_SESSION[self::APP_STATE] = json_encode($this->state);
|
|
80
81
|
}
|
|
81
82
|
|
|
82
83
|
/**
|
|
@@ -84,8 +85,8 @@ class StateManager
|
|
|
84
85
|
*/
|
|
85
86
|
private function loadState()
|
|
86
87
|
{
|
|
87
|
-
if (isset($_SESSION[
|
|
88
|
-
$this->state = json_decode($_SESSION[
|
|
88
|
+
if (isset($_SESSION[self::APP_STATE])) {
|
|
89
|
+
$this->state = json_decode($_SESSION[self::APP_STATE], true);
|
|
89
90
|
foreach ($this->listeners as $listener) {
|
|
90
91
|
call_user_func($listener, $this->state);
|
|
91
92
|
}
|
|
@@ -93,18 +94,33 @@ class StateManager
|
|
|
93
94
|
}
|
|
94
95
|
|
|
95
96
|
/**
|
|
96
|
-
* Resets the application state.
|
|
97
|
+
* Resets the application state partially or completely.
|
|
97
98
|
*
|
|
99
|
+
* @param string|null $key The key of the state to reset. If null, resets the entire state.
|
|
98
100
|
* @param bool $clearFromStorage Whether to clear the state from storage.
|
|
99
101
|
*/
|
|
100
|
-
public function resetState($clearFromStorage = false)
|
|
102
|
+
public function resetState($key = null, $clearFromStorage = false)
|
|
101
103
|
{
|
|
102
|
-
$
|
|
104
|
+
if ($key === null) {
|
|
105
|
+
// Reset the entire state
|
|
106
|
+
$this->state = [];
|
|
107
|
+
} else {
|
|
108
|
+
// Reset only the part of the state identified by $key
|
|
109
|
+
unset($this->state[$key]);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Notify all listeners about the state change
|
|
103
113
|
foreach ($this->listeners as $listener) {
|
|
104
114
|
call_user_func($listener, $this->state);
|
|
105
115
|
}
|
|
116
|
+
|
|
106
117
|
if ($clearFromStorage) {
|
|
107
|
-
|
|
118
|
+
// Save the updated state to the session or clear it
|
|
119
|
+
if (empty($this->state)) {
|
|
120
|
+
unset($_SESSION[self::APP_STATE]);
|
|
121
|
+
} else {
|
|
122
|
+
$_SESSION[self::APP_STATE] = json_encode($this->state);
|
|
123
|
+
}
|
|
108
124
|
}
|
|
109
125
|
}
|
|
110
126
|
}
|