|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Jasny\Session; |
|
6
|
|
|
|
|
7
|
|
|
use Jasny\Session\Flash\FlashBag; |
|
8
|
|
|
use Jasny\Session\Flash\FlashTrait; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Session that only exists in local memory. |
|
12
|
|
|
* |
|
13
|
|
|
* @extends \ArrayObject<string,mixed> |
|
14
|
|
|
*/ |
|
15
|
|
|
class MockSession extends \ArrayObject implements SessionInterface |
|
16
|
|
|
{ |
|
17
|
|
|
use FlashTrait; |
|
18
|
|
|
|
|
19
|
|
|
/** @var array<string,mixed> */ |
|
20
|
|
|
protected array $initialData; |
|
21
|
|
|
protected int $status = \PHP_SESSION_ACTIVE; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* MockSession constructor. |
|
25
|
|
|
* |
|
26
|
|
|
* @param array<string,mixed> $input |
|
27
|
|
|
* @param FlashBag|null $flashBag |
|
28
|
|
|
*/ |
|
29
|
7 |
|
public function __construct(array $input = [], ?FlashBag $flashBag = null) |
|
30
|
|
|
{ |
|
31
|
7 |
|
$this->initialData = $input; |
|
32
|
7 |
|
$this->flashBag = $flashBag ?? new FlashBag(); |
|
33
|
|
|
|
|
34
|
7 |
|
parent::__construct($input); |
|
35
|
7 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @inheritDoc |
|
39
|
|
|
*/ |
|
40
|
5 |
|
public function status(): int |
|
41
|
|
|
{ |
|
42
|
5 |
|
return $this->status; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @inheritDoc |
|
47
|
|
|
*/ |
|
48
|
3 |
|
public function start(): void |
|
49
|
|
|
{ |
|
50
|
3 |
|
$this->status = \PHP_SESSION_ACTIVE; |
|
51
|
3 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @inheritDoc |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public function stop(): void |
|
57
|
|
|
{ |
|
58
|
2 |
|
$this->initialData = $this->getArrayCopy(); |
|
59
|
2 |
|
$this->status = \PHP_SESSION_NONE; |
|
60
|
2 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @inheritDoc |
|
64
|
|
|
*/ |
|
65
|
2 |
|
public function abort(): void |
|
66
|
|
|
{ |
|
67
|
2 |
|
$this->exchangeArray($this->initialData); |
|
68
|
2 |
|
$this->status = \PHP_SESSION_NONE; |
|
69
|
2 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @inheritDoc |
|
73
|
|
|
*/ |
|
74
|
1 |
|
public function clear(): void |
|
75
|
|
|
{ |
|
76
|
1 |
|
$this->exchangeArray([]); |
|
77
|
1 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @inheritDoc |
|
81
|
|
|
*/ |
|
82
|
1 |
|
public function kill(): void |
|
83
|
|
|
{ |
|
84
|
1 |
|
$this->initialData = []; |
|
85
|
1 |
|
$this->exchangeArray([]); |
|
86
|
1 |
|
$this->status = \PHP_SESSION_NONE; |
|
87
|
1 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @inheritDoc |
|
91
|
|
|
*/ |
|
92
|
1 |
|
public function rotate(?callable $copy = null): void |
|
93
|
|
|
{ |
|
94
|
1 |
|
$data = isset($copy) ? $copy($this->getArrayCopy()) : []; |
|
95
|
|
|
|
|
96
|
1 |
|
$this->initialData = []; |
|
97
|
1 |
|
$this->exchangeArray($data); |
|
98
|
1 |
|
$this->status = \PHP_SESSION_ACTIVE; |
|
99
|
1 |
|
} |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param string $offset |
|
104
|
|
|
*/ |
|
105
|
1 |
|
public function offsetUnset($offset): void |
|
106
|
|
|
{ |
|
107
|
1 |
|
if (parent::offsetExists($offset)) { |
|
108
|
1 |
|
parent::offsetUnset($offset); |
|
109
|
|
|
} |
|
110
|
1 |
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|