Skip to content

Commit 13db14f

Browse files
committed
Switch to amphp/amp v3
1 parent 56713f3 commit 13db14f

29 files changed

+678
-795
lines changed

.github/workflows/ci.yml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
strategy:
2020
matrix:
2121
php-version:
22-
- '8.1'
22+
- '8.2'
2323

2424
steps:
2525
-
@@ -52,7 +52,7 @@ jobs:
5252
strategy:
5353
matrix:
5454
php-version:
55-
- '8.1'
55+
- '8.2'
5656

5757
steps:
5858
-
@@ -85,8 +85,10 @@ jobs:
8585
strategy:
8686
matrix:
8787
php-version:
88-
- '8.0'
89-
- '8.1'
88+
- '8.2'
89+
- '8.3'
90+
- '8.4'
91+
- '8.5'
9092

9193
steps:
9294
-
@@ -109,11 +111,11 @@ jobs:
109111
-
110112
name: "Install Watchman"
111113
run: |
112-
wget https://github.com/facebook/watchman/releases/download/v2020.09.14.00/watchman-v2020.09.14.00-linux.zip
113-
unzip watchman-v2020.09.14.00-linux.zip
114+
wget https://github.com/facebook/watchman/releases/download/v2025.12.22.00/watchman-v2025.12.22.00-linux.zip
115+
unzip watchman-v2025.12.22.00-linux.zip
114116
sudo mkdir -p /usr/local/{bin,lib} /usr/local/var/run/watchman
115-
sudo mv watchman-v2020.09.14.00-linux/bin/watchman /usr/local/bin/watchman
116-
sudo mv watchman-v2020.09.14.00-linux/lib/* /usr/local/lib/
117+
sudo mv watchman-v2025.12.22.00-linux/bin/watchman /usr/local/bin/watchman
118+
sudo mv watchman-v2025.12.22.00-linux/lib/* /usr/local/lib/
117119
sudo chmod 755 /usr/local/bin/watchman
118120
sudo chmod 2777 /usr/local/var/run/watchman
119121
-

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111
/.vscode
1212
.php_cs.cache
1313
.php-cs-fixer.cache
14+
/.phpactor.json

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ Amp FS Watch
44
![CI](https://github.com/phpactor/amp-fswatch/workflows/CI/badge.svg)
55

66
This is an [Amp](https://amphp.org/) library for asynchronously monitor paths
7-
on your file system changes using various stategues.
7+
on your file system changes using various stategies.
88

99
It's been created to trigger code indexing in
1010
[Phpactor](https://github.com/phpactor/phpactor).
1111

12-
- Promise based API.
1312
- Capable of automatically selecting a supported watcher for the current
1413
environment.
1514
- Provides realtime (e.g. ``inotify``) watchers in addition to polling ones.
@@ -39,9 +38,15 @@ Loop::run(function () use () {
3938
[]
4039
);
4140

42-
$process = yield $watcher->watch([$path]);
41+
$process = $watcher->watch([$path]);
4342

44-
while (null !== $file = yield $process->wait()) {
43+
if (defined('SIGINT')) {
44+
EventLoop::onSignal(SIGINT, function () use ($process): void {
45+
$process->stop();
46+
});
47+
}
48+
49+
while (null !== $file = $process->wait()) {
4550
fwrite(STDOUT, sprintf('[%s] %s (%s)'."\n", date('Y-m-d H:i:s.u'), $file->path(), $file->type()));
4651
}
4752
});

bin/watch

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,30 @@
11
#!/usr/bin/env php
22
<?php
33

4-
use Amp\Delayed;
5-
use Amp\Loop;
6-
use Phpactor\AmpFsWatch\ModifiedFile;
74
use Phpactor\AmpFsWatch\WatcherConfig;
85
use Phpactor\AmpFsWatch\Watcher\Fallback\FallbackWatcher;
96
use Phpactor\AmpFsWatch\Watcher\Find\FindWatcher;
107
use Phpactor\AmpFsWatch\Watcher\FsWatch\FsWatchWatcher;
118
use Phpactor\AmpFsWatch\Watcher\Inotify\InotifyWatcher;
12-
use Phpactor\AmpFsWatch\Watcher\Null\NullWatcher;
139
use Phpactor\AmpFsWatch\Watcher\PatternMatching\PatternMatchingWatcher;
1410
use Phpactor\AmpFsWatch\Watcher\PhpPollWatcher\PhpPollWatcher;
1511
use Psr\Log\AbstractLogger;
12+
use Revolt\EventLoop;
1613

1714
require __DIR__ . '/../vendor/autoload.php';
1815

1916
echo "This is a demo application\n";
2017

2118
if (!isset($argv[1])) {
22-
echo "You must specify a path to watch";
19+
echo 'You must specify a path to watch';
2320
exit(1);
2421
}
25-
22+
2623
$path = $argv[1];
2724
$logger = new class extends AbstractLogger {
28-
public function log($level, $message, array $context = [])
25+
public function log($level, $message, array $context = []): void
2926
{
30-
fwrite(STDERR, sprintf('[%s] %s', $level, $message)."\n");
27+
fwrite(STDERR, sprintf('[%s] %s', $level, $message) . "\n");
3128
}
3229
};
3330

@@ -40,18 +37,15 @@ $watcher = new PatternMatchingWatcher(new FallbackWatcher([
4037
new FsWatchWatcher($config, $logger),
4138
], $logger), [ '/**/*.php' ], []);
4239

