create-prisma-php-app 1.10.2 → 1.10.4
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,39 @@ 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|array|null $key The key(s) of the state to reset. If null, resets the entire state.
|
|
100
|
+
* Can be a string for a single key or an array of strings for multiple keys.
|
|
98
101
|
* @param bool $clearFromStorage Whether to clear the state from storage.
|
|
99
102
|
*/
|
|
100
|
-
public function resetState($clearFromStorage = false)
|
|
103
|
+
public function resetState($key = null, $clearFromStorage = false)
|
|
101
104
|
{
|
|
102
|
-
$
|
|
105
|
+
if ($key === null) {
|
|
106
|
+
// Reset the entire state
|
|
107
|
+
$this->state = [];
|
|
108
|
+
} elseif (is_array($key)) {
|
|
109
|
+
// Reset only the parts of the state identified by the keys in the array
|
|
110
|
+
foreach ($key as $k) {
|
|
111
|
+
unset($this->state[$k]);
|
|
112
|
+
}
|
|
113
|
+
} else {
|
|
114
|
+
// Reset only the part of the state identified by a single key
|
|
115
|
+
unset($this->state[$key]);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Notify all listeners about the state change
|
|
103
119
|
foreach ($this->listeners as $listener) {
|
|
104
120
|
call_user_func($listener, $this->state);
|
|
105
121
|
}
|
|
122
|
+
|
|
106
123
|
if ($clearFromStorage) {
|
|
107
|
-
|
|
124
|
+
// Save the updated state to the session or clear it
|
|
125
|
+
if (empty($this->state)) {
|
|
126
|
+
unset($_SESSION['appState']);
|
|
127
|
+
} else {
|
|
128
|
+
$_SESSION['appState'] = json_encode($this->state);
|
|
129
|
+
}
|
|
108
130
|
}
|
|
109
131
|
}
|
|
110
132
|
}
|