mikehaertl\shellcommand\Command::execute PHP Method

execute() public method

Execute the command
public execute ( ) : boolean
return boolean whether execution was successful. If false, error details can be obtained through getError(), getStdErr() and getExitCode().
    public function execute()
    {
        $command = $this->getExecCommand();
        if (!$command) {
            return false;
        }
        if ($this->useExec) {
            $execCommand = $this->captureStdErr ? "{$command} 2>&1" : $command;
            exec($execCommand, $output, $this->_exitCode);
            $this->_stdOut = implode("\n", $output);
            if ($this->_exitCode !== 0) {
                $this->_stdErr = $this->_stdOut;
                $this->_error = empty($this->_stdErr) ? 'Command failed' : $this->_stdErr;
                return false;
            }
        } else {
            $descriptors = array(1 => array('pipe', 'w'), 2 => array('pipe', $this->getIsWindows() ? 'a' : 'w'));
            $process = proc_open($command, $descriptors, $pipes, $this->procCwd, $this->procEnv, $this->procOptions);
            if (is_resource($process)) {
                $this->_stdOut = stream_get_contents($pipes[1]);
                $this->_stdErr = stream_get_contents($pipes[2]);
                fclose($pipes[1]);
                fclose($pipes[2]);
                $this->_exitCode = proc_close($process);
                if ($this->_exitCode !== 0) {
                    $this->_error = $this->_stdErr ? $this->_stdErr : "Failed without error message: {$command}";
                    return false;
                }
            } else {
                $this->_error = "Could not run command {$command}";
                return false;
            }
        }
        $this->_executed = true;
        return true;
    }

Usage Example

Exemplo n.º 1
0
Arquivo: Tor.php Projeto: yadakhov/tor
 /**
  * Execute tor command
  *
  * @param $command
  * @return Command|string
  */
 public function command($command)
 {
     if (!in_array($command, ['start', 'stop', 'restart', 'reload', 'force-reload', 'status'])) {
         throw new \InvalidArgumentException($command);
     }
     $command = 'sudo /etc/init.d/tor ' . $command;
     $command = new Command($command);
     $command->execute();
     return $command;
 }
All Usage Examples Of mikehaertl\shellcommand\Command::execute