9
9
use RTC \Watcher \Watching \EventInfo ;
10
10
use RTC \Watcher \Watching \EventTrait ;
11
11
use RTC \Watcher \Watching \WatchedItem ;
12
- use Swoole \Event as SWEvent ;
12
+ use Swoole \Event as SwooleEvent ;
13
13
14
14
class Watcher
15
15
{
@@ -58,6 +58,8 @@ public function getInotifyFD(): mixed
58
58
}
59
59
60
60
/**
61
+ * Returns list of files currently being watched
62
+ *
61
63
* @return WatchedItem[]
62
64
*/
63
65
public function getWatchedItems (): array
@@ -74,21 +76,19 @@ public function watch(): void
74
76
{
75
77
// Register paths
76
78
foreach ($ this ->paths as $ path ) {
77
- $ this ->registerInotifyEvent ($ path );
79
+ $ this ->recursivelyRegisterInotifyEvent ($ path );
78
80
}
79
81
80
82
// Set up a new event listener for inotify read events
81
- SWEvent ::add ($ this ->getInotifyFD (), function () {
83
+ SwooleEvent ::add ($ this ->getInotifyFD (), function () {
82
84
$ inotifyEvents = inotify_read ($ this ->getInotifyFD ());
83
85
84
86
// IF WE ARE LISTENING TO 'ON_ALL_EVENTS'
85
87
if ($ this ->willWatchAny ) {
86
88
foreach ($ inotifyEvents as $ inotifyEvent ) {
87
89
$ this ->inotifyPerformAdditionalOperations ($ inotifyEvent );
88
90
89
- if ('' != $ inotifyEvent ['name ' ]) { // Filter out invalid events
90
- $ this ->fireEvent ($ inotifyEvent );
91
- }
91
+ $ this ->fireEvent ($ inotifyEvent );
92
92
}
93
93
94
94
return ;
@@ -99,15 +99,15 @@ public function watch(): void
99
99
$ this ->inotifyPerformAdditionalOperations ($ inotifyEvent );
100
100
101
101
// Make sure that we support this event
102
- if ('' != $ inotifyEvent [ ' name ' ] && $ inotifyEvent ['mask ' ] == $ this ->event ->value ) {
102
+ if ($ inotifyEvent ['mask ' ] == $ this ->event ->value ) {
103
103
$ this ->fireEvent ($ inotifyEvent );
104
104
}
105
105
}
106
106
107
107
});
108
108
109
109
// Set to monitor and listen for read events for the given $fd
110
- SWEvent ::set (
110
+ SwooleEvent ::set (
111
111
$ this ->getInotifyFD (),
112
112
null ,
113
113
null ,
@@ -128,7 +128,7 @@ private function inotifyPerformAdditionalOperations(array $inotifyEvent): void
128
128
$ eventInfo = new EventInfo ($ inotifyEvent , $ this ->watchedItems [$ inotifyEvent ['wd ' ]]);
129
129
// Register this path also if it's directory
130
130
if ($ eventInfo ->getWatchedItem ()->isDir ()) {
131
- $ this ->registerInotifyEvent ($ eventInfo ->getWatchedItem ()->getFullPath ());
131
+ $ this ->recursivelyRegisterInotifyEvent ($ eventInfo ->getWatchedItem ()->getFullPath ());
132
132
}
133
133
134
134
return ;
@@ -144,29 +144,58 @@ private function inotifyPerformAdditionalOperations(array $inotifyEvent): void
144
144
}
145
145
}
146
146
147
- private function registerInotifyEvent (string $ path ): void
147
+ /**
148
+ * Register directory/file to inotify watcher
149
+ * Loops through directory recursively and register all it's subdirectories as well
150
+ *
151
+ * @param string $path
152
+ * @return void
153
+ */
154
+ private function recursivelyRegisterInotifyEvent (string $ path ): void
148
155
{
149
156
if (is_dir ($ path )) {
150
157
$ iterator = new RecursiveDirectoryIterator ($ path );
151
158
152
159
// Loop through files
153
160
foreach (new RecursiveIteratorIterator ($ iterator ) as $ file ) {
154
161
if ($ file ->isDir ()/**&& !in_array($file->getRealPath(), $this->watchedItems)**/ ) {
155
- $ descriptor = inotify_add_watch (
156
- $ this ->getInotifyFD (),
157
- $ file ->getRealPath (),
158
- Event::ON_ALL_EVENTS ->value
159
- );
160
-
161
- $ this ->watchedItems [$ descriptor ] = [
162
- 'path ' => $ file ->getRealPath (),
163
- 'mask ' => $ this ->event ->value ,
164
- ];
162
+ $ this ->registerInotifyEvent ($ file ->getRealPath ());
165
163
}
166
164
}
165
+
166
+ return ;
167
167
}
168
+
169
+ // Register file watch
170
+ $ this ->registerInotifyEvent ($ path );
171
+ }
172
+
173
+ /**
174
+ * Register directory/file to inotify watcher
175
+ *
176
+ * @param string $path
177
+ * @return void
178
+ */
179
+ private function registerInotifyEvent (string $ path ): void
180
+ {
181
+ $ descriptor = inotify_add_watch (
182
+ $ this ->getInotifyFD (),
183
+ $ path ,
184
+ Event::ON_ALL_EVENTS ->value
185
+ );
186
+
187
+ $ this ->watchedItems [$ descriptor ] = [
188
+ 'path ' => $ path ,
189
+ 'mask ' => $ this ->event ->value ,
190
+ ];
168
191
}
169
192
193
+ /**
194
+ * Stop watching file/directory
195
+ *
196
+ * @param EventInfo $eventInfo
197
+ * @return void
198
+ */
170
199
public function removeInotifyEvent (EventInfo $ eventInfo )
171
200
{
172
201
// Stop watching event
@@ -176,6 +205,12 @@ public function removeInotifyEvent(EventInfo $eventInfo)
176
205
unset($ this ->watchedItems [$ eventInfo ->getWatchDescriptor ()]);
177
206
}
178
207
208
+ /**
209
+ * Trigger an event
210
+ *
211
+ * @param array $inotifyEvent
212
+ * @return void
213
+ */
179
214
private function fireEvent (array $ inotifyEvent ): void
180
215
{
181
216
$ shouldFireEvent = array_key_exists ($ inotifyEvent ['mask ' ], self ::$ constants );
0 commit comments