Skip to content

Commit 6fb3204

Browse files
committed
Shift away session decode from constructor
1 parent 940eb90 commit 6fb3204

1 file changed

Lines changed: 29 additions & 17 deletions

File tree

src/Server/Session/Session.php

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,27 @@
2222
class Session implements SessionInterface
2323
{
2424
/**
25-
* @param array<string, mixed> $data Stores all session data.
26-
* Keys are snake_case by convention for MCP-specific data.
27-
*
2825
* Official keys are:
2926
* - initialized: bool
3027
* - client_info: array|null
3128
* - protocol_version: string|null
3229
* - log_level: string|null
30+
*
31+
* @var array<string, mixed>
3332
*/
33+
private array $data;
34+
3435
public function __construct(
35-
protected SessionStoreInterface $store,
36-
protected Uuid $id = new UuidV4(),
37-
protected array $data = [],
36+
private SessionStoreInterface $store,
37+
private Uuid $id = new UuidV4(),
3838
) {
39-
if ($rawData = $this->store->read($this->id)) {
40-
$this->data = json_decode($rawData, true) ?? [];
41-
}
4239
}
4340

4441
public function getId(): Uuid
4542
{
4643
return $this->id;
4744
}
4845

49-
public function getStore(): SessionStoreInterface
50-
{
51-
return $this->store;
52-
}
53-
5446
public function save(): bool
5547
{
5648
return $this->store->write($this->id, json_encode($this->data, \JSON_THROW_ON_ERROR));
@@ -59,7 +51,7 @@ public function save(): bool
5951
public function get(string $key, mixed $default = null): mixed
6052
{
6153
$key = explode('.', $key);
62-
$data = $this->data;
54+
$data = $this->readData();
6355

6456
foreach ($key as $segment) {
6557
if (\is_array($data) && \array_key_exists($segment, $data)) {
@@ -75,6 +67,7 @@ public function get(string $key, mixed $default = null): mixed
7567
public function set(string $key, mixed $value, bool $overwrite = true): void
7668
{
7769
$segments = explode('.', $key);
70+
$this->readData();
7871
$data = &$this->data;
7972

8073
while (\count($segments) > 1) {
@@ -94,7 +87,7 @@ public function set(string $key, mixed $value, bool $overwrite = true): void
9487
public function has(string $key): bool
9588
{
9689
$key = explode('.', $key);
97-
$data = $this->data;
90+
$data = $this->readData();
9891

9992
foreach ($key as $segment) {
10093
if (\is_array($data) && \array_key_exists($segment, $data)) {
@@ -112,6 +105,7 @@ public function has(string $key): bool
112105
public function forget(string $key): void
113106
{
114107
$segments = explode('.', $key);
108+
$this->readData();
115109
$data = &$this->data;
116110

117111
while (\count($segments) > 1) {
@@ -143,7 +137,7 @@ public function pull(string $key, mixed $default = null): mixed
143137

144138
public function all(): array
145139
{
146-
return $this->data;
140+
return $this->readData();
147141
}
148142

149143
public function hydrate(array $attributes): void
@@ -156,4 +150,22 @@ public function jsonSerialize(): array
156150
{
157151
return $this->all();
158152
}
153+
154+
/**
155+
* @return array<string, mixed>
156+
*/
157+
private function readData(): array
158+
{
159+
if (isset($this->data)) {
160+
return $this->data;
161+
}
162+
163+
$rawData = $this->store->read($this->id);
164+
165+
if (false === $rawData) {
166+
return $this->data = [];
167+
}
168+
169+
return $this->data = json_decode($rawData, true, flags: \JSON_THROW_ON_ERROR);
170+
}
159171
}

0 commit comments

Comments
 (0)