11<?php
22
3- namespace think \swoole \watcher ;
3+ namespace think \swoole \watcher \ driver ;
44
55use InvalidArgumentException ;
66use Swoole \Coroutine \System ;
77use Swoole \Timer ;
88use think \helper \Str ;
9- use think \swoole \contract \ WatcherInterface ;
9+ use think \swoole \watcher \ Driver ;
1010
11- class Find implements WatcherInterface
11+ class Find extends Driver
1212{
1313 protected $ name ;
1414 protected $ directory ;
1515 protected $ exclude ;
16+ protected $ timer = null ;
1617
17- public function __construct ($ directory , $ exclude , $ name )
18+ public function __construct ($ config )
1819 {
1920 $ ret = System::exec ('which find ' );
2021 if (empty ($ ret ['output ' ])) {
@@ -25,9 +26,9 @@ public function __construct($directory, $exclude, $name)
2526 throw new InvalidArgumentException ('find version not support. ' );
2627 }
2728
28- $ this ->directory = $ directory ;
29- $ this ->exclude = $ exclude ;
30- $ this ->name = $ name ;
29+ $ this ->directory = $ config [ ' directory ' ] ;
30+ $ this ->exclude = $ config [ ' exclude ' ] ;
31+ $ this ->name = $ config [ ' name ' ] ;
3132 }
3233
3334 public function watch (callable $ callback )
@@ -63,15 +64,23 @@ public function watch(callable $callback)
6364
6465 $ command = "find {$ dest }{$ name }{$ notName }{$ notPath } -mmin {$ minutes } -type f -print " ;
6566
66- Timer::tick ($ ms , function () use ($ callback , $ command ) {
67+ $ this -> timer = Timer::tick ($ ms , function () use ($ callback , $ command ) {
6768 $ ret = System::exec ($ command );
6869 if ($ ret ['code ' ] === 0 && strlen ($ ret ['output ' ])) {
6970 $ stdout = trim ($ ret ['output ' ]);
7071 if (!empty ($ stdout )) {
71- call_user_func ($ callback );
72+ $ files = array_filter (explode ("\n" , $ stdout ));
73+ call_user_func ($ callback , $ files );
7274 }
7375 }
7476 });
7577 }
7678
79+ public function stop ()
80+ {
81+ if ($ this ->timer ) {
82+ Timer::clear ($ this ->timer );
83+ }
84+ }
85+
7786}
0 commit comments