Skip to content

Commit 3608bb9

Browse files
authored
Support Swow for watcher (#5164)
1 parent 594d56a commit 3608bb9

9 files changed

Lines changed: 75 additions & 24 deletions

File tree

collector-reload.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
$basePath = $dir;
2323
}
2424

25+
! defined('SWOOLE_HOOK_ALL') && define('SWOOLE_HOOK_ALL', 0);
2526
! defined('BASE_PATH') && define('BASE_PATH', $basePath);
2627
! defined('SWOOLE_HOOK_FLAGS') && define('SWOOLE_HOOK_FLAGS', SWOOLE_HOOK_ALL);
2728

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
"hyperf/framework": "~3.0.0"
1515
},
1616
"autoload": {
17+
"files": [
18+
"src/Functions.php"
19+
],
1720
"psr-4": {
1821
"Hyperf\\Watcher\\": "src/"
1922
}

src/Driver/FindDriver.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
use Hyperf\Engine\Channel;
1515
use Hyperf\Utils\Str;
1616
use Hyperf\Watcher\Option;
17-
use Swoole\Coroutine\System;
17+
18+
use function Hyperf\Watcher\exec;
1819

1920
class FindDriver extends AbstractDriver
2021
{
@@ -27,16 +28,16 @@ public function __construct(protected Option $option)
2728
parent::__construct($option);
2829

2930
if ($this->isDarwin()) {
30-
$ret = System::exec('which gfind');
31+
$ret = exec('which gfind');
3132
if (empty($ret['output'])) {
3233
throw new \InvalidArgumentException('gfind not exists. You can `brew install findutils` to install it.');
3334
}
3435
} else {
35-
$ret = System::exec('which find');
36+
$ret = exec('which find');
3637
if (empty($ret['output'])) {
3738
throw new \InvalidArgumentException('find not exists.');
3839
}
39-
$ret = System::exec('find --help', true);
40+
$ret = exec('find --help');
4041
$this->isSupportFloatMinutes = ! str_contains($ret['output'] ?? '', 'BusyBox');
4142
}
4243
}
@@ -73,7 +74,7 @@ protected function find(array $fileModifyTimes, array $targets, string $minutes,
7374
{
7475
$changedFiles = [];
7576
$dest = implode(' ', $targets);
76-
$ret = System::exec($this->getBin() . ' ' . $dest . ' -mmin ' . $minutes . ' -type f -print');
77+
$ret = exec($this->getBin() . ' ' . $dest . ' -mmin ' . $minutes . ' -type f -print');
7778
if ($ret['code'] === 0 && strlen($ret['output'])) {
7879
$stdout = trim($ret['output']);
7980

src/Driver/FindNewerDriver.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
use Hyperf\Engine\Channel;
1515
use Hyperf\Utils\Str;
1616
use Hyperf\Watcher\Option;
17-
use Swoole\Coroutine\System;
17+
18+
use function Hyperf\Watcher\exec;
1819

1920
class FindNewerDriver extends AbstractDriver
2021
{
@@ -27,13 +28,13 @@ class FindNewerDriver extends AbstractDriver
2728
public function __construct(protected Option $option)
2829
{
2930
parent::__construct($option);
30-
$ret = System::exec('which find');
31+
$ret = exec('which find');
3132
if (empty($ret['output'])) {
3233
throw new \InvalidArgumentException('find not exists.');
3334
}
3435
// create two files
35-
System::exec('echo 1 > ' . $this->getToModifyFile());
36-
System::exec('echo 1 > ' . $this->getToScanFile());
36+
exec('echo 1 > ' . $this->getToModifyFile());
37+
exec('echo 1 > ' . $this->getToScanFile());
3738
}
3839

3940
public function watch(Channel $channel): void
@@ -48,8 +49,8 @@ public function watch(Channel $channel): void
4849
++$this->count;
4950
// update mtime
5051
if ($changedFiles) {
51-
System::exec('echo 1 > ' . $this->getToModifyFile());
52-
System::exec('echo 1 > ' . $this->getToScanFile());
52+
exec('echo 1 > ' . $this->getToModifyFile());
53+
exec('echo 1 > ' . $this->getToScanFile());
5354
}
5455

5556
foreach ($changedFiles as $file) {
@@ -75,7 +76,7 @@ protected function find(array $targets, array $ext = []): array
7576
$shell = $shell . sprintf('find %s -newer %s -type f', $dest, $file) . $symbol;
7677
}
7778

78-
$ret = System::exec($shell);
79+
$ret = exec($shell);
7980
if ($ret['code'] === 0 && strlen($ret['output'])) {
8081
$stdout = $ret['output'];
8182
$lineArr = explode(PHP_EOL, $stdout);

src/Driver/FswatchDriver.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
use Hyperf\Engine\Coroutine;
1616
use Hyperf\Utils\Str;
1717
use Hyperf\Watcher\Option;
18-
use Swoole\Coroutine\System;
18+
19+
use function Hyperf\Watcher\exec;
1920

2021
class FswatchDriver extends AbstractDriver
2122
{
@@ -24,7 +25,7 @@ class FswatchDriver extends AbstractDriver
2425
public function __construct(protected Option $option)
2526
{
2627
parent::__construct($option);
27-
$ret = System::exec('which fswatch');
28+
$ret = exec('which fswatch');
2829
if (empty($ret['output'])) {
2930
throw new \InvalidArgumentException('fswatch not exists. You can `brew install fswatch` to install it.');
3031
}

src/Functions.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact group@hyperf.io
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace Hyperf\Watcher;
13+
14+
use RuntimeException;
15+
16+
if (function_exists('exec')) {
17+
/**
18+
* @return mixed
19+
*/
20+
function exec(string $command)
21+
{
22+
if (class_exists(\Swoole\Coroutine\System::class)) {
23+
return \Swoole\Coroutine\System::exec($command);
24+
}
25+
26+
if (function_exists('\exec')) {
27+
\exec($command, $output, $code);
28+
$output = implode(PHP_EOL, $output);
29+
30+
return compact('code', 'output');
31+
}
32+
33+
if (function_exists('\passthru')) {
34+
ob_start();
35+
\passthru($command, $code);
36+
$output = ob_get_clean();
37+
ob_end_clean();
38+
39+
return compact('code', 'output');
40+
}
41+
42+
throw new RuntimeException('No available function to run command.');
43+
}
44+
}

src/Watcher.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
use Hyperf\Watcher\Driver\DriverInterface;
2222
use PhpParser\PrettyPrinter\Standard;
2323
use Psr\Container\ContainerInterface;
24-
use Swoole\Coroutine\System;
25-
use Swoole\Process;
2624
use Symfony\Component\Console\Output\OutputInterface;
2725

2826
class Watcher
@@ -72,7 +70,7 @@ public function run()
7270
$this->restart(false);
7371
}
7472
} else {
75-
$ret = System::exec(sprintf('%s %s/vendor/hyperf/watcher/collector-reload.php %s', $this->option->getBin(), BASE_PATH, $file));
73+
$ret = exec(sprintf('%s %s/vendor/hyperf/watcher/collector-reload.php %s', $this->option->getBin(), BASE_PATH, $file));
7674
if ($ret['code'] === 0) {
7775
$this->output->writeln('Class reload success.');
7876
} else {
@@ -86,7 +84,7 @@ public function run()
8684

8785
public function dumpautoload()
8886
{
89-
$ret = System::exec('composer dump-autoload -o --no-scripts -d ' . BASE_PATH);
87+
$ret = exec('composer dump-autoload -o --no-scripts -d ' . BASE_PATH);
9088
$this->output->writeln($ret['output'] ?? '');
9189
}
9290

@@ -107,8 +105,8 @@ public function restart($isStart = true)
107105
$pid = $this->filesystem->get($file);
108106
try {
109107
$this->output->writeln('Stop server...');
110-
if (Process::kill((int) $pid, 0)) {
111-
Process::kill((int) $pid, SIGTERM);
108+
if (posix_kill((int) $pid, 0)) {
109+
posix_kill((int) $pid, SIGTERM);
112110
}
113111
} catch (\Throwable) {
114112
$this->output->writeln('Stop server failed. Please execute `composer dump-autoload -o`');

tests/Driver/FswatchDriverTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
use Hyperf\Watcher\Option;
1919
use HyperfTest\Watcher\Stub\ContainerStub;
2020
use PHPUnit\Framework\TestCase;
21-
use Swoole\Coroutine\System;
21+
22+
use function Hyperf\Watcher\exec;
2223

2324
/**
2425
* @internal
@@ -34,7 +35,7 @@ public function testWatch()
3435
try {
3536
$driver = new FswatchDriver($option);
3637
Coroutine::create(fn () => $driver->watch($channel));
37-
System::exec('echo 1 > /tmp/.env');
38+
exec('echo 1 > /tmp/.env');
3839
$this->assertStringEndsWith('.env', $channel->pop($option->getScanIntervalSeconds() + 0.1));
3940
} catch (\InvalidArgumentException $e) {
4041
if (str_contains($e->getMessage(), 'fswatch not exists')) {

tests/Driver/ScanFileDriverTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
use HyperfTest\Watcher\Stub\ContainerStub;
2020
use HyperfTest\Watcher\Stub\ScanFileDriverStub;
2121
use PHPUnit\Framework\TestCase;
22-
use Swoole\Coroutine\System;
22+
23+
use function Hyperf\Watcher\exec;
2324

2425
/**
2526
* @internal
@@ -37,7 +38,7 @@ public function testWatch()
3738

3839
$driver->watch($channel);
3940

40-
System::exec('echo 1 > /tmp/.env');
41+
exec('echo 1 > /tmp/.env');
4142
$this->assertStringEndsWith('.env', $channel->pop($option->getScanIntervalSeconds() + 0.1));
4243
$channel->close();
4344
}

0 commit comments

Comments
 (0)