-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathSessionStorage.php
86 lines (65 loc) · 1.31 KB
/
SessionStorage.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
<?php
namespace Milo\Github\Storages;
use Milo\Github;
/**
* Session storage which uses $_SESSION directly. Session must be started already before use.
*
* @author Miloslav Hůla (https://github.com/milo)
*/
class SessionStorage extends Github\Sanity implements ISessionStorage
{
const SESSION_KEY = 'milo.github-api';
/** @var string */
private $sessionKey;
/**
* @param string
*/
public function __construct($sessionKey = self::SESSION_KEY)
{
$this->sessionKey = $sessionKey;
}
/**
* @param string
* @param mixed
* @return self
*/
public function set($name, $value)
{
if ($value === NULL) {
return $this->remove($name);
}
$this->check(__METHOD__);
$_SESSION[$this->sessionKey][$name] = $value;
return $this;
}
/**
* @param string
* @return mixed
*/
public function get($name)
{
$this->check(__METHOD__);
return isset($_SESSION[$this->sessionKey][$name])
? $_SESSION[$this->sessionKey][$name]
: NULL;
}
/**
* @param string
* @return self
*/
public function remove($name)
{
$this->check(__METHOD__);
unset($_SESSION[$this->sessionKey][$name]);
return $this;
}
/**
* @param string
*/
private function check($method)
{
if (!isset($_SESSION)) {
trigger_error("Start session before using $method().", E_USER_WARNING);
}
}
}