-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRecursiveFolderWatcher.php
167 lines (135 loc) · 3.07 KB
/
RecursiveFolderWatcher.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
* Watch a folder for any changes on files
*/
class RecursiveFolderWatcher
{
/**
* @var string
*/
private $pathToWatch;
/**
* @var resource
*/
private $inotify;
/**
* @var array
*/
private $watchDescriptors = array();
/**
* @var function
*/
private $callbackFunction;
/**
* @var boolean
*/
private $testsHaveRunForThisWatch = false;
/**
* Class constructor
*/
public function __construct($pathToWatch, $callbackFunction)
{
$this->pathToWatch = $pathToWatch;
$this->callbackFunction = $callbackFunction;
}
/**
* Watch a folder for any changes in files
*
* @return void
*/
public function watch() {
$this->inotify = inotify_init();
$this->addWatch($this->pathToWatch);
while (true) {
$events = inotify_read($this->inotify);
if ($events) {
$this->testsHaveRunForThisWatch = false;
array_walk($events, array($this, 'handleEvent'));
}
usleep(1000);
}
}
/**
* Add a watch on a folder
*
* @return void
*/
protected function addWatch($folderPath) {
$watchDescriptor = inotify_add_watch($this->inotify, $folderPath, IN_ALL_EVENTS);
$this->watchDescriptors[$watchDescriptor] = $folderPath;
if (is_dir($folderPath)) $this->addWatchToSubFolders($folderPath);
}
/**
* Add watches to sub folders
*
* @return void
*/
protected function addWatchToSubFolders($parentFolder) {
$iterator = new DirectoryIterator($parentFolder);
foreach ($iterator as $fileInfo) {
if ($fileInfo->isDir() && !$fileInfo->isDot()) {
$this->addWatch($fileInfo->getPathname());
}
}
}
/*
* Check if there is anything to do with an event
*
* @return void
*/
protected function handleEvent(array $event) {
if ($this->doesActionRequiresRunningUnitTest($event['mask'])) {
$this->runUnitTests();
}
if ($this->actionIsForNewFolder($event['mask'])) {
$folderPath = $this->getPathForWatchDescriptor($event['wd']) . $event['name'];
$this->addWatch($folderPath);
}
}
/*
* Check it the action is for the creation of a new folder
*
* @return void
*/
protected function actionIsForNewFolder($mask) {
return (IN_CREATE === (IN_CREATE & $mask) && IN_ISDIR === (IN_ISDIR & $mask));
}
/*
* Cehck if the action required running the unit tests
*
* @return void
*/
protected function doesActionRequiresRunningUnitTest($mask) {
$needsUnitTestRun = false;
if (IN_MODIFY === (IN_MODIFY & $mask)) {
$needsUnitTestRun = true;
}
if ((IN_CREATE === (IN_CREATE & $mask) && 0 === (IN_ISDIR & $mask))) {
$needsUnitTestRun = true;
}
return $needsUnitTestRun;
}
/*
* Return the path for a descriptor
*
* @return string
*/
protected function getPathForWatchDescriptor($watchDescriptor) {
if (array_key_exists($watchDescriptor, $this->watchDescriptors)) {
return $this->watchDescriptors[$watchDescriptor] . '/';
} else {
return '';
}
}
/*
* Run the unit tests
*
* @return void
*/
protected function runUnitTests() {
if (!$this->testsHaveRunForThisWatch) {
$this->testsHaveRunForThisWatch = true;
call_user_func($this->callbackFunction);
}
}
}