Skip to content

Commit 97e8063

Browse files
committed
fix: interval and watch
1 parent fb81a15 commit 97e8063

File tree

3 files changed

+137
-7
lines changed

3 files changed

+137
-7
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Cydrick Nonog
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# PHP Watcher
2+
3+
A simple directory and file watcher that was made using PHP.
4+
5+
## Installation
6+
7+
```shell
8+
composer require cydrickn/php-watcher
9+
```
10+
11+
## Usage
12+
13+
To use this package you just need to initialize the watcher and call the tick function
14+
15+
```php
16+
<?php
17+
18+
require_once './vendor/autoload.php';
19+
20+
$watcher = new \Cydrickn\PHPWatcher\Watcher(
21+
[__DIR__],
22+
[__DIR__ . '/vendor/'],
23+
function (array $changes) {
24+
echo json_encode($changes) . PHP_EOL;
25+
}
26+
);
27+
28+
$watcher->tick();
29+
```
30+
31+
## \Cydrickn\PHPWatcher\Watcher::__construct
32+
33+
\Cydrickn\PHPWatcher\Watcher::__construct - Creates the instance representing the watcher
34+
35+
### Description
36+
37+
```php
38+
public \Cydrickn\PHPWatcher\Watcher::__construct(
39+
array $watchFor,
40+
array $excludes,
41+
callable $handler,
42+
int $interval = 1000
43+
)
44+
```
45+
46+
### Parameters
47+
48+
**watchFor**
49+
50+
List of files and folder that will watch by the watcher.
51+
52+
For folders this will include its sub-folders.
53+
54+
**excludes**
55+
56+
List of files and folder that will be excluded in watching.
57+
58+
For folders this will include its sub-folders.
59+
60+
**handler**
61+
62+
A function that will be called once their are changes
63+
64+
**interval**
65+
66+
This is delay for how long it will wait before it will do checking of the files / folders. Default to 1000 milliseconds.
67+
68+
## \Cydrickn\PHPWatcher\Watcher::checkChanges
69+
70+
\Cydrickn\PHPWatcher\Watcher::checkChanges - Check the changes
71+
72+
### Description
73+
74+
```php
75+
public \Cydrickn\PHPWatcher\Watcher::checkChanges(): void
76+
```
77+
78+
## \Cydrickn\PHPWatcher\Watcher::tick
79+
80+
\Cydrickn\PHPWatcher\Watcher::tick - Start the watching of files
81+
82+
### Description
83+
84+
```php
85+
public \Cydrickn\PHPWatcher\Watcher::tick(
86+
?callable $handler = null
87+
): void
88+
```
89+
90+
### Parameters
91+
92+
**handler**
93+
94+
A callable use for watching the file, this is default to null.
95+
96+
Once the handler is null, it will use the default handler.
97+
98+
There are two default handler.
99+
100+
- Swoole\Timer - Will be only use when swoole is enabled in your server
101+
- The do while - If the swoole is not enabled
102+
103+
Once you pass your own handler, this will pass two argument
104+
105+
- The first argument would be the checkChanges function
106+
- The second is the interval

src/Watcher.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ public function __construct(private array $watchFor, array $excludes, callable $
3131
}
3232

3333

34-
public function addChange(string $filename, ChangeType $type): void
34+
protected function addChange(string $filename, ChangeType $type): void
3535
{
3636
$this->changes[] = ['name' => $filename, 'type' => $type, 'data' => filemtime($filename)];
3737
}
3838

39-
public function checkFile(string $file): void
39+
protected function checkFile(string $file): void
4040
{
4141
if (!array_key_exists($file, $this->files) && file_exists($file)) {
4242
$this->addChange($file, ChangeType::NEW);
@@ -60,12 +60,12 @@ public function checkFile(string $file): void
6060
}
6161
}
6262

63-
public function clearChanges(): void
63+
protected function clearChanges(): void
6464
{
6565
$this->changes = [];
6666
}
6767

68-
public function commit(): void
68+
protected function commit(): void
6969
{
7070
$totalChanges = count($this->changes);
7171
foreach ($this->changes as $change) {
@@ -94,7 +94,7 @@ public function checkChanges(): void
9494

9595
foreach ($this->watchFor as $watchFor) {
9696
if (is_dir($watchFor)) {
97-
$allFiles = new RecursiveTreeIterator(new RecursiveDirectoryIterator(__DIR__, RecursiveDirectoryIterator::SKIP_DOTS));
97+
$allFiles = new RecursiveTreeIterator(new RecursiveDirectoryIterator($watchFor, RecursiveDirectoryIterator::SKIP_DOTS));
9898
foreach ($allFiles as $file) {
9999
$filename = trim(str_replace(['|', ' ', '~', '\\'], '', $file), '-');
100100
$filename = is_dir($filename) ? $filename . '/' : $filename;
@@ -117,8 +117,11 @@ public function checkChanges(): void
117117

118118
$this->commit();
119119

120+
if (!$this->initialized) {
121+
$this->initialized = true;
122+
}
123+
120124
$this->checking = false;
121-
$this->initialized = true;
122125
}
123126

124127
public function tick(?callable $handler = null): void
@@ -136,7 +139,7 @@ public function tick(?callable $handler = null): void
136139
$this->tick(function ($checkChanges, $interval) {
137140
do {
138141
call_user_func($checkChanges);
139-
sleep($interval);
142+
sleep($interval / 1000);
140143
} while (true);
141144
});
142145
}

0 commit comments

Comments
 (0)