Skip to content

Commit 65c6d55

Browse files
authored
Merge pull request #41 from localheinz/feature/example
Enhancement: Add example for tailing a log file
2 parents 35bf3e6 + c5e7524 commit 65c6d55

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

examples/tail.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
require dirname(__DIR__) . '/vendor/autoload.php';
4+
5+
$loop = \React\EventLoop\Factory::create();
6+
7+
$filesystem = \React\Filesystem\Filesystem::create($loop);
8+
9+
$path = '/var/log/access.log';
10+
11+
$filesystem->getContents($path)->then(function ($content) use ($loop, $filesystem, $path) {
12+
echo $content;
13+
14+
$lastSize = strlen($content);
15+
16+
$file = $filesystem->file($path);
17+
18+
$file->open('r')->then(function (\React\Stream\ReadableStreamInterface $stream) use ($filesystem, $loop, $file, &$lastSize) {
19+
/** @var \React\Filesystem\Stream\GenericStreamInterface $stream */
20+
$fileDescriptor = $stream->getFiledescriptor();
21+
22+
$adapter = $filesystem->getAdapter();
23+
24+
$loop->addPeriodicTimer(1, function () use ($adapter, $fileDescriptor, $file, &$lastSize) {
25+
$file->size()->then(function ($size) use ($adapter, $fileDescriptor, &$lastSize) {
26+
if ($lastSize === $size) {
27+
return;
28+
}
29+
30+
$adapter->read($fileDescriptor, $size - $lastSize, $lastSize)->then(function ($content) {
31+
echo $content;
32+
});
33+
34+
$lastSize = $size;
35+
});
36+
});
37+
});
38+
});
39+
40+
$loop->run();

0 commit comments

Comments
 (0)