forked from brainfoolong/gpio-webinterface
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData.php
More file actions
110 lines (105 loc) · 2.79 KB
/
Copy pathData.php
File metadata and controls
110 lines (105 loc) · 2.79 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
<?php
namespace Nullix\Gpiowebinterface;
/**
* Simple json data storage
*
* @package Nullix\Gpiowebinterface
*/
class Data
{
/**
* Cache for the files
*
* @var array
*/
private static $cache;
/**
* Read a data file from disk and get a specific key from the values array
*
* @param string $file
* @param string $key
*
* @return mixed
*/
public static function getKey($file, $key)
{
$data = self::get($file);
if (is_array($data) && isset($data[$key])) {
return $data[$key];
}
}
/**
* Set a specific data value for the given key
*
* @param string $file
* @param string $key
* @param mixed $value
*/
public static function setKey($file, $key, $value)
{
$data = self::get($file);
$data[$key] = $value;
self::set($file, $data);
}
/**
* Read a data file from disk
*
* @param string $file
*
* @return mixed
*/
public static function get($file)
{
if (isset(self::$cache[$file])) {
return self::$cache[$file];
}
$path = __DIR__ . "/../data/$file.json";
if (!file_exists($path) || !is_readable($path)) {
return null;
}
return json_decode(file_get_contents($path), true);
}
/**
* Write a data file to disk
*
* @param string $file
* @param mixed $value Any value to store in the file
*
* @throws \Exception
*/
public static function set($file, $value)
{
$path = __DIR__ . "/../data/$file.json";
if (!is_dir(dirname($path)) || !is_writable(dirname($path))) {
throw new \Exception("'data' directory does not exist or is not writeable");
}
if (file_exists($path) && !is_writable($path)) {
throw new \Exception("'$path' file is not writeable");
}
// delete file if null is given
if ($value === null) {
unset(self::$cache[$file]);
if (file_exists($path)) {
unlink($path);
}
}
// if a mutex file exist (other write progress not finished) than just wait a second
// every write progress should not last longer than 1 second
// we do not save such huge files to disk
$mutex = $path . ".mutex";
if (file_exists($mutex) && filemtime($mutex) > time() - 1) {
sleep(1);
// delete mutex file anyway
unlink($mutex);
}
$json = json_encode($value);
// create mutex
file_put_contents($mutex, "");
// write file
file_put_contents($path, $json);
// delete mutex
unlink($mutex);
// set cache
self::$cache[$file] = $value;
}
}