-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSdb.php
More file actions
395 lines (366 loc) · 10.7 KB
/
Copy pathSdb.php
File metadata and controls
395 lines (366 loc) · 10.7 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
const SDB_READ_ARRAY = 1;
const SDB_READ_OBJ = 2;
const SDB_MAP_PHP = 1;
const SDB_MAP_JSON = 2;
/**
* smskdb - A New Flat Json Database
*
* Simply it reads through input file and outputs php objects/arrays or
* values directly.
* You don't need to know any javascript, json or sql.
* Soon with query engine.
*
* Copyright smskSoft, mtnsmsk, devsimsek, Metin Şimşek.
* @package Sdb
* @subpackage Sdb
* @file Sdb.php
* @version v1.0
* @author devsimsek
* @copyright Copyright (c) 2021, smskSoft, mtnsmsk
* @license https://opensource.org/licenses/MIT MIT License
* @link @no_link_specified
* @since Version 1.0
* @filesource
*/
class Sdb
{
/**
* Debug Mode
* @var bool|mixed $debugMode
* @since 1.0
*/
protected $debugMode = false;
/**
* Stores directory
* @var string $directory
* @since 1.0
*/
protected $directory;
/**
* @var string $dbFile
* @since 1.0
*/
protected $dbFile;
/**
* Temporary Array
* @var array $tempArray
* @since 1.0
*/
protected $tempArray;
/**
* smskdb - A New Flat Json Database
* Simply it reads through input file and outputs php objects/arrays or
* values directly.
* You don't need to know any javascript, json or sql.
* @param $directory
* @param false $debugMode
* @since 1.0
*/
public function __construct($directory, bool $debugMode = false)
{
$this->debugMode = $debugMode;
$this->debug("Library initialized successfully, Thank you for using smskdb on your project.", "INITIALIZATION");
if (empty($directory))
$directory = __DIR__;
if (substr($directory, -1) != "/")
$directory .= "/";
$this->directory = (string)$directory;
$this->debug("Active Directory: " . $this->directory);
}
/**
* Chmod File
* @param $file
* @param false $mode
* @return bool
* @since 1.0
*/
protected function chmod($file, bool $mode = false): bool
{
if (!$mode)
$mode = 0644;
return @chmod($file, $mode);
}
/**
* Check Database Exists And Validate Format Of It
* @param $filename
* @return bool
* @since 1.0
*/
protected function check($filename): bool
{
return file_exists($filename);
}
/**
* Load Database file
* @param string $file <p>
* The <i>database file name</i> to read.
* </p>
* @param int $mode [optional] <p>
* Bitmask of Read Options:<br/>
* {@see SDB_READ_ARRAY} returns decoded json as array.<br/>
* {@see SDB_READ_OBJ} returns decoded json as object.<br/>
* </p>
* @return mixed <p>
* Returns Array, Object or Null
* </p>
* @uses $directory
* @since 1.0
*/
public function load(string $file, int $mode = SDB_READ_ARRAY)
{
$file = $this->directory . $file;
$this->dbFile = $file;
if (!$this->check($file)) {
$this->debug("Database " . $file . " does not exists.");
return null;
}
$fileData = file_get_contents($file);
if (!empty($fileData)) {
switch ($mode) {
// Case array
case SDB_READ_ARRAY:
$array = json_decode($fileData, true);
if (!empty($array) or is_array($array)) {
$this->tempArray = $array;
return $array;
} else {
print_r($array);
$this->debug("Error, Database Is Not Formatted Correctly!", "ERROR", true);
return false;
}
// Case Object
case SDB_READ_OBJ:
default:
$array = json_decode($fileData, true);
if (!empty($array) or is_array($array)) {
$this->tempArray = $array;
return json_decode($fileData);
} else {
$this->debug("Error, Database Is Not Formatted Correctly!", "ERROR", true);
return false;
}
}
} else {
$this->debug("Error, FileData Is Empty!");
return false;
}
}
/**
* Fetch From Array
* @param string $field
* @param int $mode [optional] <p>
* Bitmask of Fetch Options:<br/>
* {@see SDB_READ_ARRAY} returns decoded json as array.<br/>
* {@see SDB_READ_OBJ} returns decoded json as object.<br/>
* </p>
* @return mixed|null
* @uses $tempArray
* @since 1.0
*/
public function fetch(string $field, int $mode = SDB_READ_ARRAY)
{
if (!empty($this->tempArray)) {
switch ($mode) {
case SDB_READ_OBJ:
return json_decode(json_encode($this->tempArray[$field], true));
case SDB_READ_ARRAY:
return json_decode(json_encode($this->tempArray[$field], false), true);
}
}
return null;
}
/**
* Set Database Field
* @warning <p>
* This function does not save to database file. This will store temporarily array in $tempArray</p>
* @param string $key <p>
* The json field parameter name that will be applied to the tempJson array.</p>
* @param mixed $value <p>
* Value could be array or string.</p>
* @return bool
* @uses $tempArray
* @since 1.0
*/
public function set(string $key, $value): bool
{
if (!empty($this->tempArray)) {
if (isset($value)) {
$this->tempArray[$key] = $value;
if ($this->tempArray[$key] === $value) {
return true;
}
}
return false;
}
return false;
}
/**
* Does Database Have Field?
* @param string $parameter <p>
* The parameter that will be searched on database
* </p>
* @return bool <p>
* Return's true when database has the field.
* </p>
* @uses $tempArray
* @since 1.0
*/
public function has(string $parameter): bool
{
if (!empty($this->tempArray)) {
if (isset($this->tempArray[$parameter])) {
return true;
}
}
return false;
}
/**
* Push array to tempArray array.
* @warning <p>
* This function does not save to database file.<br>
* This will store temporarily array in $tempArray</p>
* @param mixed $parameter
* @return bool
* @uses $tempArray
* @since 1.0
*/
public function push($parameter): bool
{
if (!empty($this->tempArray)) {
if (array_push($this->tempArray, $parameter)) {
return true;
}
return false;
}
return false;
}
/**
* Map Database
* @param int $mode [optional] <p>
* Bitmask of Fetch Options:<br/>
* {@see SDB_MAP_PHP} returns php array.<br/>
* {@see SDB_MAP_JSON} returns decoded json.<br/>
* </p>
* @return array|false|string
* @uses $tempArray
* @since 1.0
*/
public function map(int $mode = SDB_MAP_PHP)
{
switch ($mode) {
case SDB_MAP_JSON:
return json_encode($this->tempArray);
case SDB_MAP_PHP:
return $this->tempArray;
}
return false;
}
/**
* Remove Key From Database
* @param mixed $key <p>
* May be string or integer.</p>
* @return bool
* @uses $tempArray
* @since 1.0
*/
public function remove($key): bool
{
if (!empty($this->tempArray)) {
if (!empty($this->tempArray[$key])) {
unset($this->tempArray[$key]);
if ($this->has($key)) {
return false;
}
return true;
}
}
return false;
}
/**
* Save temporary data to database file
* @return bool
* @uses $tempArray
* @since 1.0
*/
public function save(): bool
{
if (!empty($this->tempArray)) {
if (file_exists($this->dbFile) && file_get_contents($this->dbFile) == json_encode($this->tempArray)) {
touch($this->dbFile);
}
if (!$fp = @fopen($this->dbFile, 'wb')) {
return false;
}
fwrite($fp, json_encode($this->tempArray));
fclose($fp);
$this->chmod($this->dbFile);
return true;
}
return false;
}
/**
* Create Empty Database
* @param string $file
* @return bool
* @uses $directory
* @since 1.0
*/
public function create(string $file): bool
{
$file = $this->directory . $file;
if (!$fp = @fopen($file, 'wb')) {
return false;
}
fwrite($fp, json_encode(array("sdb" => array(
"createdAt" => date("Y/m/d"),
"path" => $this->directory,
"status" => "created_successfully"
))));
fclose($fp);
$this->chmod($this->dbFile);
return true;
}
/**
* Delete Database File
* @param string $filename
* @return bool
* @uses $tempArray
* @since 1.0
*/
public function delete(string $filename): bool
{
$file = $this->directory . $filename;
if (!$this->check($file)) {
$this->debug("Database " . $file . " does not exists.", "WARNING", true);
return false;
}
return unlink($this->directory . $filename);
}
/**
* Debug
* Sdb has a pretty nice debugging function which helps developer to
* debug their application.
*
* @param string $message
* @param string $flag
* @param bool $isError
* @return int
* @since 1.0
*/
protected function debug(string $message, string $flag = "INFO", bool $isError = false): int
{
if ($this->debugMode) {
if ($isError) {
try {
throw new Exception("\e[42m[" . date("Y-m-d") . " SDB DEBUG \e[31mFATAL ERROR!\e[0m\e[42m]\e[0m: (" . $flag . ") " . $message);
} catch (Exception $exception) {
error_log("\e[42m[" . date("Y-m-d") . " SDB DEBUG \e[42;31mFATAL ERROR!\e[0m\e[42m]\e[0m: (" . $flag . ") " . $message . PHP_EOL . "ERROR: Can't Throw Exception, " . PHP_EOL . $exception->getMessage());
}
} else {
error_log("\e[42m[" . date("Y-m-d") . " SDB DEBUG]\e[0m: (" . $flag . ") " . $message);
}
}
return 1;
}
}