-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch.php
87 lines (76 loc) · 2.67 KB
/
watch.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
/**
* Created by PhpStorm.
* User: LinFei
* Created time 2022/10/21 21:59:51
* E-mail: [email protected]
*/
declare (strict_types=1);
use library\File;
use library\Helper;
use library\Process;
require __DIR__ . '/vendor/autoload.php';
Mix\Cli\Cli::setName('fly-watch')->setVersion('1.0.0-alpha');
$cmd = new Mix\Cli\Command([
'name' => 'run',
'short' => 'Run file hot update',
'singleton' => true,
'run' => function () {
// Contained paths, multiple paths separated by ","
$include = Mix\Cli\Flag::match('i', 'include')->string();
// Excluded paths, multiple paths separated by ","
$exclude = Mix\Cli\Flag::match('e', 'exclude')->string();
// Monitoring file interval time in seconds
$interval = Mix\Cli\Flag::match('t', 'interval')->float(2);
// Program start command
$startCmd = Mix\Cli\Flag::match('c', 'cmd')->string();
// The signal sent to the child process. The default value is 9
$signal = Mix\Cli\Flag::match('s', 'signal')->int(9);
if (empty($include)) {
echo 'The --include option cannot be empty' . PHP_EOL . PHP_EOL;
die();
}
if (empty($startCmd)) {
echo 'The --cmd option cannot be empty' . PHP_EOL . PHP_EOL;
die();
}
// Splitting parameters into arrays
$include = Helper::split(',', $include);
$exclude = Helper::split(',', $exclude);
$process = new Process($startCmd, $signal);
$process->start();
$driver = new File($include, $exclude, $interval);
$driver->callback(function () use ($process) {
// Detecting the running status of child processes
$process->isRunning();
});
$driver->run(function (string $file) use ($process) {
echo 'File changed: ' . $file . PHP_EOL;
echo 'File changed, reloading...' . PHP_EOL;
$process->restart();
});
}
]);
$cmd->addOption(
new Mix\Cli\Option([
'names' => ['i', 'include'],
'usage' => 'Contained paths, multiple paths separated by ","'
]),
new Mix\Cli\Option([
'names' => ['e', 'exclude'],
'usage' => 'Excluded paths, multiple paths separated by ","'
]),
new Mix\Cli\Option([
'names' => ['t', 'interval'],
'usage' => 'Monitoring file interval time in seconds'
]),
new Mix\Cli\Option([
'names' => ['c', 'cmd'],
'usage' => 'Program start command'
]),
new Mix\Cli\Option([
'names' => ['s', 'signal'],
'usage' => 'The signal sent to the child process. The default value is 9 (SIGKILL)'
]),
);
Mix\Cli\Cli::addCommand($cmd)->run();