forked from jasonlewis/resource-watcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher
119 lines (99 loc) · 3.19 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
#!/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\ResourceWatcher($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('path/to/resource');
/*
|--------------------------------------------------------------------------
| Create Listener
|--------------------------------------------------------------------------
|
| Listen for any create events that are fired.
|
*/
$listener->onCreate(function($resource)
{
$path = $resource->getPath();
echo "{$path} was created.".PHP_EOL;
});
/*
|--------------------------------------------------------------------------
| Delete Listener
|--------------------------------------------------------------------------
|
| Listen for any delete events that are fired.
|
*/
$listener->onDelete(function($resource)
{
$path = $resource->getPath();
echo "{$path} was deleted.".PHP_EOL;
});
/*
|--------------------------------------------------------------------------
| Modify Listener
|--------------------------------------------------------------------------
|
| Listen for any modify events that are fired.
|
*/
$listener->onModify(function($resource)
{
$path = $resource->getPath();
echo "{$path} was modified.".PHP_EOL;
});
/*
|--------------------------------------------------------------------------
| 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->startWatch();