-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.php
52 lines (45 loc) · 1.23 KB
/
watcher.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
<?php
class FEC_Watcher {
function __construct($files, $path = null) {
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
$this->send_message(array(
'message' => 'Checking for file modifications...'
));
array_walk($files, function(&$tracked_files, $source) use($path) {
if (is_string($tracked_files)) {
$tracked_files = array($tracked_files);
}
$tracked_files[] = $source;
if (isset($path)) {
array_walk($tracked_files, function(&$file) use($path) {
$file = rtrim($path, '/') . '/' . ltrim($file, '/');
});
}
});
foreach($files as $source => $files) {
foreach($files as $file) {
if (file_exists($file)) {
$last_modified = filemtime($file);
if ($last_modified >= time()) {
$this->send_message(array(
'action' => 'refresh',
'file' => $file,
'source' => $source
));
}
}
}
}
}
function send_message($data) {
ob_start();
echo 'id: ' . time() . PHP_EOL;
echo 'data: ' . json_encode($data) . PHP_EOL;
echo 'retry: 500' . PHP_EOL;
echo PHP_EOL;
ob_flush();
flush();
}
}
?>