bitandblack / folder-watcher
Watches a folder and handles its files. This script is meant to run from your CLI.
Fund package maintenance!
Buymeacoffee
Requires
- php: >=7.4
- bitandblack/helpers: ^1.0 || ^2.0
- bitandblack/measurement: ^1.0
- bitandblack/pathinfo: ^1.0
- khill/php-duration: ^1.1
- nivpenso/react-file-watcher: ^0
- react/event-loop: ^1.0
- spatie/emoji: ^3.0 || ^4.0
- symfony/console: ^5.0 || ^6.0 || ^7.0
Requires (Dev)
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^9.0
- rector/rector: ^0
- symplify/easy-coding-standard: ^12.0
README
Folder Watcher
Watches a folder and handles its files. This script is meant to run from your CLI.
It may be used to realize a hotfolder on your local system.
Installation
This library is made for the use with Composer. Add it to your project by running $ composer require bitandblack/folder-watcher
.
Usage
Create a new object and tell it the path to the folder you want to watch and a pattern for the files you want to select:
<?php
use BitAndBlack\FolderWatcher\FolderWatcher;
$watcher = new FolderWatcher(
'/path/to/your/folder',
'*.txt'
);
Set a callback function to handle the files one after another:
<?php
$watcher->setCallback(static function($file) {
echo 'Handling file ' . $file . ' now' . PHP_EOL;
});
Start the watcher:
<?php
$watcher->watch();
Cancel the watcher
Per default the watcher is meant to run forever. You can change this by calling finishOnEmptyFolder()
. It's always possible to abort by pressing the keys Ctrl
+ C
. The script will stop after finishing the current task.
Logging
The watcher may give information about the files inside a folder and is able to pass it to the Symfony console. Set the console output object to the watcher with setOutput($consoleOutput)
.
Help
If you have any questions, feel free to contact us under hello@bitandblack.com
.
Further information about Bit&Black can be found under www.bitandblack.com.