Misc fixes and improvements
* Added request logging to com.noccy.apiclient * Added plugin com.noccy.watcher * Added pipe command and filter support * Fixes and stubs
This commit is contained in:
53
plugins/com.noccy.watcher/FileWatcher.php
Normal file
53
plugins/com.noccy.watcher/FileWatcher.php
Normal file
@ -0,0 +1,53 @@
|
||||
<?php // "name":"Watch files and act when they are changed", "author":"Noccy"
|
||||
|
||||
namespace SparkPlug\Com\Noccy\Watcher;
|
||||
|
||||
use Spark\Environment\ScriptRunner;
|
||||
use SparkPlug\Com\Noccy\Watcher\Monitor\MonitorInterface;
|
||||
use SparkPlug\Com\Noccy\Watcher\Monitor\MtimeMonitor;
|
||||
use SparkPlug\Com\Noccy\Watcher\Monitor\InotifyMonitor;
|
||||
|
||||
class FileWatcher {
|
||||
|
||||
private MonitorInterface $monitor;
|
||||
|
||||
private ScriptRunner $scriptRunner;
|
||||
|
||||
private array $rules = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
if (extension_loaded('inotify')) {
|
||||
$this->monitor = new MtimeMonitor();
|
||||
//$this->monitor = new InotifyMonitor();
|
||||
} else {
|
||||
$this->monitor = new MtimeMonitor();
|
||||
}
|
||||
$this->scriptRunner = get_environment()->getScriptRunner();
|
||||
}
|
||||
|
||||
public function addRule(Rule $rule)
|
||||
{
|
||||
if ($rule->getInitialTrigger()) {
|
||||
$this->triggerRule($rule);
|
||||
}
|
||||
$this->rules[] = $rule;
|
||||
$this->monitor->add($rule);
|
||||
}
|
||||
|
||||
private function triggerRule(Rule $rule)
|
||||
{
|
||||
$actions = $rule->getActions();
|
||||
$this->scriptRunner->evaluate($actions);
|
||||
}
|
||||
|
||||
public function loop()
|
||||
{
|
||||
$this->monitor->loop();
|
||||
$modified = $this->monitor->getModified();
|
||||
foreach ($modified as $rule) {
|
||||
$this->triggerRule($rule);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user