Watcher plugin fixes
* com.noccy.watcher: Initial inotify support * ScriptRunner now accepts an array of local vars for expansion when evaluating scripts
This commit is contained in:
@ -26,16 +26,16 @@ class ScriptRunner
|
||||
$this->evaluate($script);
|
||||
}
|
||||
|
||||
public function evaluate(string|array $script)
|
||||
public function evaluate(string|array $script, array $locals=[])
|
||||
{
|
||||
if (is_array($script)) {
|
||||
foreach ($script as $step) {
|
||||
$this->evaluate($step);
|
||||
$this->evaluate($step, $locals);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
$script = $this->expandString($script);
|
||||
$script = $this->expandString($script, $locals);
|
||||
|
||||
// Determine what to do
|
||||
if (str_starts_with($script, '@')) {
|
||||
@ -81,10 +81,12 @@ class ScriptRunner
|
||||
}
|
||||
}
|
||||
|
||||
public function expandString(string $input)
|
||||
public function expandString(string $input, array $locals=[])
|
||||
{
|
||||
return preg_replace_callback('/(\$\{(.+?)\})/', function ($match) {
|
||||
|
||||
return preg_replace_callback('/(\$\{(.+?)\})/', function ($match) use ($locals) {
|
||||
if (array_key_exists($match[2], $locals)) {
|
||||
return $locals[$match[2]];
|
||||
}
|
||||
return ($_ENV[$match[2]]??getenv($match[2]))??null;
|
||||
}, $input);
|
||||
}
|
||||
|
Reference in New Issue
Block a user