Altax\Module\Task\Process\Process::run PHP Method

run() public method

Runing a command on remote server.
public run ( string $commandline, array $options = [] ) : ProcessResult
$commandline string
$options array
return ProcessResult
    public function run($commandline, $options = array())
    {
        if (!$this->node) {
            throw new \RuntimeException("Node is not defined to run the command.");
        }
        if (is_array($commandline)) {
            $commandline = implode(" && ", $commandline);
        }
        $this->runtimeTask->getOutput()->writeln($this->getRemoteInfoPrefix() . "<info>Run: </info>{$commandline}");
        $realCommand = $this->compileRealCommand($commandline, $options);
        if ($this->runtimeTask->getOutput()->isVeryVerbose()) {
            $this->runtimeTask->getOutput()->writeln($this->getRemoteInfoPrefix() . "<info>Real command: </info>{$realCommand}");
        }
        $ssh = $this->getSSH();
        $self = $this;
        if (isset($options["timeout"])) {
            $ssh->setTimeout($options["timeout"]);
        } else {
            $ssh->setTimeout(null);
        }
        $resultContent = null;
        $ssh->exec($realCommand, function ($buffer) use($self, &$resultContent) {
            $self->getRuntimeTask()->getOutput()->write($buffer);
            $resultContent .= $buffer;
        });
        $returnCode = $ssh->getExitStatus();
        return new ProcessResult($returnCode, $resultContent);
    }

Usage Example

Example #1
0
 public function testRun()
 {
     $this->runtimeTask->getOutput()->setVerbosity(3);
     $node = new Node();
     $node->setName("127.0.0.1");
     $process = new Process($this->runtimeTask, $node);
     $process->run("echo helloworld", array("cwd" => "~"));
     $output = $process->getRuntimeTask()->getOutput()->fetch();
     $this->assertRegExp("/helloworld/", $output);
     $this->assertRegExp('/Real command: bash -l -c "cd ~ && echo helloworld"/', $output);
     //echo $output;
 }