43-
Loop::run(function () use ($watcher, $path) {
44-
$process = yield $watcher->watch([$path]);
40+
$process = $watcher->watch();
4541

46-
while (null !== $file = yield $process->wait()) {
47-
fwrite(STDOUT, sprintf('[%s] %s (%s)'."\n", date('Y-m-d H:i:s.u'), $file->path(), $file->type()));
48-
}
42+
// Signals are not supported on Windows
43+
if (defined('SIGINT')) {
44+
EventLoop::onSignal(SIGINT, function () use ($process): void {
45+
$process->stop();
46+
});
47+
}
4948

50-
// Signals are not supported on Windows
51-
if(defined('SIGINT')) {
52-
Loop::onSignal(SIGINT, function () use ($process) {
53-
$process->stop();
54-
Loop::stop();
55-
});
56-
}
57-
});
49+
while (null !== $file = $process->wait()) {
50+
fwrite(STDOUT, sprintf('[%s] %s (%s)' . "\n", date('Y-m-d H:i:s.u'), $file->path(), $file->type()));
51+
}

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
}
1111
],
1212
"require": {
13-
"php": "^8.0",
14-
"amphp/amp": "^2.4",
15-
"amphp/process": "^1.1",
13+
"php": "^8.2",
14+
"amphp/amp": "^3.1",
15+
"amphp/process": "^2",
1616
"psr/log": "^1.1",
1717
"webmozart/glob": "^4.4",
1818
"symfony/filesystem": "^5.0|^6.0"
1919
},
2020
"require-dev": {
21-
"amphp/phpunit-util": "^1.3",
21+
"amphp/phpunit-util": "v3.0.0",
2222
"ergebnis/composer-normalize": "^2.0",
2323
"friendsofphp/php-cs-fixer": "^3.15",
2424
"jangregor/phpstan-prophecy": "^1.0",

src/SystemDetector/CommandDetector.php

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,22 @@
33
namespace Phpactor\AmpFsWatch\SystemDetector;
44

55
use Amp\Process\Process;
6-
use Amp\Promise;
76

87
class CommandDetector
98
{
10-
/**
11-
* @return Promise<bool>
12-
*/
13-
public function commandExists(string $command): Promise
9+
public function commandExists(string $command): bool
1410
{
1511
return $this->checkPosixCommand($command);
1612
}
1713

18-
/**
19-
* @return Promise<bool>
20-
*/
21-
private function checkPosixCommand(string $command): Promise
14+
private function checkPosixCommand(string $command): bool
2215
{
23-
return \Amp\call(function () use ($command) {
24-
$process = new Process([
25-
'command',
26-
'-v',
27-
$command
28-
]);
16+
$process = Process::start([
17+
'command',
18+
'-v',
19+
$command,
20+
]);
2921

30-
yield $process->start();
31-
32-
return 0 === yield $process->join();
33-
});
22+
return 0 === $process->join();
3423
}
3524
}

src/Watcher.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,11 @@
22

33
namespace Phpactor\AmpFsWatch;
44

5-
use Amp\Promise;
6-
75
interface Watcher
86
{
9-
/**
10-
* @return Promise<WatcherProcess>
11-
*/
12-
public function watch(): Promise;
13-
14-
/**
15-
* @return Promise<bool>
16-
*/
17-
public function isSupported(): Promise;
7+
public function watch(): WatcherProcess;
188

9+
public function isSupported(): bool;
1910

2011
public function describe(): string;
2112
}

src/Watcher/BufferedWatcher/BufferedWatcher.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Phpactor\AmpFsWatch\Watcher\BufferedWatcher;
44

5-
use Amp\Promise;
65
use Phpactor\AmpFsWatch\Watcher;
6+
use Phpactor\AmpFsWatch\WatcherProcess;
77

88
class BufferedWatcher implements Watcher
99
{
@@ -17,15 +17,13 @@ public function __construct(Watcher $innerWatcher, int $interval)
1717
$this->interval = $interval;
1818
}
1919

20-
public function watch(): Promise
20+
public function watch(): WatcherProcess
2121
{
22-
return \Amp\call(function () {
23-
return new BufferedWatcherProcess(yield $this->innerWatcher->watch(), $this->interval);
24-
});
22+
return new BufferedWatcherProcess($this->innerWatcher->watch(), $this->interval);
2523
}
2624

2725

28-
public function isSupported(): Promise
26+
public function isSupported(): bool
2927
{
3028
return $this->innerWatcher->isSupported();
3129
}

src/Watcher/BufferedWatcher/BufferedWatcherProcess.php

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace Phpactor\AmpFsWatch\Watcher\BufferedWatcher;
44

5-
use Amp\Delayed;
6-
use Amp\Promise;
75
use Phpactor\AmpFsWatch\ModifiedFile;
86
use Phpactor\AmpFsWatch\WatcherProcess;
97
use Throwable;
8+
use function Amp\async;
9+
use function Amp\delay;
1010

1111
class BufferedWatcherProcess implements WatcherProcess
1212
{
@@ -28,9 +28,9 @@ public function __construct(WatcherProcess $innerProcess, int $interval = 500)
2828
$this->innerProcess = $innerProcess;
2929
$this->interval = $interval;
3030

31-
\Amp\asyncCall(function () {
31+
async(function (): void {
3232
try {
33-
while (null !== $modifiedFile = yield $this->innerProcess->wait()) {
33+
while (null !== $modifiedFile = $this->innerProcess->wait()) {
3434
assert($modifiedFile instanceof ModifiedFile);
3535
$this->buffer[$modifiedFile->path()] = $modifiedFile;
3636
}
@@ -47,23 +47,20 @@ public function stop(): void
4747
$this->innerProcess->stop();
4848
}
4949

50-
51-
public function wait(): Promise
50+
public function wait(): ?ModifiedFile
5251
{
53-
return \Amp\call(function () {
54-
while ($this->running || !empty($this->buffer || null !== $this->error)) {
55-
if ($this->error) {
56-
$error = $this->error;
57-
$this->error = null;
58-
throw $error;
59-
}
60-
if ($this->buffer) {
61-
return array_shift($this->buffer);
62-
}
63-
yield new Delayed($this->interval);
52+
while ($this->running || !empty($this->buffer || null !== $this->error)) {
53+
if ($this->error) {
54+
$error = $this->error;
55+
$this->error = null;
56+
throw $error;
6457
}
58+
if ($this->buffer) {
59+
return array_shift($this->buffer);
60+
}
61+
delay($this->interval / 1000);
62+
}
6563

66-
return null;
67-
});
64+
return null;
6865
}
6966
}

0 commit comments

Comments
 (0)