forked from nylen/rtgui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPersistentObject.php
More file actions
32 lines (29 loc) · 798 Bytes
/
PersistentObject.php
File metadata and controls
32 lines (29 loc) · 798 Bytes
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
<?php
class PersistentObject {
private $filename = false;
public $data = array();
public $mode = 0644;
public function __construct($filename=null) {
$this->load($filename);
}
public function save($filename=null) {
if($filename !== null) {
$this->filename = $filename;
}
$tmp = tempnam(sys_get_temp_dir(), 'PO');
if(file_put_contents($tmp, json_encode($this->data))) {
return (rename($tmp, $this->filename) && chmod($this->filename, $this->mode));
} else {
return false;
}
}
public function load($filename=null) {
if($filename !== null) {
$this->filename = $filename;
}
if(($contents = @file_get_contents($filename)) !== false) {
$this->data = json_decode($contents, true);
}
return !!$contents;
}
}