-
Notifications
You must be signed in to change notification settings - Fork 0
/
Filesystem.php
225 lines (177 loc) · 5.15 KB
/
Filesystem.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
220
221
222
223
224
225
<?php
namespace jars;
use jars\contract\Exception;
class Filesystem
{
public const NO_PERSIST = 1 << 0;
public const READ_ONLY = 1 << 1;
public const AUTO_PERSIST = 1 << 2;
private bool $auto_persist;
private ?bool $auto_persist_backup = null;
private ?string $donefile = null;
private bool $no_persist;
private bool $read_only;
private array $store = [];
public function __construct(int $options = self::AUTO_PERSIST)
{
$this->auto_persist = (bool) ($options & static::AUTO_PERSIST);
$this->no_persist = (bool) ($options & static::NO_PERSIST);
$this->read_only = (bool) ($options & static::READ_ONLY);
}
public function __destruct()
{
if ($this->auto_persist) {
$this->persist();
}
}
public function __clone()
{
$store = [];
foreach (array_keys($this->store) as $file) {
$store[$file] = clone $this->store[$file];
}
$this->store = $store;
}
public function donefile(?string $donefile = null): null|string|self
{
if (func_num_args()) {
$this->donefile = $donefile;
return $this;
}
return $this->donefile;
}
public function put(string $file, $content)
{
if ($this->read_only) {
throw new Exception('Attempt made to modify to read-only filesystem');
}
$this->store[$file] = (object) [
'content' => $content,
'dirty' => true,
];
}
public function has(string $file)
{
if (array_key_exists($file, $this->store)) {
return $this->store[$file]->content !== null;
}
return is_file($file);
}
public function delete(string $file)
{
if ($this->read_only) {
throw new Exception('Attempt made to modify to read-only filesystem');
}
$this->store[$file] = (object) [
'content' => null,
'dirty' => true,
];
}
public function cached(string $file)
{
return array_key_exists($file, $this->store);
}
public function get(string $file, string $default = null)
{
if (!array_key_exists($file, $this->store)) {
$this->load($file, $default);
}
return $this->store[$file]->content;
}
public function persist() : object
{
if ($this->no_persist) {
throw new Exception('Attempt made to persist to no-persist filesystem');
}
$did_something = false;
foreach ($this->store as $file => $details) {
if (!$details->dirty) {
continue;
}
if ($details->content !== null) {
@mkdir(dirname($file), 0777, true);
file_put_contents($file, $details->content);
} elseif (is_file($file)) {
unlink($file);
}
$did_something = true;
$details->dirty = false;
}
if ($did_something && $this->donefile) {
file_put_contents($this->donefile, microtime(true));
}
return $this;
}
public function reset(): object
{
$this->store = [];
return $this;
}
public function revert(string $file): object
{
unset($this->store[$file]);
return $this;
}
public function forget(string $file): object
{
if (!@$this->store[$file]->dirty) {
unset($this->store[$file]);
}
return $this;
}
public function freeze()
{
$this->auto_persist = false;
$this->no_persist = true;
$this->read_only = true;
}
public function getStore()
{
foreach ($this->store as $details) {
$details->content = json_decode(json_encode($details->content));
}
return $this->store;
}
public function append(string $file, $content)
{
if ($this->read_only) {
throw new Exception('Attempt made to modify to read-only filesystem');
}
if (!isset($this->store[$file])) {
$this->load($file);
}
if ($content === null) {
return;
}
$this->store[$file]->content .= $content;
$this->store[$file]->dirty = true;
}
private function load($file, $default = null)
{
if (is_file($file)) {
$content = file_get_contents($file);
} else {
$content = $default;
}
$this->store[$file] = (object) [
'content' => $content,
'dirty' => false,
];
}
public function startPseudoTransaction()
{
if ($this->auto_persist_backup !== null) {
throw new Exception('Already in a pseudo transaction');
}
$this->auto_persist_backup = $this->auto_persist;
$this->auto_persist = false;
}
public function endPseudoTransaction()
{
if ($this->auto_persist_backup === null) {
throw new Exception('Not in a pseudo transaction');
}
$this->auto_persist = $this->auto_persist_backup;
$this->auto_persist_backup = null;
}
}