-
Notifications
You must be signed in to change notification settings - Fork 6
/
LiveReload.php
238 lines (209 loc) 路 5.86 KB
/
LiveReload.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
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
namespace RockFrontend;
use ProcessWire\Paths;
use ProcessWire\RockFrontend;
use ProcessWire\Wire;
use ProcessWire\WireData;
use RecursiveCallbackFilterIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
class LiveReload extends Wire
{
private $config;
public function __construct()
{
$config = $this->wire->config->livereload;
if (is_bool($config)) $config = (int)$config; // convert true/false to int
if (is_int($config)) $config = ['interval' => $config]; // set interval
if (is_float($config)) $config = ['interval' => $config]; // set interval
// prepare config object
$this->config = $this->wire(new WireData());
$this->config->setArray([
'interval' => 1,
'includeDefaults' => [
$this->wire->config->paths->site,
$this->wire->config->paths->root . "RockShell/docs",
$this->wire->config->paths->root . "RockShell/App",
],
// user defined includes
'include' => [],
'excludeDefaults' => [
'.*/vendor',
'.*/\.git',
'.*/\.github',
'.*/\.vscode',
'.*/site/assets/backups',
'.*/site/assets/cache',
'.*/site/assets/files',
'.*/site/assets/logs',
'.*/site/assets/sessions',
'.*/site/assets/ProCache-*',
'.*/site/assets/pwpc',
'.*/site/assets/RockFrontend/.*.css',
'.*/site/assets/RockPdfDumps/*',
'.*/site/assets/RockPdf/*',
'.*/site/modules/TracyDebugger/tracy-.*',
'.*/site/modules/RockBlocks/blocks/.*.css',
'.*/site/templates/bundle/*',
'.*/site/templates/uikit',
'.*/site/templates/uikit-*',
],
// user defined exclude regexes
'exclude' => [],
]);
$this->config->setArray($config ?: []);
}
/**
* Find modified file after timestamp
* This will return on first match
* @return void
*/
public function findModifiedFile($since)
{
// db(date("H:i:s", $since));
// $timer = Debug::startTimer();
// recurse files and return if a file has changed
$filter = function ($file, $key, $iterator) {
$file = Paths::normalizeSeparators($file);
// echo "<br><br>Checking $file... ";
foreach ($this->getExcludes() as $regex) {
$isExcluded = preg_match($regex, $file);
// echo "<br> $isExcluded $regex";
if ($isExcluded) return false;
}
// echo "<br>$file";
return true;
};
// loop over all included folders
foreach ($this->getIncludes() as $path) {
if (!is_dir($path)) continue;
$iterator = new RecursiveDirectoryIterator(
$path,
RecursiveDirectoryIterator::SKIP_DOTS
);
$files = new RecursiveIteratorIterator(
new RecursiveCallbackFilterIterator($iterator, $filter)
);
foreach ($files as $file) {
if ($this->hasChanged($file, $since)) {
// bd(Debug::stopTimer($timer, "ms"));
return $file;
}
}
}
// bd(Debug::stopTimer($timer, "ms"));
}
/**
* Get all regexes for excluded folders or files
*/
public function getExcludes(): array
{
$arr = array_unique(array_merge(
$this->config->excludeDefaults,
$this->config->exclude
));
$arr = array_map(function ($item) {
return "~$item~";
}, $arr);
return $arr;
}
/**
* Return array of merged include folders
*/
public function getIncludes(): array
{
return array_unique(array_merge(
$this->config->includeDefaults,
$this->config->include
));
}
/**
* Return secret from get variable
*/
public function getSecret(): string
{
return (string)$_GET[RockFrontend::getParam];
}
/**
* Has the file changed since given timestamp?
*/
public function hasChanged($file, $since): bool
{
$m = filemtime($file);
return $m > $since;
}
/**
* Send SSE message to client
* @return void
*/
public function sse($msg)
{
echo "data: $msg\n\n";
echo str_pad('', 8186) . "\n";
flush();
}
public function validSecret()
{
$secret = $this->getSecret();
$cacheName = RockFrontend::livereloadCacheName . "_$secret";
// delete outdated caches
// test
$time = date("Y-m-d H:i:s", time());
$this->database->query("DELETE
FROM `caches`
WHERE `name` LIKE 'rockfrontend_livereload_%'
AND `expires` < '$time'
");
if ($this->wire->cache->get($cacheName)) {
$this->wire->cache->delete($cacheName);
return true;
}
return false;
}
/**
* Watch the system for changed files
*/
public function watch()
{
// we dont want warnings in the stream
// for debugging you can uncomment this line
error_reporting(E_ALL & ~E_WARNING);
header("Cache-Control: no-cache");
header("Content-Type: text/event-stream");
$build = $this->wire->config->livereloadBuild;
$debug = $this->wire->config->debug;
$this->wire->log->prune('livereload', 1);
$opt = [
'showURL' => false,
'showUser' => false,
];
// start loop
$start = time();
while (true) {
$file = $this->findModifiedFile($start);
// file changed
if ($file && $debug) {
$this->wire->log->save('livereload', "File changed: $file", $opt);
if ($build) {
$cmd = "npm run build";
$this->wire->log->save('livereload', "Rebuilding tailwind with '$cmd'", $opt);
exec($cmd);
}
}
// send trigger to frontend
$this->sse($file);
// add note to log
if ($file) ob_end_flush();
while (ob_get_level() > 0) ob_end_flush();
// stop loop when connection is aborted
if (connection_aborted()) break;
// sleep until next try
$sleepSeconds = (float)$this->wire->config->livereload ?: 1.0;
usleep($sleepSeconds * 1000000);
}
}
public function __debuginfo()
{
return $this->config->getArray();
}
}