-
Notifications
You must be signed in to change notification settings - Fork 0
/
Registry.php
executable file
·219 lines (194 loc) · 5.93 KB
/
Registry.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php //#SCOPE_OS_PUBLIC
/*******************************************************************************
#LIC_FULL
@author Troy Hurteau <jthurtea@ncsu.edu>
Utility class for storing and retrieving globally accessible state
*******************************************************************************/
class Saf_Registry {
protected static $_singleton = NULL;
protected static $_configuration = NULL;
private static $_unavailableExceptionMessage = 'Requested registry value is not available.';
private static $_setExceptionTemplate = 'Unable to edit values in the {{facet}} facet.';
private static $_unwritableExceptionMessage = 'Requested registry value could not be edited.';
protected static $_writableFacets = array('root','response','session','auth');
protected static $_defaultSessionKeepers = array('debug');
protected static $_wippedValues = array();
const FACET_ROOT = 'root';
const FACET_RESPONSE = 'response';
const FACET_GET = 'get';
const FACET_POST = 'post';
const FACET_REQUEST = 'request';
const FACET_SESSION = 'session';
const FACET_AUTH = 'auth';
protected function __construct()
{
self::$_configuration = array(
self::FACET_ROOT => array(),
'response' => array(),
'get' => $_GET,
'post' => $_POST,
'request' => $_REQUEST,
'session' => &$_SESSION,
'auth' => array()
);
}
private function __clone()
{
}
private function __wakeup()
{
}
protected static function _init()
{
if (is_null(self::$_singleton)) {
self::$_singleton = new Saf_Registry();
}
}
/**
* retrive stored value
* @param string $name
* @throws Exception when not available
* @return mixed stored value
*/
public static function get($name = NULL)
{
self::_init();
if (is_null($name)) {
return self::$_singleton;
}
$request =
is_array($name)
? $name
: explode(':', $name);
$facet = array_shift($request);
if ('config' == $facet) {
return self::_get(array('root','config'), self::$_configuration); //#TODO #1.0.0 Saf_Config is not currently setup as a singleton...
} else {
try{
if(array_key_exists($facet, self::$_configuration)){
return self::_get($request, self::$_configuration[$facet]);
} else {
array_unshift($request, $facet);
return self::_get($request, self::$_configuration['root']);
}
} catch (Exception $e){
$stringName = implode(':', $request);
throw new Exception(self::$_unavailableExceptionMessage . (Saf_Debug::isEnabled() ? "({$stringName})" : ''));
}
}
}
protected static function _get($name, $source)
{
if (0 == count($name)) {
return $source;
}
$newSourceName = array_shift($name);
if (is_array($source)
&& array_key_exists($newSourceName, $source)
){
return self::_get($name, $source[$newSourceName]);
} else if (is_object($source)
&& property_exists($source, $newSourceName)
) {
return self::_get($name, $source->$newSourceName);
}
$stringName = implode(':', $name);
throw new Exception(self::$_unavailableExceptionMessage . (Saf_Debug::isEnabled() ? "({$stringName})" : ''));
}
public function __get($name)
{
return self::_get($name, self::$_configuration[self::FACET_ROOT]);
}
public function __set($name, $value)
{
return self::_set($name, self::$_configuration[self::FACET_ROOT], $value);
}
/**
* store value
* @param string $name
* @param mixed $value
* @throws Exception invalid storage option
*/
public static function set($name, $value)
{
self::_init();
$request =
is_array($name)
? $name
: explode(':', $name);
$facet = array_shift($request);
if(
(array_key_exists($facet, self::$_configuration)
&& !in_array($facet, self::$_writableFacets)
) || (!array_key_exists($facet, self::$_configuration)
&& !in_array('root', self::$_writableFacets)
)
) {
throw new Exception(str_replace('{{facet}}', $facet, self::$_setExceptionTemplate));
}
if(array_key_exists($facet, self::$_configuration)){
return self::_set($request, self::$_configuration[$facet], $value);
} else {
array_unshift($request, $facet);
return self::_set($request, self::$_configuration['root'], $value);
}
}
protected static function _set($name, &$source, $value)
{
if (0 == count($name)) {
$source = $value;
return;
}
$newSourceName = array_shift($name);
if (is_array($source)) {
return self::_set($name, $source[$newSourceName], $value);
} else if (is_object($source)
&& property_exists($source, $newSourceName)
) {
return self::_set($name, $source->$newSourceName, $value);
}
throw new Exception(self::$_unwritableExceptionMessage);
}
public function __isset($name)
{
return array_key_exists($name, self::$_configuration[self::FACET_ROOT]);
}
public function __unset($name)
{
if(array_key_exists($name, self::$_configuration[self::FACET_ROOT])) {
if (array_key_exists($name, self::$_wipedValues)) {
self::$_wipedValues[$name]++;
} else {
self::$_wipedValues[$name] = 1;
}
unset(self::$_configuration[self::FACET_ROOT]);
}
}
/**
* clears all but select values from the session
* @param array $keepers values to preserve
*/
public static function cleanSession($keepers = NULL)
{
if (is_null($keepers)) {
$keepers = self::$_defaultSessionKeepers;
}
if ('' == session_id()) {
session_start();
}
$keeperValues = array();
foreach($keepers as $keeperName){
if (array_key_exists($keeperName, $_SESSION)) {
$keeperValues[$keeperName] = $_SESSION[$keeperName];
}
}
session_unset();
foreach($keeperValues as $keeperName=>$keeperValue){
$_SESSION[$keeperName] = $keeperValue;
}
}
public static function isRegistered($name)
{
return self::$_singleton ? self::$_singleton->__isset($name) : FALSE;
}
}