Skip to content

Commit

Permalink
First implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Feb 9, 2024
1 parent 4b775ec commit 2fb0224
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
vendor
composer.lock
36 changes: 35 additions & 1 deletion README.md
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
17 changes: 17 additions & 0 deletions composer.json
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": {}
}
14 changes: 14 additions & 0 deletions example.php
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;
}
52 changes: 52 additions & 0 deletions src/Monitor.php
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);
}
}
}

0 comments on commit 2fb0224

Please sign in to comment.