-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
194 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace think\swoole\watcher; | ||
|
||
abstract class Driver | ||
{ | ||
abstract public function watch(callable $callback); | ||
|
||
abstract public function stop(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace think\swoole\watcher\driver; | ||
|
||
use InvalidArgumentException; | ||
use Swoole\Coroutine\System; | ||
use Symfony\Component\Finder\Glob; | ||
use Symfony\Component\Process\Process; | ||
use think\swoole\watcher\Driver; | ||
use Throwable; | ||
|
||
class Fswatch extends Driver | ||
{ | ||
protected $directory; | ||
protected $matchRegexps = []; | ||
/** @var Process */ | ||
protected $process; | ||
|
||
public function __construct($config) | ||
{ | ||
$ret = System::exec('which fswatch'); | ||
if (empty($ret['output'])) { | ||
throw new InvalidArgumentException('which not exists.'); | ||
} | ||
|
||
$this->directory = $config['directory']; | ||
|
||
if (!empty($config['name'])) { | ||
foreach ($config['name'] as $value) { | ||
$this->matchRegexps[] = Glob::toRegex($value); | ||
} | ||
} | ||
} | ||
|
||
public function watch(callable $callback) | ||
{ | ||
$command = $this->getCommand(); | ||
$this->process = new Process($command, timeout: 0); | ||
try { | ||
$this->process->run(function ($type, $data) use ($callback) { | ||
$files = array_unique(array_filter(explode("\n", $data))); | ||
if (!empty($this->matchRegexps)) { | ||
$files = array_filter($files, function ($file) { | ||
$filename = basename($file); | ||
foreach ($this->matchRegexps as $regex) { | ||
if (preg_match($regex, $filename)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}); | ||
} | ||
if (!empty($files)) { | ||
$callback($files); | ||
} | ||
}); | ||
} catch (Throwable) { | ||
|
||
} | ||
} | ||
|
||
protected function getCommand() | ||
{ | ||
$command = ["fswatch", "--format=%p", '-r', '--event=Created', '--event=Updated', '--event=Removed', '--event=Renamed']; | ||
|
||
return [...$command, ...$this->directory]; | ||
} | ||
|
||
public function stop() | ||
{ | ||
if ($this->process) { | ||
$this->process->stop(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
<?php | ||
define('STUB_DIR', realpath(__DIR__ . '/stub')); | ||
|
||
$app = new \think\App(STUB_DIR); | ||
|
||
$app->initialize(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
use Swoole\Coroutine; | ||
use Swoole\Timer; | ||
use think\swoole\Watcher; | ||
use think\swoole\watcher\Driver; | ||
use function Swoole\Coroutine\run; | ||
|
||
beforeEach(function () { | ||
app()->config->set([ | ||
'hot_update' => [ | ||
'name' => ['*.txt'], | ||
'include' => [runtime_path()], | ||
'exclude' => [], | ||
], | ||
], 'swoole'); | ||
}); | ||
|
||
it('test fswatch watcher', function ($type) { | ||
run(function () use ($type) { | ||
$monitor = app(Watcher::class)->monitor($type); | ||
expect($monitor)->toBeInstanceOf(Driver::class); | ||
|
||
$changes = []; | ||
Coroutine::create(function () use (&$changes, $monitor) { | ||
$monitor->watch(function ($data) use (&$changes) { | ||
$changes = array_merge($changes, $data); | ||
}); | ||
}); | ||
Timer::after(500, function () { | ||
file_put_contents(runtime_path() . 'some.css', 'test'); | ||
file_put_contents(runtime_path() . 'test.txt', 'test'); | ||
}); | ||
|
||
sleep(3); | ||
|
||
expect($changes)->toBe([runtime_path() . 'test.txt']); | ||
$monitor->stop(); | ||
}); | ||
})->with([ | ||
'find', | ||
'fswatch', | ||
'scan', | ||
])->after(function () { | ||
@unlink(runtime_path() . 'test.css'); | ||
@unlink(runtime_path() . 'test.txt'); | ||
}); |