@@ -5,7 +5,7 @@ PHP-based file system changes watcher implemented using [**Swoole**](https://swo
5
5
## Installation
6
6
7
7
```
8
- composer require phprtc/watcher ^0.0 --dev
8
+ composer require phprtc/watcher ^0.1 --dev
9
9
```
10
10
11
11
## Usage
@@ -14,6 +14,7 @@ composer require phprtc/watcher ^0.0 --dev
14
14
15
15
``` php
16
16
use RTC\Watcher\Watcher;
17
+ use RTC\Watcher\Watching\EventInfo;
17
18
18
19
require 'vendor/autoload.php';
19
20
@@ -22,14 +23,59 @@ Watcher::create()
22
23
->addPath(__DIR__ . '/views')
23
24
->onChange(function (EventInfo $eventInfo) {
24
25
echo $eventInfo->getWatchedItem()->getFullPath() . PHP_EOL;
25
- });
26
+ })
27
+ ->watch();
28
+ ```
29
+
30
+ #### Any Event
31
+
32
+ Listens to any event on given path
33
+
34
+ Be careful using this method.
35
+
36
+ ``` php
37
+ use RTC\Watcher\Watcher;
38
+ use RTC\Watcher\Watching\EventInfo;
39
+
40
+ require 'vendor/autoload.php';
41
+
42
+ Watcher::create()
43
+ ->addPath(__DIR__ . '/app')
44
+ ->onAny(function (EventInfo $eventInfo) {
45
+ echo date('H:i:s') . " - {$eventInfo->getName()} {$eventInfo->getWatchedItem()->getFullPath()}\n";
46
+ })
47
+ ->watch();
48
+ ```
49
+
50
+ #### Ignoring Path
51
+
52
+ Ignore files using regular expression
53
+
54
+ ``` php
55
+ use RTC\Watcher\Watcher;
56
+ use RTC\Watcher\Watching\EventInfo;
57
+
58
+ require 'vendor/autoload.php';
59
+
60
+ Watcher::create()
61
+ ->addPath(__DIR__ . '/app')
62
+ ->ignore(__DIR__ . '/test1/t/*') // Ignore files in "/test1/t/"
63
+ ->ignore([
64
+ __DIR__ . '/test1/t/.*(\.php$)', // Ignore files that end with "php" in "/test1/t/"
65
+ __DIR__ . '/test1/t/.*(\.js)', // Ignore files that end with "js" in "/test1/t/"
66
+ ])
67
+ ->onChange(function (EventInfo $eventInfo) {
68
+ echo date('H:i:s') . " - {$eventInfo->getName()} {$eventInfo->getWatchedItem()->getFullPath()}\n";
69
+ })
70
+ ->watch();
26
71
```
27
72
28
73
#### Filter
29
74
30
75
- Make sure that the file whose event is being fired should not end with provided characters.
31
76
``` php
32
77
use RTC\Watcher\Watcher;
78
+ use RTC\Watcher\Watching\EventInfo;
33
79
34
80
require 'vendor/autoload.php';
35
81
@@ -38,12 +84,14 @@ Watcher::create()
38
84
->fileShouldNotEndWith(['.php'])
39
85
->onChange(function (EventInfo $eventInfo) {
40
86
echo $eventInfo->getWatchedItem()->getFullPath() . PHP_EOL;
41
- });
87
+ })
88
+ ->watch();
42
89
```
43
90
44
91
- Only listen to event with file name that matches given extension(s).
45
92
```php
46
93
use RTC\Watcher\Watcher;
94
+ use RTC\Watcher\Watching\EventInfo;
47
95
48
96
require 'vendor/autoload.php';
49
97
@@ -52,25 +100,32 @@ Watcher::create()
52
100
->addExtension('php')
53
101
->onChange(function (EventInfo $eventInfo) {
54
102
echo $eventInfo->getWatchedItem()->getFullPath() . PHP_EOL;
55
- });
103
+ })
104
+ ->watch();
56
105
```
57
106
58
- #### Any-event
59
-
60
- Listens to any event on given path
61
-
62
- Be careful using this method.
107
+ #### Stopping Watcher
63
108
64
109
```php
110
+
65
111
use RTC\Watcher\Watcher;
112
+ use RTC\Watcher\Watching\EventInfo;
113
+ use Swoole\Timer;
66
114
67
115
require 'vendor/autoload.php';
68
116
69
- Watcher::create()
70
- ->addPath(__DIR__ . '/app' )
71
- ->onAny (function (EventInfo $eventInfo) {
72
- echo date('H:i:s') . " - {$eventInfo->getName()} {$eventInfo->getWatchedItem()->getFullPath()}\n";
117
+ $watcher = Watcher::create()
118
+ ->addPath(__DIR__)
119
+ ->onCreate (function (EventInfo $eventInfo) {
120
+ echo date('H:i:s') . ": CREATED - {$eventInfo->getWatchedItem()->getFullPath()}\n";
73
121
});
122
+
123
+ Timer::after(1000, fn() => $watcher->stop()); // Stop watching after 1 second
124
+
125
+ $watcher->start();
126
+
127
+ touch(__DIR__ . '/auto-created.txt');
128
+ unlink(__DIR__ . '/auto-created.txt');
74
129
```
75
130
76
131
#### Swoole Server Integration
@@ -94,7 +149,8 @@ $server->on('start', function (Server $server) {
94
149
Watcher::create()
95
150
->addPath(__DIR__ . '/app')
96
151
->addPath(__DIR__ . '/views')
97
- ->onChange(fn() => $server->reload());
152
+ ->onChange(fn() => $server->reload())
153
+ ->watch();
98
154
});
99
155
100
156
$server->start();
0 commit comments