1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace Hhxsv5\LaravelS\Swoole; |
4
|
|
|
|
5
|
|
|
use Hhxsv5\LaravelS\Console\Portal; |
6
|
|
|
use Swoole\Http\Server; |
|
|
|
|
7
|
|
|
use Swoole\Process; |
8
|
|
|
use Symfony\Component\Process\ExecutableFinder; |
|
|
|
|
9
|
|
|
|
10
|
|
|
trait ChokidarWatchTrait |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
public function addChokidarWatchProcess(Server $swoole, array $config, array $laravelConf) |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
if (empty($config['enable'])) { |
15
|
|
|
return false; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
$watchPathFiles = isset($config['watch_path_files']) ? (array)$config['watch_path_files'] : []; |
19
|
|
|
if (empty($watchPathFiles)) { |
20
|
|
|
$this->warning('No file to watch by chokidar'); |
|
|
|
|
21
|
|
|
return false; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
$callback = function (Process $worker) use ($config, $laravelConf, $watchPathFiles) { |
25
|
|
|
$log = !empty($config['log']); |
26
|
|
|
$watch_base_path = $config['watch_base_path']; |
27
|
|
|
$this->setProcessTitle(sprintf('%s laravels: chokidar process', $config['process_prefix'])); |
|
|
|
|
28
|
|
|
$nodeExecutable = (new ExecutableFinder)->find('node'); |
29
|
|
|
$nodeScriptPath = realpath(__DIR__.'/../../bin/file-watcher.cjs'); |
30
|
|
|
$this->info(sprintf('$nodeScriptPath: %s', $nodeScriptPath)); |
|
|
|
|
31
|
|
|
|
32
|
|
|
$watchOptions = isset($config['watch_options']) ? (array)$config['watch_options'] : ['ignoreInitial' => true]; |
33
|
|
|
$worker->exec($nodeExecutable, [$nodeScriptPath, |
|
|
|
|
34
|
|
|
json_encode(collect($watchPathFiles)->map(fn ($path) => $watch_base_path.'/'.$path)), |
|
|
|
|
35
|
|
|
json_encode($watchOptions), |
36
|
|
|
]); |
|
|
|
|
37
|
|
|
|
38
|
|
|
// 获取 Node.js 脚本的输出 |
39
|
|
|
tap($worker->read(), function ($output) use ($log, $laravelConf) { |
|
|
|
|
40
|
|
|
Portal::runLaravelSCommand($laravelConf['root_path'], 'reload'); |
41
|
|
|
if ($log) { |
42
|
|
|
$this->info(sprintf('reloaded by chokidar, reason: %s', $output)); |
43
|
|
|
} |
44
|
|
|
}); |
|
|
|
|
45
|
|
|
}; |
46
|
|
|
|
47
|
|
|
$process = new Process($callback, false, 0); |
48
|
|
|
$swoole->addProcess($process); |
49
|
|
|
return $process; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|