Skip to content

Commit 30d885f

Browse files
authored
Merge pull request #4 from phprtc/dev
Fixed methods visibility & and made Watcher extensible
2 parents 7788e8e + e048b56 commit 30d885f

File tree

2 files changed

+70
-71
lines changed

2 files changed

+70
-71
lines changed

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ Watcher::create()
2121
->addPath(__DIR__ . '/app')
2222
->addPath(__DIR__ . '/views')
2323
->onChange(function (EventInfo $eventInfo) {
24-
var_dump($eventInfo->getWatchedItem()->getFullPath());
24+
echo $eventInfo->getWatchedItem()->getFullPath() . PHP_EOL;
2525
});
2626
```
2727

2828
#### Filter
29+
2930
- Make sure that the file whose event is being fired should not end with provided characters.
3031
```php
3132
use RTC\Watcher\Watcher;
@@ -36,7 +37,7 @@ Watcher::create()
3637
->addPath(__DIR__ . '/app')
3738
->fileShouldNotEndWith(['.php'])
3839
->onChange(function (EventInfo $eventInfo) {
39-
var_dump($eventInfo->getWatchedItem()->getFullPath());
40+
echo $eventInfo->getWatchedItem()->getFullPath() . PHP_EOL;
4041
});
4142
```
4243

@@ -50,12 +51,12 @@ Watcher::create()
5051
->addPath(__DIR__ . '/app')
5152
->addExtension('php')
5253
->onChange(function (EventInfo $eventInfo) {
53-
var_dump($eventInfo->getWatchedItem()->getFullPath());
54+
echo $eventInfo->getWatchedItem()->getFullPath() . PHP_EOL;
5455
});
5556
```
5657

57-
5858
#### Any-event
59+
5960
Listens to any event on given path
6061

6162
Be careful using this method.
@@ -68,12 +69,10 @@ require 'vendor/autoload.php';
6869
Watcher::create()
6970
->addPath(__DIR__ . '/app')
7071
->onAny(function (EventInfo $eventInfo) {
71-
var_dump($eventInfo->getWatchedItem()->getFullPath());
72+
echo date('H:i:s') . " - {$eventInfo->getName()} {$eventInfo->getWatchedItem()->getFullPath()}\n";
7273
});
7374
```
7475

75-
76-
7776
#### Swoole Server Integration
7877

7978
```php

src/Watcher.php

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function __construct()
4848
*
4949
* @return mixed
5050
*/
51-
public function getInotifyFD(): mixed
51+
protected function getInotifyFD(): mixed
5252
{
5353
if (!isset($this->inotifyFD)) {
5454
$this->inotifyFD = inotify_init();
@@ -57,71 +57,13 @@ public function getInotifyFD(): mixed
5757
return $this->inotifyFD;
5858
}
5959

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-
11860
/**
11961
* Handles directory creation/deletion on the fly
12062
*
12163
* @param array $inotifyEvent
12264
* @return void
12365
*/
124-
private function inotifyPerformAdditionalOperations(array $inotifyEvent): void
66+
protected function inotifyPerformAdditionalOperations(array $inotifyEvent): void
12567
{
12668
// Handle directory creation
12769
if ($inotifyEvent['mask'] == $this->maskItemCreated) {
@@ -151,7 +93,7 @@ private function inotifyPerformAdditionalOperations(array $inotifyEvent): void
15193
* @param string $path
15294
* @return void
15395
*/
154-
private function recursivelyRegisterInotifyEvent(string $path): void
96+
protected function recursivelyRegisterInotifyEvent(string $path): void
15597
{
15698
if (is_dir($path)) {
15799
$iterator = new RecursiveDirectoryIterator($path);
@@ -176,7 +118,7 @@ private function recursivelyRegisterInotifyEvent(string $path): void
176118
* @param string $path
177119
* @return void
178120
*/
179-
private function registerInotifyEvent(string $path): void
121+
protected function registerInotifyEvent(string $path): void
180122
{
181123
$descriptor = inotify_add_watch(
182124
$this->getInotifyFD(),
@@ -196,7 +138,7 @@ private function registerInotifyEvent(string $path): void
196138
* @param EventInfo $eventInfo
197139
* @return void
198140
*/
199-
public function removeInotifyEvent(EventInfo $eventInfo)
141+
protected function removeInotifyEvent(EventInfo $eventInfo)
200142
{
201143
// Stop watching event
202144
inotify_rm_watch($this->getInotifyFD(), $eventInfo->getWatchDescriptor());
@@ -211,7 +153,7 @@ public function removeInotifyEvent(EventInfo $eventInfo)
211153
* @param array $inotifyEvent
212154
* @return void
213155
*/
214-
private function fireEvent(array $inotifyEvent): void
156+
protected function fireEvent(array $inotifyEvent): void
215157
{
216158
$shouldFireEvent = array_key_exists($inotifyEvent['mask'], self::$constants);
217159

@@ -248,6 +190,64 @@ private function fireEvent(array $inotifyEvent): void
248190
}
249191
}
250192

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+
251251
/**
252252
* Add file extension filter
253253
*

0 commit comments

Comments
 (0)