1 | <?php |
||
21 | class SessionManager |
||
22 | { |
||
23 | /** @var string */ |
||
24 | private $namespace = '_webhemi'; |
||
25 | /** @var string */ |
||
26 | private $cookiePrefix = 'atsn'; |
||
27 | /** @var string */ |
||
28 | private $sessionNameSalt = 'WebHemi'; |
||
29 | /** @var array */ |
||
30 | private $readonly = []; |
||
31 | /** @var array */ |
||
32 | private $data = []; |
||
33 | |||
34 | /** |
||
35 | * SessionManager constructor. |
||
36 | */ |
||
37 | public function __construct() |
||
48 | |||
49 | /** |
||
50 | * Saves data back to session. |
||
51 | */ |
||
52 | public function __destruct() |
||
57 | |||
58 | /** |
||
59 | * Check whether the session has already been started. |
||
60 | * |
||
61 | * @return bool |
||
62 | */ |
||
63 | private function sessionStarted() |
||
67 | |||
68 | /** |
||
69 | * Starts a session. |
||
70 | * |
||
71 | * @param string $name |
||
72 | * @param int $timeOut |
||
73 | * @param string $path |
||
74 | * @param string $domain |
||
75 | * @param bool $secure |
||
76 | * @param bool $httpOnly |
||
77 | * @return SessionManager |
||
78 | */ |
||
79 | public function start($name, $timeOut = 3600, $path = '/', $domain = null, $secure = false, $httpOnly = false) |
||
95 | |||
96 | /** |
||
97 | * Regenerates session identifier. |
||
98 | * |
||
99 | * @return SessionManager |
||
100 | */ |
||
101 | public function regenerateId() |
||
116 | |||
117 | /** |
||
118 | * Sets session data. |
||
119 | * |
||
120 | * @param string $name |
||
121 | * @param mixed $value |
||
122 | * @param bool $readonly |
||
123 | * @throws RuntimeException |
||
124 | * @return SessionManager |
||
125 | */ |
||
126 | public function set($name, $value, $readonly = false) |
||
144 | |||
145 | /** |
||
146 | * Deletes session data. |
||
147 | * |
||
148 | * @param string $name |
||
149 | * @param bool $forceDelete |
||
150 | * @throws RuntimeException |
||
151 | * @return SessionManager |
||
152 | */ |
||
153 | public function delete($name, $forceDelete = false) |
||
169 | |||
170 | /** |
||
171 | * Unlocks readonly data. |
||
172 | * |
||
173 | * @param string $name |
||
174 | * @return SessionManager |
||
175 | */ |
||
176 | public function unlock($name) |
||
184 | |||
185 | /** |
||
186 | * Returns the internal storage. |
||
187 | * |
||
188 | * @return array |
||
189 | */ |
||
190 | public function toArray() |
||
194 | } |
||
195 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: