1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace jschreuder\Middle\Session; |
4
|
|
|
|
5
|
|
|
use Laminas\Session\Container; |
6
|
|
|
use Laminas\Session\SessionManager; |
7
|
|
|
|
8
|
|
|
final class ZendSession implements SessionInterface |
9
|
|
|
{ |
10
|
|
|
const FLASH_DATA_KEY_PREFIX = '_flash_data.'; |
11
|
|
|
|
12
|
|
|
private SessionManager $sessionManager; |
13
|
|
|
private \ArrayAccess $container; |
14
|
|
|
private bool $changed = false; |
15
|
|
|
|
16
|
12 |
|
public function __construct(SessionManager $sessionManager, Container $container) |
17
|
|
|
{ |
18
|
12 |
|
$this->sessionManager = $sessionManager; |
19
|
12 |
|
$this->container = $container; |
20
|
|
|
} |
21
|
|
|
|
22
|
2 |
|
public function has(string $key): bool |
23
|
|
|
{ |
24
|
2 |
|
return isset($this->container[$key]); |
25
|
|
|
} |
26
|
|
|
|
27
|
1 |
|
public function get(string $key): mixed |
28
|
|
|
{ |
29
|
1 |
|
return $this->container[$key]; |
30
|
|
|
} |
31
|
|
|
|
32
|
1 |
|
public function set(string $key, $value): void |
33
|
|
|
{ |
34
|
1 |
|
$this->changed = true; |
35
|
1 |
|
$this->container[$key] = $value; |
36
|
|
|
} |
37
|
|
|
|
38
|
2 |
|
public function hasFlash(string $key): bool |
39
|
|
|
{ |
40
|
2 |
|
return isset($this->container[self::FLASH_DATA_KEY_PREFIX . $key]); |
41
|
|
|
} |
42
|
|
|
|
43
|
1 |
|
public function getFlash(string $key): mixed |
44
|
|
|
{ |
45
|
1 |
|
return $this->container[self::FLASH_DATA_KEY_PREFIX . $key]; |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
public function setFlash(string $key, $value): void |
49
|
|
|
{ |
50
|
1 |
|
$key = self::FLASH_DATA_KEY_PREFIX . $key; |
51
|
1 |
|
$this->changed = true; |
52
|
1 |
|
$this->container[$key] = $value; |
53
|
1 |
|
$this->container->setExpirationHops(1, [$key]); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
public function destroy(): void |
57
|
|
|
{ |
58
|
1 |
|
$this->sessionManager->destroy(); |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
public function rotateId(): void |
62
|
|
|
{ |
63
|
1 |
|
$this->changed = true; |
64
|
1 |
|
$this->sessionManager->regenerateId(); |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
public function isEmpty(): bool |
68
|
|
|
{ |
69
|
2 |
|
return $this->container->count() === 0; |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
public function hasChanged(): bool |
73
|
|
|
{ |
74
|
1 |
|
return $this->changed; |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
public function toArray(): array |
78
|
|
|
{ |
79
|
1 |
|
return $this->container->getArrayCopy(); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|