forked from parse-community/parse-php-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParseSessionStorage.php
More file actions
118 lines (107 loc) · 2.37 KB
/
Copy pathParseSessionStorage.php
File metadata and controls
118 lines (107 loc) · 2.37 KB
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
<?php
/**
* Class ParseSessionStorage | Parse/ParseSessionStorage.php
*/
namespace Parse;
/**
* Class ParseSessionStorage - Uses PHP session support for persistent storage.
*
* @author Fosco Marotto <fjm@fb.com>
* @package Parse
*/
class ParseSessionStorage implements ParseStorageInterface
{
/**
* Parse will store its values in a specific key.
*
* @var string
*/
private $storageKey = 'parseData';
/**
* ParseSessionStorage constructor.
* @throws ParseException
*/
public function __construct()
{
if (session_status() !== PHP_SESSION_ACTIVE) {
throw new ParseException(
'PHP session_start() must be called first.'
);
}
if (!isset($_SESSION[$this->storageKey])) {
$_SESSION[$this->storageKey] = [];
}
}
/**
* Sets a key-value pair in storage.
*
* @param string $key The key to set
* @param mixed $value The value to set
*
* @return void
*/
public function set($key, $value)
{
$_SESSION[$this->storageKey][$key] = $value;
}
/**
* Remove a key from storage.
*
* @param string $key The key to remove.
*
* @return void
*/
public function remove($key)
{
unset($_SESSION[$this->storageKey][$key]);
}
/**
* Gets the value for a key from storage.
*
* @param string $key The key to get the value for
*
* @return mixed
*/
public function get($key)
{
if (isset($_SESSION[$this->storageKey][$key])) {
return $_SESSION[$this->storageKey][$key];
}
return null;
}
/**
* Clear all the values in storage.
*
* @return void
*/
public function clear()
{
$_SESSION[$this->storageKey] = [];
}
/**
* Save the data, if necessary. Not implemented.
*/
public function save()
{
// No action required. PHP handles persistence for $_SESSION.
return;
}
/**
* Get all keys in storage.
*
* @return array
*/
public function getKeys()
{
return array_keys($_SESSION[$this->storageKey]);
}
/**
* Get all key-value pairs from storage.
*
* @return array
*/
public function getAll()
{
return $_SESSION[$this->storageKey];
}
}