create-prisma-php-app 1.15.6 → 1.15.7

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.
@@ -8,51 +8,59 @@ namespace Lib;
8
8
  class StateManager
9
9
  {
10
10
  private const APP_STATE = 'app_state_F989A';
11
- private $state;
12
- private $listeners;
11
+ private array $state = [];
12
+ private array $listeners = [];
13
13
 
14
14
  /**
15
15
  * Initializes a new instance of the StateManager class.
16
- *
17
- * @param array $initialState The initial state of the application.
18
16
  */
19
- public function __construct(array $initialState = [])
17
+ public function __construct()
20
18
  {
21
19
  global $isWire;
22
20
 
23
- $this->state = $initialState;
24
- $this->listeners = [];
25
21
  $this->loadState();
26
22
 
27
- if (!$isWire) $this->resetState();
23
+ if (!$isWire) {
24
+ $this->resetState();
25
+ }
28
26
  }
29
27
 
30
28
  /**
31
- * Retrieves the current state of the application.
29
+ * Gets the state value for the specified key.
32
30
  *
33
- * @param string|null $key The key of the state value to retrieve. If null, returns the entire state.
34
- * @return mixed|null The state value corresponding to the given key, or null if the key is not found.
31
+ * @param string|null $key The key of the state value to get.
32
+ * @param mixed $initialValue The initial value to set if the key does not exist.
33
+ * @return mixed The state value for the specified key.
35
34
  */
36
- public function getState($key = null): mixed
35
+ public function getState(string $key = null, mixed $initialValue = null): mixed
37
36
  {
38
37
  if ($key === null) {
39
38
  return new \ArrayObject($this->state, \ArrayObject::ARRAY_AS_PROPS);
40
39
  }
41
40
 
42
- $value = $this->state[$key] ?? null;
41
+ if (!array_key_exists($key, $this->state)) {
42
+ if ($initialValue !== null) {
43
+ $this->setState($key, $initialValue);
44
+ }
45
+ } elseif ($initialValue !== null && $this->state[$key] !== $initialValue) {
46
+ $this->setState($key, $this->state[$key]);
47
+ }
48
+
49
+ $value = $this->state[$key] ?? $initialValue;
50
+
43
51
  return is_array($value) ? new \ArrayObject($value, \ArrayObject::ARRAY_AS_PROPS) : $value;
44
52
  }
45
53
 
46
54
  /**
47
- * Updates the application state with the given update.
55
+ * Sets the state value for the specified key.
48
56
  *
49
- * @param string|array $key The key of the state value to update, or an array of key-value pairs to update multiple values.
50
- * @param mixed|null $value The value to update the state with, ignored if $key is an array.
57
+ * @param string $key The key of the state value to set.
58
+ * @param mixed $value The value to set.
51
59
  */
52
- public function setState($key, $value = null): void
60
+ public function setState(string $key, mixed $value = null): void
53
61
  {
54
- $update = is_array($key) ? $key : [$key => $value];
55
- $this->state = array_merge($this->state, $update);
62
+ $this->state[$key] = $value;
63
+
56
64
  $this->notifyListeners();
57
65
  $this->saveState();
58
66
  }
@@ -66,10 +74,8 @@ class StateManager
66
74
  public function subscribe(callable $listener): callable
67
75
  {
68
76
  $this->listeners[] = $listener;
69
- $listener($this->state); // Immediate call with current state
70
- return function () use ($listener) {
71
- $this->listeners = array_filter($this->listeners, fn ($l) => $l !== $listener);
72
- };
77
+ $listener($this->state);
78
+ return fn () => $this->listeners = array_filter($this->listeners, fn ($l) => $l !== $listener);
73
79
  }
74
80
 
75
81
  /**
@@ -77,7 +83,7 @@ class StateManager
77
83
  */
78
84
  private function saveState(): void
79
85
  {
80
- $_SESSION[self::APP_STATE] = json_encode($this->state);
86
+ $_SESSION[self::APP_STATE] = json_encode($this->state, JSON_THROW_ON_ERROR);
81
87
  }
82
88
 
83
89
  /**
@@ -86,8 +92,8 @@ class StateManager
86
92
  private function loadState(): void
87
93
  {
88
94
  if (isset($_SESSION[self::APP_STATE])) {
89
- $loadedState = json_decode($_SESSION[self::APP_STATE], true);
90
- if ($loadedState !== null) {
95
+ $loadedState = json_decode($_SESSION[self::APP_STATE], true, 512, JSON_THROW_ON_ERROR);
96
+ if (is_array($loadedState)) {
91
97
  $this->state = $loadedState;
92
98
  $this->notifyListeners();
93
99
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-prisma-php-app",
3
- "version": "1.15.6",
3
+ "version": "1.15.7",
4
4
  "description": "Prisma-PHP: A Revolutionary Library Bridging PHP with Prisma ORM",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",