@@ -48,7 +48,7 @@ public function __construct()
48
48
*
49
49
* @return mixed
50
50
*/
51
- public function getInotifyFD (): mixed
51
+ protected function getInotifyFD (): mixed
52
52
{
53
53
if (!isset ($ this ->inotifyFD )) {
54
54
$ this ->inotifyFD = inotify_init ();
@@ -57,71 +57,13 @@ public function getInotifyFD(): mixed
57
57
return $ this ->inotifyFD ;
58
58
}
59
59
60
- /**
61
- * Returns list of files currently being watched
62
- *
63
- * @return WatchedItem[]
64
- */
65
- public function getWatchedItems (): array
66
- {
67
- return $ this ->watchedItems ;
68
- }
69
-
70
- /**
71
- * Start watching file system changes
72
- *
73
- * @return void
74
- */
75
- public function watch (): void
76
- {
77
- // Register paths
78
- foreach ($ this ->paths as $ path ) {
79
- $ this ->recursivelyRegisterInotifyEvent ($ path );
80
- }
81
-
82
- // Set up a new event listener for inotify read events
83
- SwooleEvent::add ($ this ->getInotifyFD (), function () {
84
- $ inotifyEvents = inotify_read ($ this ->getInotifyFD ());
85
-
86
- // IF WE ARE LISTENING TO 'ON_ALL_EVENTS'
87
- if ($ this ->willWatchAny ) {
88
- foreach ($ inotifyEvents as $ inotifyEvent ) {
89
- $ this ->inotifyPerformAdditionalOperations ($ inotifyEvent );
90
-
91
- $ this ->fireEvent ($ inotifyEvent );
92
- }
93
-
94
- return ;
95
- }
96
-
97
- // INDIVIDUAL LISTENERS
98
- foreach ($ inotifyEvents as $ inotifyEvent ) {
99
- $ this ->inotifyPerformAdditionalOperations ($ inotifyEvent );
100
-
101
- // Make sure that we support this event
102
- if ($ inotifyEvent ['mask ' ] == $ this ->event ->value ) {
103
- $ this ->fireEvent ($ inotifyEvent );
104
- }
105
- }
106
-
107
- });
108
-
109
- // Set to monitor and listen for read events for the given $fd
110
- SwooleEvent::set (
111
- $ this ->getInotifyFD (),
112
- null ,
113
- null ,
114
- SWOOLE_EVENT_READ
115
- );
116
- }
117
-
118
60
/**
119
61
* Handles directory creation/deletion on the fly
120
62
*
121
63
* @param array $inotifyEvent
122
64
* @return void
123
65
*/
124
- private function inotifyPerformAdditionalOperations (array $ inotifyEvent ): void
66
+ protected function inotifyPerformAdditionalOperations (array $ inotifyEvent ): void
125
67
{
126
68
// Handle directory creation
127
69
if ($ inotifyEvent ['mask ' ] == $ this ->maskItemCreated ) {
@@ -151,7 +93,7 @@ private function inotifyPerformAdditionalOperations(array $inotifyEvent): void
151
93
* @param string $path
152
94
* @return void
153
95
*/
154
- private function recursivelyRegisterInotifyEvent (string $ path ): void
96
+ protected function recursivelyRegisterInotifyEvent (string $ path ): void
155
97
{
156
98
if (is_dir ($ path )) {
157
99
$ iterator = new RecursiveDirectoryIterator ($ path );
@@ -176,7 +118,7 @@ private function recursivelyRegisterInotifyEvent(string $path): void
176
118
* @param string $path
177
119
* @return void
178
120
*/
179
- private function registerInotifyEvent (string $ path ): void
121
+ protected function registerInotifyEvent (string $ path ): void
180
122
{
181
123
$ descriptor = inotify_add_watch (
182
124
$ this ->getInotifyFD (),
@@ -196,7 +138,7 @@ private function registerInotifyEvent(string $path): void
196
138
* @param EventInfo $eventInfo
197
139
* @return void
198
140
*/
199
- public function removeInotifyEvent (EventInfo $ eventInfo )
141
+ protected function removeInotifyEvent (EventInfo $ eventInfo )
200
142
{
201
143
// Stop watching event
202
144
inotify_rm_watch ($ this ->getInotifyFD (), $ eventInfo ->getWatchDescriptor ());
@@ -211,7 +153,7 @@ public function removeInotifyEvent(EventInfo $eventInfo)
211
153
* @param array $inotifyEvent
212
154
* @return void
213
155
*/
214
- private function fireEvent (array $ inotifyEvent ): void
156
+ protected function fireEvent (array $ inotifyEvent ): void
215
157
{
216
158
$ shouldFireEvent = array_key_exists ($ inotifyEvent ['mask ' ], self ::$ constants );
217
159
@@ -248,6 +190,64 @@ private function fireEvent(array $inotifyEvent): void
248
190
}
249
191
}
250
192
193
+ /**
194
+ * Returns list of files currently being watched
195
+ *
196
+ * @return WatchedItem[]
197
+ */
198
+ public function getWatchedItems (): array
199
+ {
200
+ return $ this ->watchedItems ;
201
+ }
202
+
203
+ /**
204
+ * Start watching file system changes
205
+ *
206
+ * @return void
207
+ */
208
+ public function watch (): void
209
+ {
210
+ // Register paths
211
+ foreach ($ this ->paths as $ path ) {
212
+ $ this ->recursivelyRegisterInotifyEvent ($ path );
213
+ }
214
+
215
+ // Set up a new event listener for inotify read events
216
+ SwooleEvent::add ($ this ->getInotifyFD (), function () {
217
+ $ inotifyEvents = inotify_read ($ this ->getInotifyFD ());
218
+
219
+ // IF WE ARE LISTENING TO 'ON_ALL_EVENTS'
220
+ if ($ this ->willWatchAny ) {
221
+ foreach ($ inotifyEvents as $ inotifyEvent ) {
222
+ $ this ->inotifyPerformAdditionalOperations ($ inotifyEvent );
223
+
224
+ $ this ->fireEvent ($ inotifyEvent );
225
+ }
226
+
227
+ return ;
228
+ }
229
+
230
+ // INDIVIDUAL LISTENERS
231
+ foreach ($ inotifyEvents as $ inotifyEvent ) {
232
+ $ this ->inotifyPerformAdditionalOperations ($ inotifyEvent );
233
+
234
+ // Make sure that we support this event
235
+ if ($ inotifyEvent ['mask ' ] == $ this ->event ->value ) {
236
+ $ this ->fireEvent ($ inotifyEvent );
237
+ }
238
+ }
239
+
240
+ });
241
+
242
+ // Set to monitor and listen for read events for the given $fd
243
+ SwooleEvent::set (
244
+ $ this ->getInotifyFD (),
245
+ null ,
246
+ null ,
247
+ SWOOLE_EVENT_READ
248
+ );
249
+ }
250
+
251
251
/**
252
252
* Add file extension filter
253
253
*
0 commit comments