1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
7
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
8
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
9
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
10
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
11
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
12
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
13
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
14
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
15
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
16
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
17
|
|
|
* |
18
|
|
|
* This software consists of voluntary contributions made by many individuals |
19
|
|
|
* and is licensed under the MIT license. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace SanSessionToolbar\Manager; |
23
|
|
|
|
24
|
|
|
use Laminas\Session\Container; |
25
|
|
|
use Laminas\Stdlib\ArrayObject; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* A class to manage session data. |
29
|
|
|
* |
30
|
|
|
* @author Abdul Malik Ikhsan <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
final class SessionManager implements SessionManagerInterface |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
15 |
|
public function getSessionData(bool $checkExists = true): ?array |
38
|
|
|
{ |
39
|
15 |
|
if ($checkExists) { |
40
|
13 |
|
$manager = Container::getDefaultManager(); |
41
|
13 |
|
if (!$manager->sessionExists()) { |
42
|
1 |
|
return null; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
14 |
|
$container = new Container(); |
47
|
14 |
|
$arraysession = $container->getManager()->getStorage()->toArray(); |
48
|
|
|
|
49
|
14 |
|
return $this->collectSessionData($arraysession); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
14 |
|
private function collectSessionData(array $arraysession): array |
56
|
|
|
{ |
57
|
14 |
|
$data = []; |
58
|
|
|
|
59
|
14 |
|
foreach ($arraysession as $key => $row) { |
60
|
11 |
|
if ($row instanceof ArrayObject) { |
61
|
11 |
|
$iterator = $row->getIterator(); |
62
|
11 |
|
while ($iterator->valid()) { |
63
|
10 |
|
$data[$key][$iterator->key()] = $iterator->current(); |
64
|
10 |
|
$iterator->next(); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
14 |
|
return $data; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
11 |
|
public function sessionSetting(string $containerName, string $keysession, string $value = null, array $options = []): bool |
76
|
|
|
{ |
77
|
11 |
|
$container = new Container($containerName); |
78
|
11 |
|
$new = $options['new'] ?? false; |
79
|
|
|
|
80
|
11 |
|
if ($new && $value) { |
81
|
4 |
|
return $this->addSession($container, $keysession, $value); |
82
|
|
|
} |
83
|
|
|
|
84
|
7 |
|
$set = $options['set'] ?? false; |
85
|
|
|
|
86
|
7 |
|
return $this->setUnset($container, $keysession, $value, $set); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Add new session data. |
91
|
|
|
*/ |
92
|
4 |
|
private function addSession(Container $container, string $keysession, string $value): bool |
93
|
|
|
{ |
94
|
4 |
|
if ($container->offsetExists($keysession)) { |
95
|
2 |
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
$container->offsetSet($keysession, $value); |
99
|
|
|
|
100
|
2 |
|
return true; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Set/Unset session data. |
105
|
|
|
*/ |
106
|
7 |
|
private function setUnset(Container $container, string $keysession, ?string $value, bool $set = false): bool |
107
|
|
|
{ |
108
|
7 |
|
if (!$container->offsetExists($keysession)) { |
109
|
3 |
|
return false; |
110
|
|
|
} |
111
|
|
|
|
112
|
4 |
|
if ($set && $value) { |
113
|
2 |
|
$container->offsetSet($keysession, $value); |
114
|
|
|
|
115
|
2 |
|
return true; |
116
|
|
|
} |
117
|
|
|
|
118
|
2 |
|
$container->offsetUnset($keysession); |
119
|
|
|
|
120
|
2 |
|
return true; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
4 |
|
public function clearSession(string $byContainer = null): void |
127
|
|
|
{ |
128
|
4 |
|
(new Container())->getManager() |
129
|
4 |
|
->getStorage() |
130
|
4 |
|
->clear($byContainer); |
131
|
4 |
|
} |
132
|
|
|
} |
133
|
|
|
|