1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace Hhxsv5\LaravelS\Swoole; |
4
|
|
|
|
5
|
|
|
use Swoole\Event; |
6
|
|
|
use Swoole\Timer; |
7
|
|
|
|
8
|
|
|
class Inotify |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
private $fd; |
|
|
|
|
11
|
|
|
private $watchPath; |
|
|
|
|
12
|
|
|
private $watchMask; |
|
|
|
|
13
|
|
|
private $watchHandler; |
|
|
|
|
14
|
|
|
private $doing = false; |
|
|
|
|
15
|
|
|
private $fileTypes = []; |
|
|
|
|
16
|
|
|
private $excludedDirs = []; |
|
|
|
|
17
|
|
|
private $wdPath = []; |
|
|
|
|
18
|
|
|
private $pathWd = []; |
|
|
|
|
19
|
|
|
|
20
|
|
|
public function __construct($watchPath, $watchMask, callable $watchHandler) |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
$this->fd = inotify_init(); |
23
|
|
|
$this->watchPath = $watchPath; |
24
|
|
|
$this->watchMask = $watchMask; |
25
|
|
|
$this->watchHandler = $watchHandler; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function addFileType($type) |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
$type = '.' . trim($type, '.'); |
31
|
|
|
$this->fileTypes[$type] = true; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function addFileTypes(array $types) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
foreach ($types as $type) { |
37
|
|
|
$this->addFileType($type); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function addExcludedDir($dir) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$dir = realpath($dir); |
44
|
|
|
$this->excludedDirs[$dir] = $dir; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function addExcludedDirs(array $dirs) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
foreach ($dirs as $dir) { |
50
|
|
|
$this->addExcludedDir($dir); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function isExcluded($path) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
foreach ($this->excludedDirs as $excludedDir) { |
57
|
|
|
if ($excludedDir === $path || strpos($path, $excludedDir . '/') === 0) { |
58
|
|
|
return true; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function watch() |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$this->_watch($this->watchPath); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function _watch($path) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
if ($this->isExcluded($path)) { |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
$wd = inotify_add_watch($this->fd, $path, $this->watchMask); |
75
|
|
|
if ($wd === false) { |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
$this->bind($wd, $path); |
79
|
|
|
|
80
|
|
|
if (is_dir($path)) { |
81
|
|
|
$wd = inotify_add_watch($this->fd, $path, $this->watchMask); |
82
|
|
|
if ($wd === false) { |
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
$this->bind($wd, $path); |
86
|
|
|
$files = scandir($path); |
87
|
|
|
foreach ($files as $file) { |
88
|
|
|
if ($file === '.' || $file === '..' || $this->isExcluded($file)) { |
89
|
|
|
continue; |
90
|
|
|
} |
91
|
|
|
$file = $path . DIRECTORY_SEPARATOR . $file; |
92
|
|
|
if (is_dir($file)) { |
93
|
|
|
$this->_watch($file); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
return true; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
protected function clearWatch() |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
foreach ($this->wdPath as $wd => $path) { |
103
|
|
|
@inotify_rm_watch($this->fd, $wd); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
$this->wdPath = []; |
106
|
|
|
$this->pathWd = []; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
protected function bind($wd, $path) |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$this->pathWd[$path] = $wd; |
112
|
|
|
$this->wdPath[$wd] = $path; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
protected function unbind($wd, $path = null) |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
unset($this->wdPath[$wd]); |
118
|
|
|
if ($path !== null) { |
119
|
|
|
unset($this->pathWd[$path]); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function start() |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
Event::add($this->fd, function ($fp) { |
|
|
|
|
126
|
|
|
$events = inotify_read($fp); |
127
|
|
|
foreach ($events as $event) { |
128
|
|
|
if ($event['mask'] == IN_IGNORED) { |
129
|
|
|
continue; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$fileType = strchr($event['name'], '.'); |
133
|
|
|
if (!isset($this->fileTypes[$fileType])) { |
134
|
|
|
continue; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if ($this->doing) { |
138
|
|
|
continue; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
Timer::after(100, function () use ($event) { |
|
|
|
|
142
|
|
|
call_user_func_array($this->watchHandler, [$event]); |
143
|
|
|
$this->doing = false; |
144
|
|
|
}); |
|
|
|
|
145
|
|
|
$this->doing = true; |
146
|
|
|
break; |
147
|
|
|
} |
148
|
|
|
}); |
|
|
|
|
149
|
|
|
Event::wait(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function stop() |
|
|
|
|
153
|
|
|
{ |
154
|
|
|
Event::del($this->fd); |
155
|
|
|
fclose($this->fd); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function getWatchedFileCount() |
|
|
|
|
159
|
|
|
{ |
160
|
|
|
return count($this->wdPath); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function __destruct() |
|
|
|
|
164
|
|
|
{ |
165
|
|
|
$this->stop(); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|