-
Notifications
You must be signed in to change notification settings - Fork 2
/
simple_storage.class.php
194 lines (160 loc) · 4.38 KB
/
simple_storage.class.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
<?php
/**
* SIMPLE FLAT FILE STORAGE CLASS
*
* @version 1.1
* @author Matthew Colf <[email protected]>
*
* @section LICENSE
*
* Copyright 2011 Matthew Colf <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class SimpleStorage {
protected $file;
protected $dirty = FALSE;
protected $domain = "default";
protected $data = array(
"meta" => array(
"updated" => "",
"checksum" => ""
),
"domains" => array(
"default" => array(
"foo" => "bar"
)
)
);
public function __construct($domain = null, $file = null)
{
// determine which file to use
if ($file == null)
$this->file = __DIR__ . '/storage.json';
else
$this->file = $file;
// check if file is writeable
if (!is_writable($this->file))
throw new Exception("File is not writeable.");
// load data
$json = file_get_contents($this->file);
if ( strlen($json) == 0 )
return;
if ( ($this->data = json_decode($json,$assoc = TRUE)) === NULL )
throw new Exception("Unable to decode $this->file.");
// verify data integrity
if ( $this->data["meta"]["checksum"] != md5($this->data["domains"]) )
throw new Exception("Data from $this->file is not valid. Fails checksum.");
// import data
if ( ($this->data["domains"] = json_decode($this->data["domains"],$assoc = TRUE)) === NULL )
throw new Exception("Unable to unserialize data from $file.");
// setup domain
if ( $domain )
{
if ( $this->domain_exists($domain) == FALSE ) $this->domain_add($domain);
$this->domain = $domain;
}
}
public function __destruct()
{
$this->flush();
}
public function flush()
{
// check if writeback is needed
if ( $this->dirty == FALSE ) return TRUE;
// prepare to writeback to file
$data = $this->data;
$data["domains"] = json_encode($this->data["domains"]);
$data["meta"]["updated"] = date("c");
$data["meta"]["checksum"] = md5($data["domains"]);
// overwrite existing data
if ( file_put_contents($this->file,json_encode($data)) )
return TRUE;
else
throw new Exception("Unable to write back to $this->file. Data will be lost.");
}
// save data
public function put($key,$data,$domain = NULL)
{
if ( $domain == NULL ) $domain = $this->domain;
if ( is_string($key.$domain) AND $this->domain_exists($domain) )
{
$this->data["domains"][$domain][$key] = $data;
$this->dirty = TRUE;
return TRUE;
}
return FALSE;
}
// check if data exists
public function exists($key,$domain = NULL)
{
if ( $domain == NULL ) $domain = $this->domain;
if ( is_string($key.$domain) AND $this->domain_exists($domain) )
{
return array_key_exists($key,$this->data["domains"][$domain]);
}
return false;
}
// retrieve data
public function get($key,$domain = NULL)
{
if ( $domain == NULL ) $domain = $this->domain;
if ( is_string($key.$domain) AND $this->domain_exists($domain) )
{
if ( array_key_exists($key,$this->data["domains"][$domain]) )
{
return $this->data["domains"][$domain][$key];
}
}
return NULL;
}
// delete data
public function remove($key,$domain = NULL)
{
if ( $domain == NULL ) $domain = $this->domain;
if ( is_string($key.$domain) AND $this->domain_exists($domain) )
{
if ( array_key_exists($key,$this->data["domains"][$domain]) )
{
unset($this->data["domains"][$domain][$key]);
$this->dirty = TRUE;
return TRUE;
}
}
return NULL;
}
// check if a domain exists
public function domain_exists($domain)
{
return array_key_exists($domain,$this->data["domains"]);
}
// add a new domain
public function domain_add($domain)
{
if ( $this->domain_exists($domain) ) return FALSE;
$this->data["domains"][$domain] = array();
$this->dirty = TRUE;
}
// remove an existing domain and all associated data
public function domain_remove($domain)
{
if ( $this->domain_exists($domain) )
{
unset($this->data["domains"][$domain]);
$this->dirty = TRUE;
return TRUE;
}
return FALSE;
}
}