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