1
1
<?php
2
2
3
- namespace think \swoole \watcher ;
3
+ namespace think \swoole \watcher \ driver ;
4
4
5
5
use InvalidArgumentException ;
6
6
use Swoole \Coroutine \System ;
7
7
use Swoole \Timer ;
8
8
use think \helper \Str ;
9
- use think \swoole \contract \ WatcherInterface ;
9
+ use think \swoole \watcher \ Driver ;
10
10
11
- class Find implements WatcherInterface
11
+ class Find extends Driver
12
12
{
13
13
protected $ name ;
14
14
protected $ directory ;
15
15
protected $ exclude ;
16
+ protected $ timer = null ;
16
17
17
- public function __construct ($ directory , $ exclude , $ name )
18
+ public function __construct ($ config )
18
19
{
19
20
$ ret = System::exec ('which find ' );
20
21
if (empty ($ ret ['output ' ])) {
@@ -25,9 +26,9 @@ public function __construct($directory, $exclude, $name)
25
26
throw new InvalidArgumentException ('find version not support. ' );
26
27
}
27
28
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 ' ] ;
31
32
}
32
33
33
34
public function watch (callable $ callback )
@@ -63,15 +64,23 @@ public function watch(callable $callback)
63
64
64
65
$ command = "find {$ dest }{$ name }{$ notName }{$ notPath } -mmin {$ minutes } -type f -print " ;
65
66
66
- Timer::tick ($ ms , function () use ($ callback , $ command ) {
67
+ $ this -> timer = Timer::tick ($ ms , function () use ($ callback , $ command ) {
67
68
$ ret = System::exec ($ command );
68
69
if ($ ret ['code ' ] === 0 && strlen ($ ret ['output ' ])) {
69
70
$ stdout = trim ($ ret ['output ' ]);
70
71
if (!empty ($ stdout )) {
71
- call_user_func ($ callback );
72
+ $ files = array_filter (explode ("\n" , $ stdout ));
73
+ call_user_func ($ callback , $ files );
72
74
}
73
75
}
74
76
});
75
77
}
76
78
79
+ public function stop ()
80
+ {
81
+ if ($ this ->timer ) {
82
+ Timer::clear ($ this ->timer );
83
+ }
84
+ }
85
+
77
86
}
0 commit comments