Skip to content

Commit 6f6a64d

Browse files
author
ahmard
committed
Now supports file watching
Added doc blocks Renamed some methods
1 parent f906309 commit 6f6a64d

File tree

1 file changed

+55
-20
lines changed

1 file changed

+55
-20
lines changed

src/Watcher.php

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use RTC\Watcher\Watching\EventInfo;
1010
use RTC\Watcher\Watching\EventTrait;
1111
use RTC\Watcher\Watching\WatchedItem;
12-
use Swoole\Event as SWEvent;
12+
use Swoole\Event as SwooleEvent;
1313

1414
class Watcher
1515
{
@@ -58,6 +58,8 @@ public function getInotifyFD(): mixed
5858
}
5959

6060
/**
61+
* Returns list of files currently being watched
62+
*
6163
* @return WatchedItem[]
6264
*/
6365
public function getWatchedItems(): array
@@ -74,21 +76,19 @@ public function watch(): void
7476
{
7577
// Register paths
7678
foreach ($this->paths as $path) {
77-
$this->registerInotifyEvent($path);
79+
$this->recursivelyRegisterInotifyEvent($path);
7880
}
7981

8082
// Set up a new event listener for inotify read events
81-
SWEvent::add($this->getInotifyFD(), function () {
83+
SwooleEvent::add($this->getInotifyFD(), function () {
8284
$inotifyEvents = inotify_read($this->getInotifyFD());
8385

8486
// IF WE ARE LISTENING TO 'ON_ALL_EVENTS'
8587
if ($this->willWatchAny) {
8688
foreach ($inotifyEvents as $inotifyEvent) {
8789
$this->inotifyPerformAdditionalOperations($inotifyEvent);
8890

89-
if ('' != $inotifyEvent['name']) { // Filter out invalid events
90-
$this->fireEvent($inotifyEvent);
91-
}
91+
$this->fireEvent($inotifyEvent);
9292
}
9393

9494
return;
@@ -99,15 +99,15 @@ public function watch(): void
9999
$this->inotifyPerformAdditionalOperations($inotifyEvent);
100100

101101
// Make sure that we support this event
102-
if ('' != $inotifyEvent['name'] && $inotifyEvent['mask'] == $this->event->value) {
102+
if ($inotifyEvent['mask'] == $this->event->value) {
103103
$this->fireEvent($inotifyEvent);
104104
}
105105
}
106106

107107
});
108108

109109
// Set to monitor and listen for read events for the given $fd
110-
SWEvent::set(
110+
SwooleEvent::set(
111111
$this->getInotifyFD(),
112112
null,
113113
null,
@@ -128,7 +128,7 @@ private function inotifyPerformAdditionalOperations(array $inotifyEvent): void
128128
$eventInfo = new EventInfo($inotifyEvent, $this->watchedItems[$inotifyEvent['wd']]);
129129
// Register this path also if it's directory
130130
if ($eventInfo->getWatchedItem()->isDir()) {
131-
$this->registerInotifyEvent($eventInfo->getWatchedItem()->getFullPath());
131+
$this->recursivelyRegisterInotifyEvent($eventInfo->getWatchedItem()->getFullPath());
132132
}
133133

134134
return;
@@ -144,29 +144,58 @@ private function inotifyPerformAdditionalOperations(array $inotifyEvent): void
144144
}
145145
}
146146

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
148155
{
149156
if (is_dir($path)) {
150157
$iterator = new RecursiveDirectoryIterator($path);
151158

152159
// Loop through files
153160
foreach (new RecursiveIteratorIterator($iterator) as $file) {
154161
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());
165163
}
166164
}
165+
166+
return;
167167
}
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+
];
168191
}
169192

193+
/**
194+
* Stop watching file/directory
195+
*
196+
* @param EventInfo $eventInfo
197+
* @return void
198+
*/
170199
public function removeInotifyEvent(EventInfo $eventInfo)
171200
{
172201
// Stop watching event
@@ -176,6 +205,12 @@ public function removeInotifyEvent(EventInfo $eventInfo)
176205
unset($this->watchedItems[$eventInfo->getWatchDescriptor()]);
177206
}
178207

208+
/**
209+
* Trigger an event
210+
*
211+
* @param array $inotifyEvent
212+
* @return void
213+
*/
179214
private function fireEvent(array $inotifyEvent): void
180215
{
181216
$shouldFireEvent = array_key_exists($inotifyEvent['mask'], self::$constants);

0 commit comments

Comments
 (0)