-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathSession.php
executable file
·156 lines (132 loc) · 3.71 KB
/
Session.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* Webiny Framework (http://www.webiny.com/framework)
*
* @copyright Copyright Webiny LTD
*/
namespace Webiny\Component\Http;
use Webiny\Component\Config\ConfigObject;
use Webiny\Component\Http\Session\SessionException;
use Webiny\Component\Http\Session\Storage\NativeStorage;
use Webiny\Component\StdLib\SingletonTrait;
use Webiny\Component\StdLib\StdLibTrait;
use Webiny\Component\StdLib\StdObject\ArrayObject\ArrayObject;
/**
* Session Http component.
*
* @package Webiny\Component\Http
*/
class Session
{
use StdLibTrait, SingletonTrait;
/**
* @var ArrayObject
*/
private $sessionBag;
/**
* @var string
*/
private $sessionId;
/**
* Constructor.
*
* @throws Session\SessionException
*/
protected function init()
{
$config = self::getConfig();
// validate that headers have not already been sent
if (headers_sent()) {
throw new SessionException('Unable to register session handler because headers have already been sent.');
}
// remove any shut down functions
session_register_shutdown();
// get the driver
$saveHandler = $config->get('Storage.Driver', NativeStorage::class);
try {
// try to create driver instance
$saveHandler = new $saveHandler($config);
// register driver as session handler
session_set_save_handler($saveHandler, false);
// start the session
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
// get session id
$this->sessionId = session_id();
// save current session locally
$this->sessionBag = $this->arr($_SESSION);
} catch (\Exception $e) {
throw new SessionException($e->getMessage());
}
}
/**
* Get a session value for the given $key.
* If key doesn't not exist, $value will be returned and assigned under that key.
*
* @param string $key Key for which you wish to get the value.
* @param mixed $value Default value that will be returned if $key doesn't exist.
*
* @return string Value of the given $key.
*/
public function get($key, $value = null)
{
$return = $this->sessionBag->key($key, $value, true);
$_SESSION[$key] = $return;
return $return;
}
/**
* Save, or overwrite, a session value under the given $key with the given $value.
*
* @param string $key Key for which you wish to get the value.
* @param mixed $value Value that will be stored under the $key.
*
* @return $this
*/
public function save($key, $value)
{
$this->sessionBag->removeKey($key)->append($key, $value);
$_SESSION[$key] = $value;
return $this;
}
/**
* Removes the given $key from session.
*
* @param string $key Name of the session key you wish to remove.
*
* @return bool
*/
public function delete($key)
{
$this->sessionBag->removeKey($key);
unset($_SESSION[$key]);
return true;
}
/**
* Get current session id.
*
* @return string Session id.
*/
public function getSessionId()
{
return $this->sessionId;
}
/**
* Get all session values.
*
* @return array Key-value array of all session entries.
*/
public function getAll()
{
return $this->sessionBag->val();
}
/**
* Returns session config from Http object.
*
* @return ConfigObject
*/
public static function getConfig()
{
return Http::getConfig()->get('Session', new ConfigObject([]));
}
}