Christopher Vagnetoft
30dfd4889b
* Added request logging to com.noccy.apiclient * Added plugin com.noccy.watcher * Added pipe command and filter support * Fixes and stubs
54 lines
1.3 KiB
PHP
54 lines
1.3 KiB
PHP
<?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);
|
|
}
|
|
}
|
|
}
|
|
|