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