Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/Server/Session/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ class Session implements SessionInterface
*
* @var array<string, mixed>
*/
private array $data;
private array $data = [];

private bool $loaded = false;

public function __construct(
private SessionStoreInterface $store,
Expand Down Expand Up @@ -126,6 +128,7 @@ public function forget(string $key): void
public function clear(): void
{
$this->data = [];
$this->loaded = true;
}

public function pull(string $key, mixed $default = null): mixed
Expand All @@ -144,6 +147,7 @@ public function all(): array
public function hydrate(array $attributes): void
{
$this->data = $attributes;
$this->loaded = true;
}

/** @return array<string, mixed> */
Expand All @@ -157,20 +161,22 @@ public function jsonSerialize(): array
*/
private function readData(): array
{
if (isset($this->data)) {
if ($this->loaded) {
return $this->data;
}

$this->loaded = true;

$rawData = $this->store->read($this->id);

if (false === $rawData) {
return $this->data = [];
return $this->data;
}

$decoded = json_decode($rawData, true, flags: \JSON_THROW_ON_ERROR);

if (!\is_array($decoded)) {
return $this->data = [];
return $this->data;
}

return $this->data = $decoded;
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Server/Session/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ public function testSaveBeforeReadInitializesData()

// save() before any get()/set() should not crash
$this->assertTrue($session->save());
$this->assertSame([], $session->all());
}

public function testAllReturnsEmptyArrayForNullPayload()
Expand Down