3232
3333namespace ElementaryFramework \FireFS \Watcher ;
3434
35- use ElementaryFramework \FireFS \Listener \IFileSystemListener ;
3635use ElementaryFramework \FireFS \FireFS ;
37- use ElementaryFramework \FireFS \Exceptions \FileSystemEntityNotFoundException ;
38- use ElementaryFramework \FireFS \Exceptions \FileSystemWatcherException ;
3936use ElementaryFramework \FireFS \Events \FileSystemEntityModifiedEvent ;
4037use ElementaryFramework \FireFS \Events \FileSystemEntityDeletedEvent ;
4138use ElementaryFramework \FireFS \Events \FileSystemEntityCreatedEvent ;
39+ use ElementaryFramework \FireFS \Exceptions \FileSystemEntityNotFoundException ;
40+ use ElementaryFramework \FireFS \Exceptions \FileSystemWatcherException ;
41+ use ElementaryFramework \FireFS \Listener \IFileSystemListener ;
4242
43-
43+ /**
44+ * File System Watcher
45+ *
46+ * Watch for changes (files/folders creation, modification and deletion) on the file system.
47+ *
48+ * @package FireFS
49+ * @subpackage Watcher
50+ * @author Axel Nana <[email protected] > 51+ */
4452class FileSystemWatcher
4553{
4654 /**
@@ -76,17 +84,20 @@ class FileSystemWatcher
7684 /**
7785 * The regex pattern of files and folders to watch.
7886 *
79- * @var string
87+ * @var array
8088 */
81- private $ _patternInclude = " /^.*$/U " ;
89+ private $ _patternInclude = array () ;
8290
8391 /**
8492 * The regex pattern of files and folders
8593 * to excludes from watching.
8694 *
87- * @var string
95+ * @var array
8896 */
89- private $ _patternExclude = "" ;
97+ private $ _patternExclude = array (
98+ "/^.+[\/ \\\\]node_modules[\/ \\\\].*$/ " ,
99+ "/^.+[\/ \\\\]\.git[\/ \\\\].*$/ "
100+ );
90101
91102 /**
92103 * The number of milliseconds to wait before
@@ -186,16 +197,58 @@ public function setRecursive(bool $recursive): self
186197 return $ this ;
187198 }
188199
189- public function setPattern (string $ pattern ): self
200+ /**
201+ * Adds a new regex pattern for files to watch.
202+ *
203+ * @param string $pattern The regex pattern to add.
204+ *
205+ * @return self
206+ */
207+ public function addPattern (string $ pattern ): self
190208 {
191- $ this ->_patternInclude = $ pattern ;
209+ $ this ->_patternInclude [] = $ pattern ;
192210
193211 return $ this ;
194212 }
195213
196- public function setExcludePattern (string $ pattern ): self
214+ /**
215+ * Adds a new regex pattern for files to exclude from watcher.
216+ *
217+ * @param string $pattern The regex pattern to add.
218+ *
219+ * @return self
220+ */
221+ public function addExcludePattern (string $ pattern ): self
197222 {
198- $ this ->_patternExclude = $ pattern ;
223+ $ this ->_patternExclude [] = $ pattern ;
224+
225+ return $ this ;
226+ }
227+
228+ /**
229+ * Set the array of regex patterns matching files to watch.
230+ *
231+ * @param array $patterns The array of regex patterns.
232+ *
233+ * @return self
234+ */
235+ public function setPatterns (array $ patterns ) : self
236+ {
237+ $ this ->_patternInclude = $ patterns ;
238+
239+ return $ this ;
240+ }
241+
242+ /**
243+ * Set the array of regex patterns matching files to exclude from watcher.
244+ *
245+ * @param array $patterns The array of regex patterns.
246+ *
247+ * @return self
248+ */
249+ public function setExcludePatterns (array $ patterns )
250+ {
251+ $ this ->_patternExclude = $ patterns ;
199252
200253 return $ this ;
201254 }
@@ -311,7 +364,6 @@ private function _detectChanges()
311364
312365 private function _watchFolder (string $ _path )
313366 {
314- // TODO: Apply include and exclude patterns
315367 $ directory = $ this ->_fs ->readDir ($ _path , $ this ->_recursive );
316368 $ watching = array_merge ($ this ->_filesCache , $ directory );
317369
@@ -328,26 +380,48 @@ private function _watchFolder(string $_path)
328380
329381 private function _watchFile (string $ _path )
330382 {
331- $ path = $ this ->_fs ->cleanPath ($ _path );
383+ if (count ($ this ->_patternExclude ) > 0 ) {
384+ foreach ($ this ->_patternExclude as $ pattern ) {
385+ if (preg_match ($ pattern , $ _path , $ m )) {
386+ return ;
387+ }
388+ }
389+ }
390+
391+ $ match = true ;
392+
393+ if (count ($ this ->_patternInclude ) > 0 ) {
394+ $ match = false ;
395+ foreach ($ this ->_patternInclude as $ pattern ) {
396+ if (preg_match ($ pattern , $ _path , $ m )) {
397+ $ match = true ;
398+ break ;
399+ }
400+ }
401+ }
402+
403+ if ($ match ) {
404+ $ path = $ this ->_fs ->cleanPath ($ _path );
332405
333- if ($ this ->_fs ->exists ($ path )) {
334- if (array_key_exists ($ path , $ this ->_lastModTimeCache )) {
335- if ($ this ->_lastModTimeCache [$ path ] < $ this ->_lmt ($ path )) {
336- if ($ this ->_listener ->onAny (new FileSystemEntityModifiedEvent ($ path ))) {
337- $ this ->_listener ->onModified (new FileSystemEntityModifiedEvent ($ path ));
406+ if ($ this ->_fs ->exists ($ path )) {
407+ if (array_key_exists ($ path , $ this ->_lastModTimeCache )) {
408+ if ($ this ->_lastModTimeCache [$ path ] < $ this ->_lmt ($ path )) {
409+ if ($ this ->_listener ->onAny (new FileSystemEntityModifiedEvent ($ path ))) {
410+ $ this ->_listener ->onModified (new FileSystemEntityModifiedEvent ($ path ));
411+ }
412+ }
413+ } else {
414+ $ this ->_addForWatch ($ _path );
415+ if ($ this ->_listener ->onAny (new FileSystemEntityCreatedEvent ($ path ))) {
416+ $ this ->_listener ->onModified (new FileSystemEntityCreatedEvent ($ path ));
338417 }
339418 }
340419 } else {
341- $ this ->_addForWatch ($ _path );
342- if ($ this ->_listener ->onAny (new FileSystemEntityCreatedEvent ($ path ))) {
343- $ this ->_listener ->onModified (new FileSystemEntityCreatedEvent ($ path ));
344- }
345- }
346- } else {
347- if (array_key_exists ($ path , $ this ->_lastModTimeCache )) {
348- $ this ->_removeFromWatch ($ _path );
349- if ($ this ->_listener ->onAny (new FileSystemEntityDeletedEvent ($ path ))) {
350- $ this ->_listener ->onDeleted (new FileSystemEntityDeletedEvent ($ path ));
420+ if (array_key_exists ($ path , $ this ->_lastModTimeCache )) {
421+ $ this ->_removeFromWatch ($ _path );
422+ if ($ this ->_listener ->onAny (new FileSystemEntityDeletedEvent ($ path ))) {
423+ $ this ->_listener ->onDeleted (new FileSystemEntityDeletedEvent ($ path ));
424+ }
351425 }
352426 }
353427 }
0 commit comments