function _drush_proc_open

3.x backend.inc _drush_proc_open($cmd, $data = NULL, $context = NULL)
4.x backend.inc _drush_proc_open($cmd, $data = NULL, $context = NULL)

Call an external command using proc_open.

Parameters

cmd: The command to execute. This command already needs to be properly escaped.

data: An associative array that will be JSON encoded and passed to the script being called. Objects are not allowed, as they do not json_decode gracefully.

Return value

False if the command could not be executed, or did not return any output. If it executed successfully, it returns an associative array containing the command called, the output of the command, and the error code of the command.

1 call to _drush_proc_open()
_drush_backend_invoke in includes/backend.inc
Create a new pipe with proc_open, and attempt to parse the output.

File

includes/backend.inc, line 227
Drush backend API

Code

function _drush_proc_open($cmd, $data = NULL, $context = NULL) {
  $descriptorspec = array(
    0 => array("pipe", "r"), // stdin is a pipe that the child will read from
    1 => array("pipe", "w"), // stdout is a pipe that the child will write to
    2 => array("pipe", "w"), // stderr is a pipe the child will write to
  );
  if (drush_get_context('DRUSH_SIMULATE') && !array_key_exists('#override-simulated', $data)) {
    drush_print('proc_open: ' . $cmd);
    return FALSE;
  }
  $process = proc_open($cmd, $descriptorspec, $pipes, null, null, array('context' => $context));
  if (is_resource($process)) {
    if ($data) {
      fwrite($pipes[0], json_encode($data)); // pass the data array in a JSON encoded string
    }

    $info = stream_get_meta_data($pipes[1]);
    stream_set_blocking($pipes[1], TRUE);
    stream_set_timeout($pipes[1], 1);
    $string = '';
    while (!feof($pipes[1]) && !$info['timed_out']) {
      $string .= fgets($pipes[1], 4096);
      $info = stream_get_meta_data($pipes[1]);
      flush();
    }

    $info = stream_get_meta_data($pipes[2]);
    stream_set_blocking($pipes[2], TRUE);
    stream_set_timeout($pipes[2], 1);
    while (!feof($pipes[2]) && !$info['timed_out']) {
      $string .= fgets($pipes[2], 4096);
      $info = stream_get_meta_data($pipes[2]);
      flush();
    }

    fclose($pipes[0]);
    fclose($pipes[1]);
    fclose($pipes[2]);
    $code = proc_close($process);
    return array(
      'cmd' => $cmd,
      'output' => $string,
      'code' => $code,
    );
  }
  return FALSE;
}