-
Notifications
You must be signed in to change notification settings - Fork 44
/
watcher
133 lines (114 loc) · 3.87 KB
/
watcher
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env php
<?php
/*
|--------------------------------------------------------------------------
| Register Autoloader
|--------------------------------------------------------------------------
|
| Require in the Composer autoloading script so that all the required
| classes for the Resource Watcher are loaded in.
|
*/
require __DIR__.'/vendor/autoload.php';
/*
|--------------------------------------------------------------------------
| Resource Watcher Dependencies
|--------------------------------------------------------------------------
|
| Create a new instance of Illuminate's Filesystem class and of the
| Resource Watcher's Tracker class. These are dependencies of the Resource
| Watcher and will be injected into the constructor.
|
*/
$files = new Illuminate\Filesystem\Filesystem();
$tracker = new JasonLewis\ResourceWatcher\Tracker();
/*
|--------------------------------------------------------------------------
| Instantiate Resource Watcher
|--------------------------------------------------------------------------
|
| Create a new instance of the Resource Watcher so we can watch resources
| for any changes.
|
*/
$watcher = new JasonLewis\ResourceWatcher\Watcher($tracker, $files);
/*
|--------------------------------------------------------------------------
| Watch For Changes
|--------------------------------------------------------------------------
|
| Watch for changes to a resource. The resource given does not need to
| exist to begin watching.
|
*/
$listener = $watcher->watch('foo');
/*
|--------------------------------------------------------------------------
| Create Listener
|--------------------------------------------------------------------------
|
| Listen for any create events that are fired.
|
*/
$listener->onCreate(function ($resource, $path) {
echo "{$path} was created.".PHP_EOL;
});
/*
|--------------------------------------------------------------------------
| Delete Listener
|--------------------------------------------------------------------------
|
| Listen for any delete events that are fired.
|
*/
$listener->onDelete(function ($resource, $path) {
echo "{$path} was deleted.".PHP_EOL;
});
/*
|--------------------------------------------------------------------------
| Modify Listener
|--------------------------------------------------------------------------
|
| Listen for any modify events that are fired.
|
*/
$listener->onModify(function ($resource, $path) {
echo "{$path} was modified.".PHP_EOL;
});
/*
|--------------------------------------------------------------------------
| Anything Listener
|--------------------------------------------------------------------------
|
| Listen for anything.
|
*/
$listener->onAnything(function ($event, $resource, $path) {
switch ($event->getCode()) {
case JasonLewis\ResourceWatcher\Event::RESOURCE_DELETED:
echo "{$path} was deleted (from anything listener).".PHP_EOL;
break;
case JasonLewis\ResourceWatcher\Event::RESOURCE_MODIFIED:
echo "{$path} was modified (from anything listener).".PHP_EOL;
break;
case JasonLewis\ResourceWatcher\Event::RESOURCE_CREATED:
echo "{$path} was created (from anything listener).".PHP_EOL;
break;
}
});
/*
|--------------------------------------------------------------------------
| Start Watching
|--------------------------------------------------------------------------
|
| Now that all the listeners are bound we can start watching. By default
| the watcher will poll for changes every second. You can adjust this by
| passing in an optional first parameter. The interval is given in
| microseconds. 1,000,000 microseconds is 1 second.
|
| By default the watch will continue until such time that it's aborted from
| the terminal. To set a timeout pass in the number of microseconds before
| the watch will abort as the second parameter.
|
*/
$watcher->start();