public function execute()
{
// try to load the script from the configuration
if ($script = $this->getStepNode()->getExecute()->getScript()) {
// prepare script by prepending the webapp directory
$script = new \SplFileInfo($this->getWebappPath() . DIRECTORY_SEPARATOR . ltrim($script, DIRECTORY_SEPARATOR));
// check if the configured script is a file
if ($script->isFile() === false) {
throw new \Exception(sprintf('Script %s is not a file', $script));
}
// prepare the scripts arguments
$args = '';
if ($params = $this->getStepNode()->getExecute()->getArgs()) {
$args .= ' -- ';
foreach ($params as $param) {
// query whether or not the argument has a name
if ($name = $param->getName()) {
$args .= ' --' . $name;
}
// append the value finally
$args .= ' ' . $param->castToType();
}
}
// prepare the PHP executable, the script and the arguments
$toExecute = $this->getPhpExecutable() . ' -f ' . $script . $args;
// initialize exec() output and return var
$output = array();
$returnVar = 0;
$this->getInitialContext()->getSystemLogger()->info("Now execute: {$toExecute}");
// execute the script on the command line
exec($toExecute, $output, $returnVar);
// check if script has been executed successfully
if ($returnVar !== 0) {
// if not, throw an exception
throw new \Exception(implode(PHP_EOL, $output));
}
}
}