-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.php
105 lines (74 loc) · 2.58 KB
/
command.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
declare(strict_types=1);
namespace Assess\Examples;
use Assess\Configuration;
use Assess\Event\Event;
use Assess\Event\EventType;
use Assess\Watcher;
use Revolt\EventLoop;
use function array_slice;
use function array_map;
use function array_unique;
use function memory_get_peak_usage;
use const SIGINT;
require_once __DIR__ . '/../vendor/autoload.php';
$directories = array_slice($argv, 1);
$directories = array_map(realpath(...), $directories);
$directories = array_unique($directories);
if (empty($directories)) {
echo "Usage: php command.php <directory> [<directory> ...]\n";
exit(1);
}
$configuration = Configuration::createForDirectories($directories)
// poll interval in seconds
->withPollInterval(0.5)
// do not watch directories
->withWatchDirectories(false)
// include only PHP files
->withExtensions(['php'])
// do not watch for access
->withWatchForAccess(true)
// do not watch for changes
->withWatchForChanges(true)
;
$watcher = Watcher::create($configuration);
$watcher->register(EventType::Created, function (Event $event): void {
$node = $event->newIndex->nodes[$event->id];
echo "Created: {$node->path}\n";
});
$watcher->register(EventType::Modified, function (Event $event): void {
$node = $event->newIndex->nodes[$event->id];
echo "Modified: {$node->path}\n";
});
$watcher->register(EventType::Accessed, function (Event $event): void {
$node = $event->newIndex->nodes[$event->id];
echo "Accessed: {$node->path}\n";
});
$watcher->register(EventType::Changed, function (Event $event): void {
$node = $event->newIndex->nodes[$event->id];
echo "Changed: {$node->path}\n";
});
$watcher->register(EventType::Moved, function (Event $event): void {
$oldNode = $event->oldIndex->nodes[$event->id];
$newNode = $event->newIndex->nodes[$event->id];
echo "Moved: {$oldNode->path} -> {$newNode->path}\n";
});
$watcher->register(EventType::Deleted, function (Event $event): void {
$node = $event->oldIndex->nodes[$event->id];
echo "Deleted: {$node->path}\n";
});
$watcher->enable();
$watcher->reference();
echo "Started watching...\n";
EventLoop::unreference(EventLoop::repeat($configuration->pollInterval * 5, function (): void {
$peakMemory = memory_get_peak_usage(true) / 1024 / 1024;
echo "Watching... (Peak memory: {$peakMemory} MB)\n";
}));
EventLoop::onSignal(SIGINT, function (string $callbackId) use($watcher): void {
EventLoop::cancel($callbackId);
echo "Stopping...\n";
$watcher->disable();
$watcher->unreference();
});
EventLoop::run();
echo "Stopped watching...\n";