85 lines
2.1 KiB
PHP
85 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace VfxApply\Plugin\Executor;
|
|
|
|
use VfxApply\Plugin;
|
|
use VfxApply\Input;
|
|
use VfxApply\Output;
|
|
use VfxApply\Preset;
|
|
|
|
class Script
|
|
{
|
|
/** @var array Variables */
|
|
protected $vars = [];
|
|
|
|
protected $plugin;
|
|
|
|
public function setPlugin($plugin)
|
|
{
|
|
$this->plugin = $plugin;
|
|
}
|
|
|
|
public function set($k,$v)
|
|
{
|
|
$this->vars[$k] = $v;
|
|
}
|
|
|
|
public function addOperation(Operation $operation)
|
|
{
|
|
$operation->setScript($this);
|
|
$this->operations[] = $operation;
|
|
}
|
|
|
|
public function addHelper($name, Helper $helper)
|
|
{
|
|
$helper->setScript($this);
|
|
$this->helpers[$name] = $helper;
|
|
}
|
|
|
|
public function getHelper($name)
|
|
{
|
|
return $this->helpers[$name];
|
|
}
|
|
|
|
public function execute(array $env)
|
|
{
|
|
$env = $this->buildEnvironment($env, $this->vars);
|
|
//print_r($env);
|
|
foreach ($this->operations as $operation) {
|
|
//printf("Executing: %s\n", $operation->getInfo());
|
|
$dialog = $this->plugin->createDialog(DIALOG_PROGRESS, $operation->getInfo());
|
|
$dialog->show();
|
|
$operation->execute($env, $dialog);
|
|
unset($dialog);
|
|
}
|
|
}
|
|
|
|
private function buildEnvironment(array $env, array $vars)
|
|
{
|
|
$vars = array_merge($env, $vars);
|
|
foreach ($vars as $k=>$var) {
|
|
if (is_array($var)) {
|
|
$esc = empty($var['escape'])?false:$var['escape'];
|
|
$var = preg_replace_callback('/\{\%([a-z]+?)\}/i', function ($match) use (&$vars,$var) {
|
|
$k = $match[1];
|
|
if (empty($vars[$k])) return null;
|
|
return $vars[$k];
|
|
}, $var['value']);
|
|
if ($esc) $var = escapeshellarg($var);
|
|
$vars[$k] = $var;
|
|
}
|
|
}
|
|
return $vars;
|
|
}
|
|
|
|
public function parseVariable($value, array $env)
|
|
{
|
|
return preg_replace_callback('/\{\%([a-z]+?)\}/i', function ($match) use (&$env) {
|
|
$k = $match[1];
|
|
if (empty($env[$k])) return null;
|
|
return $env[$k];
|
|
}, $value);
|
|
}
|
|
}
|
|
|