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