Skip to content

Commit 6cf2604

Browse files
authored
Merge pull request #20 from phprtc/dev
v0.1.0
2 parents 9dfd4c2 + 48e9617 commit 6cf2604

File tree

10 files changed

+463
-125
lines changed

10 files changed

+463
-125
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@
55
/.test1
66
/.test2
77
/.test.php
8-
.phpunit.result.cache
8+
.phpunit.result.cache
9+
10+
/tests/bait

README.md

Lines changed: 70 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ PHP-based file system changes watcher implemented using [**Swoole**](https://swo
55
## Installation
66

77
```
8-
composer require phprtc/watcher ^0.0 --dev
8+
composer require phprtc/watcher ^0.1 --dev
99
```
1010

1111
## Usage
@@ -14,6 +14,7 @@ composer require phprtc/watcher ^0.0 --dev
1414

1515
```php
1616
use RTC\Watcher\Watcher;
17+
use RTC\Watcher\Watching\EventInfo;
1718

1819
require 'vendor/autoload.php';
1920

@@ -22,14 +23,59 @@ Watcher::create()
2223
->addPath(__DIR__ . '/views')
2324
->onChange(function (EventInfo $eventInfo) {
2425
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();
2671
```
2772

2873
#### Filter
2974

3075
- Make sure that the file whose event is being fired should not end with provided characters.
3176
```php
3277
use RTC\Watcher\Watcher;
78+
use RTC\Watcher\Watching\EventInfo;
3379

3480
require 'vendor/autoload.php';
3581

@@ -38,12 +84,14 @@ Watcher::create()
3884
->fileShouldNotEndWith(['.php'])
3985
->onChange(function (EventInfo $eventInfo) {
4086
echo $eventInfo->getWatchedItem()->getFullPath() . PHP_EOL;
41-
});
87+
})
88+
->watch();
4289
```
4390

4491
- Only listen to event with file name that matches given extension(s).
4592
```php
4693
use RTC\Watcher\Watcher;
94+
use RTC\Watcher\Watching\EventInfo;
4795

4896
require 'vendor/autoload.php';
4997

@@ -52,25 +100,32 @@ Watcher::create()
52100
->addExtension('php')
53101
->onChange(function (EventInfo $eventInfo) {
54102
echo $eventInfo->getWatchedItem()->getFullPath() . PHP_EOL;
55-
});
103+
})
104+
->watch();
56105
```
57106

58-
#### Any-event
59-
60-
Listens to any event on given path
61-
62-
Be careful using this method.
107+
#### Stopping Watcher
63108

64109
```php
110+
65111
use RTC\Watcher\Watcher;
112+
use RTC\Watcher\Watching\EventInfo;
113+
use Swoole\Timer;
66114

67115
require 'vendor/autoload.php';
68116

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";
73121
});
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');
74129
```
75130

76131
#### Swoole Server Integration
@@ -94,7 +149,8 @@ $server->on('start', function (Server $server) {
94149
Watcher::create()
95150
->addPath(__DIR__ . '/app')
96151
->addPath(__DIR__ . '/views')
97-
->onChange(fn() => $server->reload());
152+
->onChange(fn() => $server->reload())
153+
->watch();
98154
});
99155

100156
$server->start();

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"evenement/evenement": "^3.0"
1616
},
1717
"require-dev": {
18-
"phpstan/phpstan": "^0.12.90",
18+
"phpstan/phpstan": "^1.5",
1919
"phpunit/phpunit": "^9.5",
2020
"symfony/var-dumper": "^6.0",
2121
"openswoole/ide-helper": "^4.10"
@@ -25,6 +25,11 @@
2525
"RTC\\Watcher\\": "src/"
2626
}
2727
},
28+
"autoload-dev": {
29+
"psr-4": {
30+
"RTC\\Tests\\Watcher\\": "tests/"
31+
}
32+
},
2833
"scripts": {
2934
"analyse" : "vendor/bin/phpstan analyse --debug",
3035
"test" : "vendor/bin/phpunit"

0 commit comments

Comments
 (0)