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

runLocally() public method

Running a command on local machine.
public runLocally ( string $commandline, array $options = [] ) : ProcessResult
$commandline string
$options array
return ProcessResult
    public function runLocally($commandline, $options = array())
    {
        if (is_array($commandline)) {
            $os = php_uname('s');
            if (preg_match('/Windows/i', $os)) {
                $commandline = implode(" & ", $commandline);
            } else {
                $commandline = implode(" && ", $commandline);
            }
        }
        $this->runtimeTask->getOutput()->writeln($this->getLocalInfoPrefix() . "<info>Run: </info>{$commandline}");
        $realCommand = $this->compileRealCommand($commandline, $options, TRUE);
        if ($this->runtimeTask->getOutput()->isVeryVerbose()) {
            $this->runtimeTask->getOutput()->writeln($this->getLocalInfoPrefix() . "<info>Real command: </info>{$realCommand}");
        }
        $self = $this;
        $symfonyProcess = new SymfonyProcess($realCommand);
        if (isset($options["timeout"])) {
            $symfonyProcess->setTimeout($options["timeout"]);
        } else {
            $symfonyProcess->setTimeout(null);
        }
        $resultContent = null;
        $returnCode = $symfonyProcess->run(function ($type, $buffer) use($self, &$resultContent) {
            $self->getRuntimeTask()->getOutput()->write($buffer);
            $resultContent .= $buffer;
        });
        return new ProcessResult($returnCode, $resultContent);
    }

Usage Example

Example #1
0
 public function testRunLocally2()
 {
     $this->runtimeTask->getOutput()->setVerbosity(3);
     $node = new Node();
     $node->setName("127.0.0.1");
     $process = new Process($this->runtimeTask, $node);
     $os = php_uname('s');
     if (preg_match('/Windows/i', $os)) {
         $process->runLocally(array("cd C:/Windows/Temp", "cd"));
         $output = $process->getRuntimeTask()->getOutput()->fetch();
         $this->assertRegExp('/Real command: cmd.exe \\/C "cd C:\\/Windows\\/Temp & cd"/', $output);
     } else {
         $process->runLocally(array("cd /var/tmp", "pwd"));
         $output = $process->getRuntimeTask()->getOutput()->fetch();
         $this->assertRegExp('/Real command: bash -l -c "cd \\/var\\/tmp && pwd"/', $output);
     }
 }