|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package midcom.services |
|
4
|
|
|
* @author The Midgard Project, http://www.midgard-project.org |
|
5
|
|
|
* @copyright The Midgard Project, http://www.midgard-project.org |
|
6
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Provides sessioning for components. |
|
11
|
|
|
* |
|
12
|
|
|
* Use get, set and exists to access the sessioning data within your component. |
|
13
|
|
|
* |
|
14
|
|
|
* <b>Example:</b> |
|
15
|
|
|
* |
|
16
|
|
|
* <code> |
|
17
|
|
|
* <?php |
|
18
|
|
|
* $session = new midcom_services_session(); |
|
19
|
|
|
* if ($session->exists("mykey")) { |
|
20
|
|
|
* echo "found session value: " . $session->get ("mykey") . ", removing it."; |
|
21
|
|
|
* $value = $session->remove("mykey"); |
|
22
|
|
|
* } else { |
|
23
|
|
|
* echo "setting session value..."; |
|
24
|
|
|
* $session->set("mykey", "hello world"); |
|
25
|
|
|
* } |
|
26
|
|
|
* </code> |
|
27
|
|
|
* |
|
28
|
|
|
* You should keep in mind, that the isolation level by default is per-component, |
|
29
|
|
|
* not per-request. If you, for example, have the same component active twice |
|
30
|
|
|
* (through dynamic_load) you have to manually ensure, that each request is |
|
31
|
|
|
* treated separately. Unfortunately MidCOM cannot help you here. |
|
32
|
|
|
* |
|
33
|
|
|
* <b>Implementation Notes:</b> |
|
34
|
|
|
* |
|
35
|
|
|
* This is a simple wrapper that provides access to the sessioning singleton. |
|
36
|
|
|
* It has the same public member functions as midcom_services__sessioning, refer |
|
37
|
|
|
* to this class for a detailed documentation. |
|
38
|
|
|
* |
|
39
|
|
|
* Basically this wrapper ensures the singleton pattern is maintained and provides |
|
40
|
|
|
* you with an easy way of lock the domain you're working in. |
|
41
|
|
|
* |
|
42
|
|
|
* @package midcom.services |
|
43
|
|
|
* @see midcom_services__sessioning |
|
44
|
|
|
*/ |
|
45
|
|
|
class midcom_services_session |
|
46
|
|
|
{ |
|
47
|
|
|
private midcom_services__sessioning $_sessioning; |
|
48
|
|
|
|
|
49
|
|
|
private ?string $_domain; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Constructs a session object. |
|
53
|
|
|
* |
|
54
|
|
|
* The default constructor will create a sessioning object within the domain |
|
55
|
|
|
* of the current context's component. This will be sufficient for almost all |
|
56
|
|
|
* actual uses of the sessions. |
|
57
|
|
|
* |
|
58
|
|
|
* If passed a string argument, this value is used as a domain. This |
|
59
|
|
|
* is useful for components that need sessioning while under dynamic_load |
|
60
|
|
|
* conditions or while used as a library. |
|
61
|
|
|
*/ |
|
62
|
94 |
|
public function __construct(?string $domain = null) |
|
63
|
|
|
{ |
|
64
|
94 |
|
$this->_domain = $domain ?? midcom_core_context::get()->get_key(MIDCOM_CONTEXT_COMPONENT); |
|
65
|
|
|
|
|
66
|
94 |
|
$this->_sessioning = midcom::get()->session; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Returns a value from the session. |
|
71
|
|
|
* |
|
72
|
|
|
* Returns null if the key |
|
73
|
|
|
* is non-existent. Note, that this is not necessarily a valid non-existence |
|
74
|
|
|
* check, as the sessioning system does allow null values. Use the exists function |
|
75
|
|
|
* if unsure. |
|
76
|
|
|
*/ |
|
77
|
|
|
public function get(string $key) |
|
78
|
|
|
{ |
|
79
|
|
|
$data = $this->_sessioning->get($this->_domain, []); |
|
|
|
|
|
|
80
|
|
|
return $data[$key] ?? null; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* This will store the value to the specified key. |
|
85
|
|
|
*/ |
|
86
|
68 |
|
public function set(string $key, $value) |
|
87
|
|
|
{ |
|
88
|
68 |
|
$data = $this->_sessioning->get($this->_domain, []); |
|
|
|
|
|
|
89
|
68 |
|
$data[$key] = $value; |
|
90
|
68 |
|
$this->_sessioning->set($this->_domain, $data); |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Checks, if the specified key has been added to the session store. |
|
95
|
|
|
* |
|
96
|
|
|
* This is often used in conjunction with get to verify a keys existence. |
|
97
|
|
|
*/ |
|
98
|
35 |
|
public function exists(string $key) : bool |
|
99
|
|
|
{ |
|
100
|
35 |
|
$data = $this->_sessioning->get($this->_domain, []); |
|
|
|
|
|
|
101
|
35 |
|
return array_key_exists($key, $data); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Removes the specified key. |
|
106
|
|
|
*/ |
|
107
|
|
|
public function remove(string $key) |
|
108
|
|
|
{ |
|
109
|
|
|
$data = $this->_sessioning->get($this->_domain, []); |
|
|
|
|
|
|
110
|
|
|
unset($data[$key]); |
|
111
|
|
|
$this->_sessioning->set($this->_domain, $data); |
|
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|