-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
=
committed
Feb 9, 2024
1 parent
4b775ec
commit 2fb0224
Showing
5 changed files
with
121 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea | ||
vendor | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,35 @@ | ||
# php-tail-f | ||
# tail -f for PHP | ||
|
||
A simple implementation to provide "tail -f" functionality in PHP code. Allows you to monitor newly added lines to a given file. | ||
|
||
This class is designed for implementation within a PHP daemon, specifically excluding its suitability for PHP scripts that generate output for web browsers. | ||
|
||
It is a lightweight class that minimally consumes RAM, CPU, or disk I/O resources. | ||
|
||
## Sample usage | ||
|
||
```php | ||
<?php | ||
|
||
use Nicodemuz\PhpTailF\Monitor; | ||
|
||
require 'vendor/autoload.php'; | ||
|
||
$monitor = new Monitor( | ||
filePath: '/tmp/test.log', | ||
sleepMicroseconds: 500000, | ||
); | ||
|
||
foreach ($monitor->run() as $output) { | ||
echo $output; | ||
} | ||
``` | ||
|
||
|
||
## Authors | ||
|
||
* [Nico Hiort af Ornäs](https://github.com/nicodemuz) | ||
|
||
## Credits | ||
|
||
Based on the work from https://github.com/Basch3000/php-tail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "nicodemuz/php-tail-f", | ||
"description": "Provides tail -f functionailty to watch changes in files", | ||
"type": "library", | ||
"license": "MIT", | ||
"autoload": { | ||
"psr-4": { | ||
"Nicodemuz\\PhpTailF\\": "src/" | ||
} | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Nico Hiort af Ornäs" | ||
} | ||
], | ||
"require": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
use Nicodemuz\PhpTailF\Monitor; | ||
|
||
require 'vendor/autoload.php'; | ||
|
||
$monitor = new Monitor( | ||
filePath: '/tmp/test.log', | ||
sleepMicroseconds: 500000, | ||
); | ||
|
||
foreach ($monitor->run() as $output) { | ||
echo $output; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Nicodemuz\PhpTailF; | ||
|
||
use Generator; | ||
|
||
class Monitor | ||
{ | ||
private int $lastModified = 0; | ||
private int $lastSize = 0; | ||
|
||
public function __construct( | ||
private readonly string $filePath, | ||
private readonly int $sleepMicroseconds | ||
) { | ||
} | ||
|
||
public function run(): Generator | ||
{ | ||
$filePointer = fopen($this->filePath, "r"); | ||
while(true) | ||
{ | ||
clearstatcache(false, $this->filePath); | ||
$modified = filemtime($this->filePath); | ||
|
||
if($modified == $this->lastModified) { | ||
usleep($this->sleepMicroseconds); | ||
continue; | ||
} | ||
|
||
$this->lastModified = $modified; | ||
|
||
$lastSize = filesize($this->filePath); | ||
$bytesAdded = $lastSize - $this->lastSize; | ||
$this->lastSize = $lastSize; | ||
|
||
if($bytesAdded == $lastSize) | ||
{ | ||
fseek($filePointer, $lastSize); | ||
usleep($this->sleepMicroseconds); | ||
continue; | ||
} | ||
|
||
fseek($filePointer, -$bytesAdded); | ||
$added = fread($filePointer, $lastSize); | ||
|
||
yield $added; | ||
|
||
usleep($this->sleepMicroseconds); | ||
} | ||
} | ||
} |